We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
let day = 17 import { config } from "https://deno.land/x/dotenv/mod.ts" config() let session = Deno.env.get("AOC_SESSION") let get_stuff = (day): Promise<string> => { let url: string = `https://adventofcode.com/2023/day/${day}/input` console.log(session) let headers = new Headers({ 'Cookie': `session=${session}` }) return fetch(url, { headers }) .then((resp) => resp.text()) .catch((err) => { throw err }) } import {Heap} from "heap-js" get_stuff(day).then((infile) => { // let lines: string[] = infile.split('\n').map( (line) => line.split('').map((c) => parseInt(c)) ) let lines: number[][] = infile.split('\n').map( (line) => line.split('').map((c) => parseInt(c)) ) lines.pop() //console.log(lines) let r1 = 0, r2 = 0 let p2 = false r1 = DIJK( lines, !p2 ) r2 = DIJK( lines, p2 ) console.log("part 1:", r1) console.log("part 2:", r2) }).catch((err) => { throw err }) const checkSetHas = (set: Set<number[]>, this_array: number[]): boolean => { for (let that_array of set) if (checkTwoArraysAreEqual(this_array, that_array)) return true return false } const checkTwoArraysAreEqual = (L:number[], R:number[]): boolean => { return L.length === R.length && L.every((val, idx) => val === R[idx]) } const DIJK = (A : number[][], part2: Boolean) => { if (!A || !A[0]) return undefined let R = A.length, C = A[0].length let r = 0, c = 0, dr = 0, dc = 0, loss = A[0][0], step = 0 let newloss, newr, newc, rr, cc, i let DR: number[] = [1, 0,-1, 0] let DC: number[] = [0, 1, 0,-1] let east: number[] = [0, step, 0, 0, 0, 1] let south: number[] = [0, step, 0, 0, 1, 0] let heapq = new Heap<number[]>((a, b) => a[0] - b[0]); heapq.push( east ) heapq.push( south ) //let seen = new Set<number[]>([0, 0, 0, 0, 0]) let seen = new Set<number[]>(); seen.add([0, 0, 0, 0, 0]); let res = undefined while (heapq.length) { // let temp = heapq.shift() let temp = heapq.pop() let [loss, step, r, c, dr, dc] = temp if (r > R - 1 || r < 0 || c < 0 || c > C - 1) continue if (r == R - 1 && c == C - 1 && step > (part2 ? 3 : -1)) { res = loss break } let state: number[] = [ step, r, c, dr, dc ] if ( checkSetHas(seen, state) ) { continue } seen.add( state ) if ( step > (part2 ? 9 : 2) ) { step = 1 i = -1 while (++i < 4) { rr = DR[i] cc = DC[i] if ( rr == dr && cc == dc || rr == -dr && cc == -dc ) continue newr = r + rr newc = c + cc if ( ! (newr > R - 1 || newr < 0 || newc < 0 || newc > C - 1) ) { newloss = loss + A[newr][newc] heapq.push( [ newloss, step, newr, newc, rr, cc ] ) } } console.log(newloss, r, c) continue } newr = r + dr newc = c + dc // cont. curr direction if ( ! (newr > R - 1 || newr < 0 || newc < 0 || newc > C - 1) ) { newloss = loss + A[newr][newc] heapq.push( [ newloss, step + 1, newr, newc, dr, dc ] ) } // or move sideways if (step > (part2 ? 3 : -1)) { // PART 2 step = 1 i = -1 while (++i < 4) { rr = DR[i] cc = DC[i] if (rr == dr && cc == dc || rr == -dr && cc == -dc) continue newr = r + rr, newc = c + cc if ( ! (newr > R - 1 || newr < 0 || newc < 0 || newc > C - 1) ) { newloss = loss + A[newr][newc] heapq.push( [ newloss, step, newr, newc, rr, cc ] ) } } } } return res }
The text was updated successfully, but these errors were encountered:
No branches or pull requests
The text was updated successfully, but these errors were encountered: