day 6 part 1 is straightforward... part 2... hmmm
This commit is contained in:
		
							
								
								
									
										75
									
								
								06/index.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										75
									
								
								06/index.ts
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,75 @@
 | 
				
			|||||||
 | 
					import fs from "node:fs";
 | 
				
			||||||
 | 
					import { printTime, now } from "../util";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type tile = { guard: boolean; blocked: boolean; visited: boolean };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const test = true;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const data = fs.readFileSync(
 | 
				
			||||||
 | 
					  test ? "./inputs/testinput" : "./inputs/input",
 | 
				
			||||||
 | 
					  "utf8"
 | 
				
			||||||
 | 
					);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					let timer = now.instant();
 | 
				
			||||||
 | 
					console.log(
 | 
				
			||||||
 | 
					  "part one:",
 | 
				
			||||||
 | 
					  (() => {
 | 
				
			||||||
 | 
					    let floor = data
 | 
				
			||||||
 | 
					      .split("\n")
 | 
				
			||||||
 | 
					      .slice(0, -1)
 | 
				
			||||||
 | 
					      .reduce((floor, row, i) => {
 | 
				
			||||||
 | 
					        row.split("").forEach((v, col) => {
 | 
				
			||||||
 | 
					          floor[`${i}|${col}`] = {
 | 
				
			||||||
 | 
					            guard: v === "^",
 | 
				
			||||||
 | 
					            blocked: v === "#",
 | 
				
			||||||
 | 
					            visited: false,
 | 
				
			||||||
 | 
					          };
 | 
				
			||||||
 | 
					        });
 | 
				
			||||||
 | 
					        return floor;
 | 
				
			||||||
 | 
					      }, {} as Record<string, tile>);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    let direction = "north";
 | 
				
			||||||
 | 
					    let [guard] = Object.entries(floor).filter((tile) => tile[1].guard)[0];
 | 
				
			||||||
 | 
					    while (true) {
 | 
				
			||||||
 | 
					      let [x, y] = guard.split("|").map((v) => parseInt(v, 10));
 | 
				
			||||||
 | 
					      floor[guard].visited = true;
 | 
				
			||||||
 | 
					      let next = "";
 | 
				
			||||||
 | 
					      switch (direction) {
 | 
				
			||||||
 | 
					        case "north":
 | 
				
			||||||
 | 
					          next = `${x - 1}|${y}`;
 | 
				
			||||||
 | 
					          break;
 | 
				
			||||||
 | 
					        case "south":
 | 
				
			||||||
 | 
					          next = `${x + 1}|${y}`;
 | 
				
			||||||
 | 
					          break;
 | 
				
			||||||
 | 
					        case "east":
 | 
				
			||||||
 | 
					          next = `${x}|${y + 1}`;
 | 
				
			||||||
 | 
					          break;
 | 
				
			||||||
 | 
					        case "west":
 | 
				
			||||||
 | 
					          next = `${x}|${y - 1}`;
 | 
				
			||||||
 | 
					          break;
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					      if (floor[next] === undefined) {
 | 
				
			||||||
 | 
					        break;
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					      if (!floor[next].blocked) {
 | 
				
			||||||
 | 
					        floor[next].guard = true;
 | 
				
			||||||
 | 
					        floor[guard].guard = false;
 | 
				
			||||||
 | 
					        guard = next;
 | 
				
			||||||
 | 
					      } else {
 | 
				
			||||||
 | 
					        if (direction === "north") {
 | 
				
			||||||
 | 
					          direction = "east";
 | 
				
			||||||
 | 
					        } else if (direction === "east") {
 | 
				
			||||||
 | 
					          direction = "south";
 | 
				
			||||||
 | 
					        } else if (direction === "south") {
 | 
				
			||||||
 | 
					          direction = "west";
 | 
				
			||||||
 | 
					        } else if (direction === "west") {
 | 
				
			||||||
 | 
					          direction = "north";
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    return Object.entries(floor).reduce((total, tile) => {
 | 
				
			||||||
 | 
					      return total + (tile[1].visited ? 1 : 0);
 | 
				
			||||||
 | 
					    }, 0);
 | 
				
			||||||
 | 
					  })(),
 | 
				
			||||||
 | 
					  printTime(now.instant().since(timer))
 | 
				
			||||||
 | 
					);
 | 
				
			||||||
		Reference in New Issue
	
	Block a user