-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
99c633d
commit e36a501
Showing
5 changed files
with
136 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
type Map = string[][]; | ||
type Position = [x: number, y: number]; | ||
type Move = "<" | "^" | ">" | "v"; | ||
|
||
const moveMap = { | ||
"<": [-1, 0], | ||
"^": [0, -1], | ||
">": [1, 0], | ||
"v": [0, 1], | ||
}; | ||
|
||
function getNextPosition(current: Position, move: Move): Position { | ||
const [dx, dy] = moveMap[move]; | ||
return [current[0] + dx, current[1] + dy]; | ||
} | ||
|
||
export async function main(target = "input") { | ||
const dirpath = new URL(".", import.meta.url).pathname; | ||
const text = await Deno.readTextFile(`${dirpath}${target}.txt`); | ||
|
||
const { map, moves, start } = text.split("\n").reduce((agg, line, idx) => { | ||
if (line.match(/^[#.O@]+$/)) { | ||
const row = line.split(""); | ||
const start = row.findIndex((cell) => cell === "@"); | ||
if (start !== -1) { | ||
agg.start = [start, idx]; | ||
row[start] = "."; | ||
} | ||
agg.map.push(row); | ||
} else if (line.match(/^[<^>v]+$/)) { | ||
agg.moves.push(...(line.split("") as Move[])); | ||
} | ||
|
||
return agg; | ||
}, { map: [] as Map, moves: [] as Move[], start: [0, 0] as Position }); | ||
|
||
let current = start; | ||
moves.forEach((move) => { | ||
const next = getNextPosition(current, move); | ||
|
||
switch (map[next[1]]?.[next[0]]) { | ||
case "#": | ||
return; | ||
case ".": | ||
current = next; | ||
return; | ||
case "O": { | ||
let nextInDirection = getNextPosition(next, move); | ||
while (map[nextInDirection[1]]?.[nextInDirection[0]]) { | ||
if (map[nextInDirection[1]][nextInDirection[0]] === "#") { | ||
break; | ||
} | ||
if (map[nextInDirection[1]][nextInDirection[0]] === "O") { | ||
nextInDirection = getNextPosition(nextInDirection, move); | ||
continue; | ||
} | ||
if (map[nextInDirection[1]][nextInDirection[0]] === ".") { | ||
map[nextInDirection[1]][nextInDirection[0]] = "O"; | ||
map[next[1]][next[0]] = "."; | ||
current = next; | ||
return; | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
|
||
const gpsSum = map.reduce((agg, row, y) => { | ||
return agg + row.reduce((agg2, cell, x) => { | ||
if (cell === "O") { | ||
agg2 += y * 100 + x; | ||
} | ||
return agg2; | ||
}, 0); | ||
}, 0); | ||
|
||
return { gpsSum }; | ||
} | ||
|
||
if (import.meta.main) { | ||
const startTime = performance.now(); | ||
console.log(await main()); | ||
console.log(`Elapsed: ${Math.round(performance.now() - startTime)}ms`); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
######## | ||
#..O.O.# | ||
##@.O..# | ||
#...O..# | ||
#.#.O..# | ||
#...O..# | ||
#......# | ||
######## | ||
|
||
<^^>>>vv<v>>v<< |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
########## | ||
#..O..O.O# | ||
#......O.# | ||
#.OO..O.O# | ||
#[email protected].# | ||
#O#..O...# | ||
#O..O..O.# | ||
#.OO.O.OO# | ||
#....O...# | ||
########## | ||
|
||
<vv>^<v^>v>^vv^v>v<>v^v<v<^vv<<<^><<><>>v<vvv<>^v^>^<<<><<v<<<v^vv^v>^ | ||
vvv<<^>^v^^><<>>><>^<<><^vv^^<>vvv<>><^^v>^>vv<>v<<<<v<^v>^<^^>>>^<v<v | ||
><>vv>v^v^<>><>>>><^^>vv>v<^^^>>v^v^<^^>v^^>v^<^v>v<>>v^v^<v>v^^<^^vv< | ||
<<v<^>>^^^^>>>v^<>vvv^><v<<<>^^^vv^<vvv>^>v<^^^^v<>^>vvvv><>>v^<<^^^^^ | ||
^><^><>>><>^^<<^^v>>><^<v>^<vv>>v>>>^v><>^v><<<<v>>v<v<v>vvv>^<><<>^>< | ||
^>><>^v<><^vvv<^^<><v<<<<<><^v<<<><<<^^<v<^^^><^>>^<v^><<<^>>^v<v^v<v^ | ||
>^>>^v>vv>^<<^v<>><<><<v<<v><>v<^vv<<<>^^v^>^^>>><<^v>>v^v><^^>>^<>vv^ | ||
<><^^>^^^<><vvvvv^v<v<<>^v<v>v<<^><<><<><<<^^<<<^<<>><<><^^^>^^<>^>v<> | ||
^^>vv<^v^v<vv>^<><v<^v>^^^>>>^^vvv^>vvv<>>>^<^>>>>>^<<^v>^vvv<>^<><<v> | ||
v^^>>><<^^<>>^v^<v^vv<>v^<<>^<^v^v><^<<<><<^<v><v<>vv>>v><v^<vv<>v^<<^ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { assertEquals } from "@std/assert"; | ||
import { main } from "./main.ts"; | ||
|
||
const target = "sample"; | ||
|
||
Deno.test(`correct GPS sum for ${target}`, async () => { | ||
const result = await main(target); | ||
assertEquals(result.gpsSum, 2028); | ||
}); | ||
|
||
Deno.test(`correct GPS sum for sample2`, async () => { | ||
const result = await main("sample2"); | ||
assertEquals(result.gpsSum, 10092); | ||
}); | ||
|
||
// Deno.test(`correct placeholder2 for ${target}`, async () => { | ||
// const result = await main(target); | ||
// assertEquals(result.gpsSum, 0); | ||
// }); |