Advertisement
mosredna

AoC 2024 day 6

Dec 6th, 2024 (edited)
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 2.97 KB | Source Code | 0 0
  1. // Advent of Code 2024 - Day 6
  2. const fs = require("fs")
  3.  
  4. const input = fs.readFileSync(__dirname + (process.argv.includes("--test") ? "/test.txt" : "/input.txt"), "utf8")
  5.  
  6. const parseInput = (rawInput) => {
  7.     return rawInput.split(/\r?\n/).map((r) => r.split(""))
  8. }
  9. const directions = [
  10.     [-1, 0], // up
  11.     [0, 1], // right
  12.     [1, 0], // down
  13.     [0, -1] // left
  14. ]
  15. function findStartPosition(map) {
  16.     for (let i = 0; i < map.length; i++) {
  17.         const col = map[i].indexOf("^")
  18.         if (col !== -1) return [i, col] // Return the position as soon as we find '^'
  19.     }
  20. }
  21. const visitedPositions = new Set()
  22.  
  23. const part1 = (map) => {
  24.     let guardPos = [...guardStartPos]
  25.     let directionIndex = 0
  26.  
  27.     const rows = map.length
  28.     const cols = map[0].length
  29.    
  30.     while (true) {
  31.         const [dx, dy] = directions[directionIndex]
  32.         const [x, y] = guardPos
  33.         const nextPos = [x + dx, y + dy]
  34.  
  35.         if (nextPos[0] < 0 || nextPos[0] >= rows || nextPos[1] < 0 || nextPos[1] >= cols) {
  36.             break
  37.         }
  38.  
  39.         if (map[nextPos[0]][nextPos[1]] !== "#") {
  40.             visitedPositions.add(nextPos.toString())
  41.             guardPos = nextPos
  42.         } else {
  43.             directionIndex = (directionIndex + 1) % directions.length
  44.         }
  45.     }
  46.  
  47.     return visitedPositions.size
  48. }
  49.  
  50. const part2 = (map) => {
  51.     const rows = map.length
  52.     const cols = map[0].length
  53.  
  54.     const simulateMovement = (modifiedMap) => {
  55.         const visitedStates = new Set()
  56.         let guardPos = [...guardStartPos]
  57.         let directionIndex = 0
  58.  
  59.         while (true) {
  60.             const [dx, dy] = directions[directionIndex]
  61.             const [x, y] = guardPos
  62.             const nextPos = [x + dx, y + dy]
  63.  
  64.             if (nextPos[0] < 0 || nextPos[0] >= rows || nextPos[1] < 0 || nextPos[1] >= cols) {
  65.                 return false
  66.             }
  67.  
  68.             if (modifiedMap[nextPos[0]][nextPos[1]] !== "#") {
  69.                 const stateKey = `${guardPos}-${directionIndex}`
  70.                 if (visitedStates.has(stateKey)) {
  71.                     return true
  72.                 }
  73.                 visitedStates.add(stateKey)
  74.                 guardPos = nextPos
  75.             } else {
  76.                 directionIndex = (directionIndex + 1) % directions.length
  77.             }
  78.         }
  79.     }
  80.  
  81.     let loopCount = 0
  82.     visitedPositions.delete(guardStartPos.toString())
  83.     for (const pos of visitedPositions) {
  84.         const [row, col] = pos.split(",").map(Number)
  85.  
  86.         const modifiedMap = map.map((row) => [...row])
  87.         modifiedMap[row][col] = "#"
  88.  
  89.         if (simulateMovement(modifiedMap)) {
  90.             loopCount++
  91.         }
  92.     }
  93.  
  94.     return loopCount
  95. }
  96.  
  97. const data = parseInput(input)
  98. let guardStartPos = findStartPosition(data)
  99. console.time("Part 1 Time")
  100. console.log("Part 1:", part1(data))
  101. console.timeEnd("Part 1 Time")
  102.  
  103. console.time("Part 2 Time")
  104. console.log("Part 2:", part2(data))
  105. console.timeEnd("Part 2 Time")
  106.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement