Skip to content

Commit 992ff2c

Browse files
committed
feat(day20): solution 1
NOTE: the maze is solved from end to start
1 parent ebecb70 commit 992ff2c

File tree

5 files changed

+141
-8
lines changed

5 files changed

+141
-8
lines changed

day20/day20.solution.spec.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import {
2+
transportingMazeSolver,
3+
drawSolution,
4+
Location,
5+
} from './transportingMazeSolver'
6+
import * as fs from 'fs'
7+
import * as path from 'path'
8+
9+
describe('Day 20: Part 1', () => {
10+
it('should solve the puzzle', () => {
11+
const input = fs.readFileSync(
12+
path.resolve(process.cwd(), 'day20/input.txt'),
13+
'utf-8',
14+
)
15+
const res = transportingMazeSolver(input, { start: 'ZZ', end: 'AA' })
16+
expect(res?.path).toHaveLength(400)
17+
drawSolution(input, res as Location)
18+
})
19+
})

day20/input.txt

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
H I J D A U U W
2+
R Z M S A N V T
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+
#.#.#.#.......#.#.....#.# Y Z L U D H G #...#...#...#.....#...#.#
29+
#.###.#####.#.#.#.#####.# A H U O H S U #.###.#####.###.#####.#.#
30+
#...#.#.#...#.#.......#.# #.#.....#.....#...#.#.#.#
31+
###.#.#.###.#####.#.#.#.# #.#.#####.###.#.###.#.#.#
32+
#.......#.....#.#.#.#.#.# MZ........#.#.#.#.........#
33+
###.#.#####.#.#.#.#####.# #.#########.#.#.###.###.#
34+
#...#.#...#.#.#...#.#...# #.#.#.#.........#...#...#
35+
#.#####.###.#####.#.#.### #.#.#.#######.#.###.###.#
36+
ZH..................#...#.# #.#.....#.#.#.#.#...#....QV
37+
#.###.#####.#.###.#.#.#.# #.#.#.###.#.#.#########.#
38+
#...#...#...#.#.....#...# #...#.........#.#.#...#.#
39+
#####.#####.#####.#.###.# ###############.#.#.#####
40+
#...#.#.....#.#.#.#.#....WT QV....#.....#...#.......#.#
41+
#.###########.#.###.#.#.# ###.#.#.###.#.#.#.#.###.#
42+
#...#.....#.#.#.#.#.#.#.# #.....#.....#...#.#.#...#
43+
#.#.###.###.#.#.#.####### #############.#####.#.#.#
44+
UO..#......................BT #.#.#.#.#...#.#...#...#..LY
45+
######################### #.#.#.#.#.#######.#######
46+
#...................#...# #.....#.............#.#.#
47+
#.###.#.#####.#####.#.#.# ###.###.###.#######.#.#.#
48+
HO..#.#.#...#...#.....#.#.# #...#.....#...#.........#
49+
#.#.#####.#####.#####.#.# ###.###.#####.###.#####.#
50+
#.....#.#...#.....#...#..LY PY..#.........#.#.....#...#
51+
#####.#.#########.#.###.# #.#.###.#.###########.###
52+
MM......#.....#.........#..IZ #...#...#.....#...#......HS
53+
###########.###.#######.# ###########.#####.#######
54+
#.........#...#.#.....#.# QW........#.#.#...#...#....MZ
55+
#.#######.#.###.#.#.##### #.###.###.#####.###.###.#
56+
#.#.........#.#.#.#.....# #...#.#.......#.#...#...#
57+
#.#.###.#.###.#.###.##### #####.#.#.#####.###.###.#
58+
#.#.#...#...#.#.#........HR #.....#.#...#...#...#.#.#
59+
#.#####.#####.#####.###.# #.#####.#.#####.###.#.#.#
60+
SH....#.................#.# #.......#...............#
61+
###.#####.#####.######### ###########.###.#####.###
62+
#.#.#.#...#...#.#.#.....# #...#.....#.#.#.#...#.#.#
63+
#.###.#######.###.#.###.# #.#.###.#.###.###.#####.#
64+
#.#...#...#...#.....#.#.# JM..#.....#...#.....#...#..ZZ
65+
#.#.#####.#.#####.#.#.#.# #.###.###.#####.#.###.#.#
66+
#...#.......#.....#.#...# #...#.#.....#.#.#.....#.#
67+
#.#.#.###.#####.###.#.### #.#####.#####.#.#####.#.#
68+
BT..#.....#.........#.#....HO #.#...#.........#.#......PY
69+
#######.#.###.#.#####.### ###.#####.#####.#.#.#####
70+
LU....#...#.#...#.....#.#.# #...#.#.....#...#.#.#....UF
71+
###.###.#####.#####.###.# ###.#.###########.#####.#
72+
#.....#.#...#.#...#...#..UF LE........................#
73+
#.#########.###.#.#####.# #####.#####.#.###########
74+
#.......#.....#.#.#.#.#.# #.........#.#...#...#.#.#
75+
###.###.###.#.#.#.#.#.#.# #############.###.#.#.#.#
76+
#.#.#.......#...#.......# UN....#.#...#.#.#...#...#..DQ
77+
#.#.#####.#.#.#.###.#.#.# #.###.#.###.#####.#.###.#
78+
#.....#...#.#.#.#...#.#.# #.#.#...#.#...#.#.#.....#
79+
#########.###.#######.#.# #.#.###.#.###.#.#.#####.#
80+
#.....#.#.#.......#...#.# #.....................#.#
81+
#####.#.#.#.###.######### S U D M A D ###.#.#####.#.#####.###.#
82+
#...#.....#.#...#...#.#.# H V Q M B S #...#...#...#...#.....#.#
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+
D G L A Y Q
109+
H U E B A W

day20/test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ import * as path from 'path'
33
import { transportingMazeSolver, drawSolution } from './transportingMazeSolver'
44

55
const example = fs.readFileSync(
6-
path.resolve(process.cwd(), 'day20/example2.txt'),
6+
path.resolve(process.cwd(), 'day20/input.txt'),
77
'utf-8',
88
)
99

10-
const res = transportingMazeSolver(example)
10+
const res = transportingMazeSolver(example, { start: 'ZZ', end: 'AA' })
1111
if (res) drawSolution(example, res)
12+
console.log(res?.path.length)

day20/transportingMazeSolver.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ describe('Transporting maze solver', () => {
1212
path.resolve(process.cwd(), 'day20/example1.txt'),
1313
'utf-8',
1414
)
15-
const res = transportingMazeSolver(example)
15+
const res = transportingMazeSolver(example, { start: 'AA', end: 'ZZ' })
1616
expect(res?.path).toHaveLength(23)
1717
drawSolution(example, res as Location)
1818
})
@@ -22,7 +22,7 @@ describe('Transporting maze solver', () => {
2222
path.resolve(process.cwd(), 'day20/example2.txt'),
2323
'utf-8',
2424
)
25-
const res = transportingMazeSolver(example)
25+
const res = transportingMazeSolver(example, { start: 'AA', end: 'ZZ' })
2626
expect(res?.path).toHaveLength(58)
2727
drawSolution(example, res as Location)
2828
})

day20/transportingMazeSolver.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ const exploreInDirection = (
5656
visited: boolean[],
5757
portals: Portal[],
5858
location: Location,
59+
end: string,
5960
) => (direction: DIRECTION): Location => {
6061
let newLocation: Location
6162
const cl = createLocation(maze, visited, location)
@@ -80,7 +81,7 @@ const exploreInDirection = (
8081
const portal = portals.find(({ pos }) => equals(pos, newLocation.pos))
8182
if (portal) {
8283
// Is this the last portal
83-
if (portal.label === 'ZZ') {
84+
if (portal.label === end) {
8485
return {
8586
...newLocation,
8687
status: 'Target',
@@ -106,7 +107,10 @@ type MazeString = {
106107
width: number
107108
}
108109

109-
export const transportingMazeSolver = (maze: string): Location | undefined => {
110+
export const transportingMazeSolver = (
111+
maze: string,
112+
{ start, end }: { start: string; end: string },
113+
): Location | undefined => {
110114
const width = maze.indexOf('\n')
111115
const mazeString: MazeString = {
112116
width,
@@ -115,7 +119,7 @@ export const transportingMazeSolver = (maze: string): Location | undefined => {
115119
const portals = findPortals(maze)
116120
const visited = [] as boolean[]
117121

118-
const startPos = portals.find(({ label }) => label === 'AA') as Portal
122+
const startPos = portals.find(({ label }) => label === start) as Portal
119123
const queue = [
120124
{
121125
path: [],
@@ -127,7 +131,7 @@ export const transportingMazeSolver = (maze: string): Location | undefined => {
127131

128132
while (queue.length > 0) {
129133
const location = queue.shift() as Location
130-
const e = exploreInDirection(mazeString, visited, portals, location)
134+
const e = exploreInDirection(mazeString, visited, portals, location, end)
131135

132136
// Up
133137
const up = e(DIRECTION.UP)

0 commit comments

Comments
 (0)