Skip to content

Commit a6d1405

Browse files
committed
2024 day 6
1 parent ba9073c commit a6d1405

File tree

2 files changed

+168
-0
lines changed

2 files changed

+168
-0
lines changed

2024/input/6.txt

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
..................#................................................................#........#.....................................
2+
...#...........#...................................................#........................................#.................#...
3+
...................................#................#.#...............#.................................................#.........
4+
.....#......#................#.....................................................................#..........#........#......#...
5+
............................................................................................#.....................#...............
6+
.....................#..##...#........................#.................#.......................#..#.........#.......#...#......#.
7+
............##....................................##..................#...............................#....#......................
8+
.............#............#.#..#.......#...........#..............#...............#.....#.........................................
9+
....................#..........##.........#.........#................#............................................................
10+
....#.......#................................................................#...#.#..........................................#.#.
11+
..#..............#.....................................#..........#.#....#..............#..................#.#............#.......
12+
.#.......#.........................#........................#..#.....#............................................................
13+
........#...#......................#...........#......................................#....................#......................
14+
.............................#..............#........##....#....................#......#....................#............#......#.
15+
..............#..............#............#.......................##....#..........................................#.........#....
16+
.............#............#..........#..........#...#.....................................................#.......................
17+
..#........#.....#..........................................#................................................#...#.#...#..........
18+
........#.....................#...#..#......#.........................................................#.....##.........#..........
19+
..............................................................................................#...#...............................
20+
..#.......................#..........................#......#...................................................................#.
21+
.......#.........................#..............#.........#.............#.......................................#...#.............
22+
..................#.................#.......................#....................#....................................#...........
23+
...#...#.......#......##.#...............#....#..............#..........................................#....#..#.................
24+
#................................#.......#.....#.......#.............#..................#........#................................
25+
...........#..................................#.......................................................................#.#..#......
26+
.......#........#.........................................#..........................#...........................#.....#..........
27+
................#..#.....................#..#...........................#...........................................#.............
28+
#...#.......................#................................................#................#................................#..
29+
..........#...........................#.......#............................................................................#.....#
30+
..................................#............................................................#..................................
31+
......................#........#.............#....#.....#.......#..........................#..........................#...........
32+
...........................##.................#.............#........................#...#..................#.....................
33+
.............................................................................................................#....#..........#....
34+
................#...................................#...........#..........#....................#............................#....
35+
........#....#................................................#................##..#..................................#...........
36+
...........#....................................................#...#.#......#....................................................
37+
.......................#.........................................................^....................................#...........
38+
..........................#.............##..#........#.#....#.......#....................#...........#.........#.....#............
39+
........#...........................#..#..........................................................#...............................
40+
....#....................................#....#.........................................#.........#...............................
41+
...............#...................................#.....#.................................#....................................#.
42+
......#...............................................................#.............................#........................#....
43+
.........................#.....#.........................#.#...............#.........#.....#..................#..#.........#......
44+
.........................................................#......#........##.......................#...#......#................#...
45+
.......................................#.....#.................................#..................................................
46+
..................#..................................#..........................................................................#.
47+
...................#....#.........#.......#................#....................#.........................#......#.....#..#.......
48+
................................#..............................................................................#....#.............
49+
.....................#.#.............#.................#..........#.......#.........................................#.............
50+
......#....................#....................................................................#...#.............................
51+
........#........#...........#.##.............#........................#............#..............#.........#....#........#......
52+
........................#.................................................................#.......................................
53+
............................................................#.....#....................#............#.....#....#............#.....
54+
...........#............#....#........................................................##............#..............#..............
55+
...........#...........#.....#..............#..............##....#........#......#................................................
56+
..#................................................................................#......................#......#................
57+
....................#.........#......................#.............................#.......#.......................#.............#
58+
..#...#....#......................................................................................................#...............
59+
...........#................................................................................#...............................#.....
60+
....................................#............#..........#.........................#..................................#.......#
61+
...................#......#...................#............#......#........#................#.............#...........#.........#.
62+
........................#..................................#........#..................#..........................................
63+
............#..#...........#..................##....................#...........................................#.................
64+
.......................................................................................#.....#............#.......................
65+
...............................................#....#................................#..#..........................#..#...........
66+
..............#..#.....#.........#....................#.......#..#..............#...............................#.....#...........
67+
..........#............................#..........................................................................................
68+
........................................#........#........................................................#.#.....................
69+
#................#......................#..................................................................................#......
70+
...............................................................................#................................#.................
71+
...................................................................................................................#.#........#...
72+
#........................................................................#......................................#.#...............
73+
...#......#............#...................#.............#........#..........................................#......#.............
74+
..............##....#...................................................#......................#..................................
75+
...............#........................................................................#...........#...#.#.......................
76+
..........#.#.....#................................#...........#........#.......#.................................................
77+
#....................#......................................................................................#.....................
78+
..............#...##...........................................#.............#.......#..................#..............#..........
79+
.#.............................................#..............................................................................#...
80+
...#................................#............................................#............................#...................
81+
..............................#......................................................#..........................................#.
82+
...................#.....................................#..................................#......#..............................
83+
....#.#..........##.............#.......#........................##..........#.................#..................................
84+
#..#................#........##....................................................................................#..............
85+
........#...#.........................................................................#...........................................
86+
......................................................#....#...................#..#.................................#.............
87+
........#...............#........#..............................................#............................#....................
88+
...#..............................#.........................................##.......................................#....#.......
89+
...#................#..............#.......#......................................................................................
90+
........#..................#.............................................................#...........................#....#.......
91+
......................#.#..........#.......#..................................#..#...#............#...............#...............
92+
.....#..................#....#..#......#.......................#.....................................................#............
93+
.....#................................#.........................#..................#.#..........#...#............#.....#..........
94+
....#...................#....#.#.........#........#..................................................................#....#..#....
95+
........#...............#......................................#........#..............................#..........................
96+
....#...#.#.....#..............#..................#..........#...#..#......................#...#..........#......................#
97+
...........#...................................................................................................#................#.
98+
.................................##..............#.............................#...............................#..................
99+
...........#.................#.............##...................................#.......................#.................##......
100+
.....#..................#......#........#.................................................#..#........................#..#........
101+
......#.......#......................................................................#.#.........#....#...........................
102+
..................#.#........................#......#................................................................#......#.....
103+
.....................#............#...................#.....#....................................#.......#..........#.......#.....
104+
#.............................#...............................#...#...............................................................
105+
..##.........#........#......................................#.....................#...#.............#............#..#............
106+
..........#.....................#..#....................#...#....................#....................#.....................#.....
107+
...........#..........#...............#..#..........#..............................................#.#............................
108+
............#...#...#.............#.........#.............................#...............#.......................................
109+
.........#...........#...............................................................#............................................
110+
.#..#.................................................................#............#.#.......................##...................
111+
........##............#...................#................................#......................................................
112+
............................................#..........................#..........................................................
113+
#...................#...........#..............#...#............#.#........#.................#......#.............................
114+
........#....................................#....................................................................................
115+
...........#.........#..#...............#...........#.....##.#......#....##............#....#....#.#.........##.........#.........
116+
..#......................#..........#.................#.................#......#.............................#........#...........
117+
....................#.................#....#..............................#....#.............#....#....#.....................#....
118+
............#.................#.................................#...............................#..#...........................#..
119+
...................................................#.....#..............#.......#....................#.#..........................
120+
..#.........##..#........#...................................................................................#............#.......
121+
.....#..............................#......................................................................#........#.............
122+
................................#........##.......#...............#..............#.......#...#.#........#.........................
123+
..............................#.....................#.........................................#..................................#
124+
...........#...................##.................#.................................#...............................#........#....
125+
..............................#...##......#..................#...#................................#......#.......#...............#
126+
.....................##......#......#.#.......#..............#......#.................#.........#...................#.............
127+
..........................#................................#...........#..........................................................
128+
...#.....#....................#.....................#...#.....#.............................#.#....#.....#.#.................#....
129+
............................#...#........#......................................................................#.....#...........
130+
............#..........##..................#.............................................................#.....#..#...............

2024/js/day6.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { asLines } from './util.js';
2+
3+
const map = asLines('../input/6.txt').map(l => l.split(""));
4+
5+
const dirs = { '^': [0, -1, '>'], 'v': [0, 1, '<'], '<': [-1, 0, '^'], '>': [1, 0, 'v']};
6+
const findPosAndDir = (map) => map.flatMap((r, y) => r.flatMap((c, x) => dirs[c] ? [x, y, c] : null).filter(p => p !== null));
7+
const outOfBounds = (map, x, y) => x < 0 || x > map[0].length - 1 || y < 0 || y > map.length - 1;
8+
9+
const patrol = (map, x, y, dir) => {
10+
const route = [];
11+
while (true) {
12+
const posAndDir = `${x},${y} ${dir}`;
13+
if (route.includes(posAndDir)) return null;
14+
route.push(posAndDir);
15+
const [dx, dy, rotate] = dirs[dir];
16+
const [nx, ny] = [x + dx, y + dy];
17+
if (outOfBounds(map, nx, ny)) return route;
18+
if (map[ny][nx] == '#') dir = rotate;
19+
else [x, y] = [nx, ny];
20+
}
21+
}
22+
23+
const [startx, starty, startdir] = findPosAndDir(map);
24+
const route = patrol(map, startx, starty, startdir);
25+
const positions = route.map(posAndDir => posAndDir.split(' ')[0]);
26+
const uniqPositions = [...new Set(positions)];
27+
console.log(`day6a: ${uniqPositions.length}`);
28+
29+
// try adding obstacle to visited position on route and check for loop
30+
const loop = uniqPositions.filter(pos => {
31+
const [x, y] = pos.split(',').map(Number);
32+
if (x == startx && y == starty) return false; // not allowed to add obstacle to start
33+
const nmap = JSON.parse(JSON.stringify(map)); // deep clone
34+
nmap[y][x] = '#'; // add new obstacle
35+
return patrol(nmap, startx, starty, startdir) == null; //
36+
});
37+
const uniqLoop = new Set(loop);
38+
console.log(`day6b: ${uniqLoop.size}`);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy