Advertisement
Guest User

Untitled

a guest
Dec 16th, 2023
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { readFileSync } from 'fs'
  2.  
  3. let input = `.|...\\....
  4. |.-.\\.....
  5. .....|-...
  6. ........|.
  7. ..........
  8. .........\\
  9. ..../.\\\\..
  10. .-.-/..|..
  11. .|....-|.\\
  12. ..//.|....`
  13.  
  14. function parse(input: string): string[][] {
  15.     const lines = input.trim().split('\n')
  16.     return Array.from({ length: lines.length }, (_, y) =>
  17.         Array.from({ length: lines[0].length }, (_, x) => lines[y][x]),
  18.     )
  19. }
  20.  
  21. function isOutOfBounds({ x, y }: State, grid: string[][]): boolean {
  22.     return x < 0 || y < 0 || y >= grid.length || x >= grid[0].length
  23. }
  24.  
  25. type Direction = 'up' | 'down' | 'left' | 'right'
  26.  
  27. type State = {
  28.     x: number
  29.     y: number
  30.     direction: Direction
  31.     seen: Set<string>
  32. }
  33.  
  34. function move(state: State): State {
  35.     const { x, y, direction } = state
  36.     switch (direction) {
  37.         case 'up':
  38.             return { ...state, y: y - 1 }
  39.         case 'down':
  40.             return { ...state, y: y + 1 }
  41.         case 'left':
  42.             return { ...state, x: x - 1 }
  43.         case 'right':
  44.             return { ...state, x: x + 1 }
  45.     }
  46. }
  47.  
  48. function solve(input: string) {
  49.     const grid = parse(input)
  50.     const state: State = {
  51.         x: 0,
  52.         y: 0,
  53.         direction: 'right',
  54.         // A path history of (x,y,direction) so we can detect when beam is stuck in cycle
  55.         seen: new Set(),
  56.     }
  57.     const energized = new Set<string>()
  58.  
  59.     function step(state: State) {
  60.         if (isOutOfBounds(state, grid)) return
  61.  
  62.         const { x, y, direction, seen } = state
  63.  
  64.         //Check for path cycle
  65.         const key = `${x},${y},${direction}`
  66.         if (seen.has(key)) return
  67.         seen.add(key)
  68.  
  69.         energized.add(`${x},${y}`)
  70.  
  71.         // render(grid, energized)
  72.         switch (grid[y][x]) {
  73.             case '.':
  74.                 step(move(state))
  75.                 break
  76.             case '|':
  77.                 switch (direction) {
  78.                     case 'up':
  79.                     case 'down':
  80.                         step(move(state))
  81.                         break
  82.                     case 'left':
  83.                     case 'right':
  84.                         step(move({ ...state, direction: 'up' }))
  85.                         step(move({ ...state, direction: 'down' }))
  86.                         break
  87.                 }
  88.                 break
  89.             case '-':
  90.                 switch (direction) {
  91.                     case 'left':
  92.                     case 'right':
  93.                         step(move(state))
  94.                         break
  95.                     case 'up':
  96.                     case 'down':
  97.                         step(move({ ...state, direction: 'left' }))
  98.                         step(move({ ...state, direction: 'right' }))
  99.                         break
  100.                 }
  101.                 break
  102.             case '/':
  103.                 switch (direction) {
  104.                     case 'up':
  105.                         step(move({ ...state, direction: 'right' }))
  106.                         break
  107.                     case 'down':
  108.                         step(move({ ...state, direction: 'left' }))
  109.                         break
  110.                     case 'left':
  111.                         step(move({ ...state, direction: 'down' }))
  112.                         break
  113.                     case 'right':
  114.                         step(move({ ...state, direction: 'up' }))
  115.                         break
  116.                 }
  117.                 break
  118.             case '\\':
  119.                 switch (direction) {
  120.                     case 'up':
  121.                         step(move({ ...state, direction: 'left' }))
  122.                         break
  123.                     case 'down':
  124.                         step(move({ ...state, direction: 'right' }))
  125.                         break
  126.                     case 'left':
  127.                         step(move({ ...state, direction: 'up' }))
  128.                         break
  129.                     case 'right':
  130.                         step(move({ ...state, direction: 'down' }))
  131.                         break
  132.                 }
  133.                 break
  134.         }
  135.     }
  136.  
  137.     step(state)
  138.     render(grid, energized)
  139.     console.log(energized.size)
  140. }
  141.  
  142. function render(grid: string[][], energized: Set<string>) {
  143.     for (let y = 0; y < grid.length; y++) {
  144.         let row = ''
  145.         for (let x = 0; x < grid[0].length; x++) {
  146.             row += energized.has(`${x},${y}`) ? '#' : grid[y][x]
  147.         }
  148.         console.log(row)
  149.     }
  150. }
  151.  
  152. input = readFileSync('day16.txt', { encoding: 'utf8' })
  153. solve(input)
  154.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement