Skip to content
New issue

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

[Potential bug] Strange that it does not work on JS #42

Open
nuoxoxo opened this issue Dec 17, 2023 · 0 comments
Open

[Potential bug] Strange that it does not work on JS #42

nuoxoxo opened this issue Dec 17, 2023 · 0 comments
Labels
bug Something isn't working

Comments

@nuoxoxo
Copy link
Owner

nuoxoxo commented Dec 17, 2023

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
}
@nuoxoxo nuoxoxo changed the title [Potential bug] Strange that it does not work [Potential bug] Strange that it does not work on JS Dec 18, 2023
@nuoxoxo nuoxoxo added the bug Something isn't working label Dec 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant