Skip to content

Commit 465da87

Browse files
committed
feat(day20): example with 23 steps
1 parent ff60f96 commit 465da87

File tree

3 files changed

+27
-9
lines changed

3 files changed

+27
-9
lines changed

day20/test.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import * as fs from 'fs'
22
import * as path from 'path'
3-
import { transportingMazeSolver } from './transportingMazeSolver'
3+
import { transportingMazeSolver, drawSolution } from './transportingMazeSolver'
44

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

1010
const res = transportingMazeSolver(example)
11-
console.log(res)
12-
console.log(res?.path.length)
11+
if (res) drawSolution(example, res)
Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { transportingMazeSolver } from './transportingMazeSolver'
1+
import {
2+
transportingMazeSolver,
3+
drawSolution,
4+
Location,
5+
} from './transportingMazeSolver'
26
import * as fs from 'fs'
37
import * as path from 'path'
48

@@ -8,6 +12,8 @@ describe('Transporting maze solver', () => {
812
path.resolve(process.cwd(), 'day20/example1.txt'),
913
'utf-8',
1014
)
11-
expect(transportingMazeSolver(example)?.path).toHaveLength(23)
15+
const res = transportingMazeSolver(example)
16+
expect(res?.path).toHaveLength(23)
17+
drawSolution(example, res as Location)
1218
})
1319
})

day20/transportingMazeSolver.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { findPortals, Portal } from './findPortals'
33
export type Position = { x: number; y: number }
44

55
type LocationStatus = 'Start' | 'Valid' | 'Blocked' | 'Target'
6-
type Location = {
6+
export type Location = {
77
pos: Position
88
path: Position[]
99
status: LocationStatus
@@ -92,8 +92,8 @@ const exploreInDirection = (
9292
visited[pair.pos.y * maze.width + pair.pos.x] = true
9393
return {
9494
pos: pair.pos,
95-
path: [...location.path, location.pos],
96-
status: status(maze, visited, pair.pos),
95+
path: [...location.path, location.pos, portal.pos],
96+
status: 'Valid',
9797
}
9898
}
9999
}
@@ -164,3 +164,16 @@ export const transportingMazeSolver = (maze: string): Location | undefined => {
164164

165165
return undefined
166166
}
167+
168+
export const drawSolution = (maze: string, finalLocation: Location) => {
169+
const width = maze.indexOf('\n')
170+
const mapAsString = maze.trimEnd().replace(/\n/g, '')
171+
let solution = mapAsString
172+
finalLocation.path.map(p => {
173+
solution =
174+
solution.substr(0, p.y * width + p.x) +
175+
'@' +
176+
solution.substr(p.y * width + p.x + 1)
177+
})
178+
console.log(new RegExp(`.{${width}}`, 'g').exec(solution)?.join('\n'))
179+
}

0 commit comments

Comments
 (0)