my solutions to advent of code
aoc advent-of-code
at main 1.4 kB view raw
1type Direction = "^" | "v" | "<" | ">"; 2interface Location { 3 x: number; 4 y: number; 5} 6type World = Map<string, number>; 7type Turn = "Santa" | "Robo"; 8 9function giveGift(world: World, location: Location) { 10 const locationString = JSON.stringify(location); 11 return world.set(locationString, (world.get(locationString) ?? 0) + 1); 12} 13 14function moveTo(location: Location, direction: Direction) { 15 if (direction === "^") { 16 location.y -= 1; 17 } else if (direction === "v") { 18 location.y += 1; 19 } else if (direction === "<") { 20 location.x -= 1; 21 } else if (direction === ">") { 22 location.x += 1; 23 } 24 return location; 25} 26 27const input = (await Bun.file("../input.txt").text()) 28 .trim() 29 .split("") as Direction[]; 30 31// the gleam one is so much better... 32// part 1 33{ 34 const location: Location = { x: 0, y: 0 }; 35 const world: World = new Map(); 36 input.forEach((direction) => { 37 giveGift(world, location); 38 moveTo(location, direction); 39 }); 40 console.log(world.size); 41} 42 43// part 2 44{ 45 const santa_location: Location = { x: 0, y: 0 }; 46 const robo_location: Location = { x: 0, y: 0 }; 47 const world: World = new Map(); 48 let turn: Turn = "Santa"; 49 const location = () => (turn === "Santa" ? santa_location : robo_location); 50 input.forEach((direction) => { 51 giveGift(world, location()); 52 moveTo(location(), direction); 53 turn = turn === "Santa" ? "Robo" : "Santa"; 54 }); 55 console.log(world.size); 56}