-
Notifications
You must be signed in to change notification settings - Fork 0
/
day12.js
164 lines (140 loc) · 3.11 KB
/
day12.js
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
'use strict' // Finding different routes in cave system.
const { loadData } = require('./core/utils')
const rawInput = [loadData(module.filename)]
/**
* @param {string[]} data
* @return {{smallCaves: string[], links: Map<string, string[]>}}
*/
const prepare = (data) => {
const links = new Map(), smallCaves = ['']
const addLink = (links, a, b) => {
const v = links.get(a) || []
v.push(b)
links.set(a, v)
if (b !== 'end' && b[0] >= 'a' && !smallCaves.includes(b)) smallCaves.push(b)
}
for (const [a, b] of data) {
if (a !== 'end' && b !== 'start') addLink(links, a, b)
if (b !== 'end' && a !== 'start') addLink(links, b, a)
}
return { links, smallCaves }
}
/**
* @param {*[]} array
* @param {*} value
* @return {boolean}
*/
const wouldCycle = (array, value) => {
for (let i, last = array.length - 1, start = -1;
(i = array.indexOf(value, start + 1)) >= 0; start = i) {
if (i === last) {
return true
}
if (i > 0 && array[i - 1] === array[last]) {
return true
}
}
return false
}
/**
* @param {Map<string[]>} links
* @param {string} [specialCave] - small cave that may be visited twice.
* @return {string[][]} - routes generated.
*/
const computeRoutes = (links, specialCave = undefined) => {
const route = [], routes = []
const walk = (from, special) => {
const destinations = links.get(from)
for (const dst of destinations) {
let spec = special
if (dst === 'end') {
routes.push(route.slice())
continue
}
if (dst[0] >= 'a') {
if (route.includes(dst)) {
if (dst !== special) {
continue
}
spec = undefined
} else if (wouldCycle(route, dst)) {
continue
}
}
route.push(dst)
walk(dst, spec)
route.pop()
}
}
walk('start', specialCave)
return routes
}
/** @param {string[]} data */
const puzzle1 = (data) => {
return computeRoutes(prepare(data).links).length
}
/** @param {string[]} data */
const puzzle2 = (data) => {
const passed = new Set()
for (let { links, smallCaves } = prepare(data), routes; smallCaves.length > 0;) {
routes = computeRoutes(links, smallCaves.pop())
for (const route of routes) {
passed.add(route.join(' '))
}
}
return passed.size
}
const parse = (dsn) => {
let data = rawInput[dsn]
if (data && (data = data.split('\n').filter(v => Boolean(v))).length) {
return data.map(row => row.split('-'))
}
return data // NOTE: The runner will distinguish between undefined and falsy!
}
// Example (demo) data.
rawInput[1] = `
start-A
start-b
A-c
A-b
b-d
A-end
b-end
` /* */
/*
rawInput[1] = `
dc-end
HN-start
start-kj
dc-start
dc-HN
LN-dc
HN-end
kj-sa
kj-HN
kj-dc` /* */
/*
rawInput[1] = `
fs-end
he-DX
fs-he
start-DX
pj-DX
end-zg
zg-sl
zg-pj
pj-he
RW-he
fs-DX
pj-RW
zg-RW
start-pj
he-WI
zg-he
pj-fs
start-RW` /* */
module.exports = { parse, puzzles: [puzzle1, puzzle2] }
/*
"demo": { "1": { "value": 10, "time": 343 }, "2": { "value": 36, "time": 293 } },
"main": { "1": { "value": 4167, "time": 9199 }, "2": { "value": 98441, "time": 237054 } }
*/