|
1 | | -export function part1(input) { |
| 1 | +const step = { |
| 2 | + up: { x: 0, y: -1, turn: "right" }, |
| 3 | + down: { x: 0, y: 1, turn: "left" }, |
| 4 | + left: { x: -1, y: 0, turn: "up" }, |
| 5 | + right: { x: 1, y: 0, turn: "down" }, |
| 6 | +}; |
| 7 | + |
| 8 | +function test(input, add = undefined) { |
2 | 9 | const map = input.split("\n").map(line => line.split("")); |
3 | 10 | const startY = map.findIndex(line => line.includes("^")); |
4 | 11 | const startX = map[startY].indexOf("^"); |
5 | 12 | let current = { x: startX, y: startY, direction: "up" }; |
6 | 13 | const visited = new Set(); |
| 14 | + const turns = new Set(); |
| 15 | + if (add && map[add.y]?.[add.x] === ".") map[add.y][add.x] = "#"; |
| 16 | + map[startY][startX] = "."; |
7 | 17 | while (map[current.y]?.[current.x] !== undefined) { |
8 | 18 | let next = { ...current }; |
9 | | - visited.add(`${current.x},${current.y}`); |
10 | | - if (current.direction === "up") { |
11 | | - next.y -= 1; |
12 | | - } else if (current.direction === "down") { |
13 | | - next.y += 1; |
14 | | - } else if (current.direction === "left") { |
15 | | - next.x -= 1; |
16 | | - } else if (current.direction === "right") { |
17 | | - next.x += 1; |
| 19 | + while (map[next.y]?.[next.x] === ".") { |
| 20 | + if (!add) visited.add(`${next.x},${next.y}`); |
| 21 | + next.x += step[current.direction].x; |
| 22 | + next.y += step[current.direction].y; |
18 | 23 | } |
19 | 24 | if (map[next.y]?.[next.x] === "#") { |
20 | | - next = { ...current }; |
21 | | - if (current.direction === "up") next.direction = "right"; |
22 | | - else if (current.direction === "down") next.direction = "left"; |
23 | | - else if (current.direction === "left") next.direction = "up"; |
24 | | - else if (current.direction === "right") next.direction = "down"; |
| 25 | + next.x -= step[current.direction].x; |
| 26 | + next.y -= step[current.direction].y; |
| 27 | + next.direction = step[current.direction].turn; |
| 28 | + if (turns.has(`${next.x},${next.y},${next.direction}`)) return turns; |
| 29 | + turns.add(`${next.x},${next.y},${next.direction}`); |
25 | 30 | } |
26 | 31 | current = next; |
27 | 32 | } |
28 | | - return visited.size; |
| 33 | + return add ? undefined : visited; |
29 | 34 | } |
30 | 35 |
|
31 | | -function test(input) { |
32 | | - const map = input.split("\n").map(line => line.split("")); |
33 | | - const startY = map.findIndex(line => line.includes("^")); |
34 | | - const startX = map[startY].indexOf("^"); |
35 | | - let current = { x: startX, y: startY, direction: "up" }; |
36 | | - const visited = new Set(); |
37 | | - while (map[current.y]?.[current.x] !== undefined) { |
38 | | - let next = { ...current }; |
39 | | - if (visited.has(`${current.x},${current.y},${current.direction}`)) { |
40 | | - return true; |
41 | | - } |
42 | | - visited.add(`${current.x},${current.y},${current.direction}`); |
43 | | - if (current.direction === "up") { |
44 | | - next.y -= 1; |
45 | | - } else if (current.direction === "down") { |
46 | | - next.y += 1; |
47 | | - } else if (current.direction === "left") { |
48 | | - next.x -= 1; |
49 | | - } else if (current.direction === "right") { |
50 | | - next.x += 1; |
51 | | - } |
52 | | - if (map[next.y]?.[next.x] === "#") { |
53 | | - next = { ...current }; |
54 | | - if (current.direction === "up") next.direction = "right"; |
55 | | - else if (current.direction === "down") next.direction = "left"; |
56 | | - else if (current.direction === "left") next.direction = "up"; |
57 | | - else if (current.direction === "right") next.direction = "down"; |
58 | | - } |
59 | | - current = next; |
60 | | - } |
61 | | - return false; |
| 36 | +export function part1(input) { |
| 37 | + return test(input).size; |
62 | 38 | } |
63 | 39 |
|
64 | 40 | export function part2(input) { |
65 | 41 | let count = 0; |
66 | | - for (let i = 0; i < input.length; i++) { |
67 | | - if ( |
68 | | - input[i] === "." && |
69 | | - test(`${input.slice(0, i)}#${input.slice(i + 1)}`) |
70 | | - ) { |
71 | | - count++; |
72 | | - } |
73 | | - } |
| 42 | + test(input).forEach(pos => { |
| 43 | + const [x, y] = pos.split(",").map(Number); |
| 44 | + if (test(input, { x, y })) count++; |
| 45 | + }); |
74 | 46 | return count; |
75 | 47 | } |
0 commit comments