-
Notifications
You must be signed in to change notification settings - Fork 0
/
aoc-2019-day03.js
192 lines (158 loc) Β· 5.08 KB
/
aoc-2019-day03.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import aocLoader from 'aoc-loader'
aocLoader(2019, 3, '53616c7465645f5fa933f62e13c8adae8f04b04212c2b41f522aac916a7eb661323b1552217882666e607e10f24d8952').then(data => {
console.log(dayXP1(data))
console.log(dayXP2(data))
})
function dayXP1(data){
solveForFirstStar(data)
}
function dayXP2(data){
solveForSecondStar(data)
}
//https://github.com/johnbeech/advent-of-code-2019/blob/master/solutions/day3/solution.js
// const path = require('path')
// const { read, write, position } = require('promise-path')
// const fromHere = position(__dirname)
// const report = (...messages) => console.log(`[${require(fromHere('../../package.json')).logName} / ${__dirname.split(path.sep).pop()}]`, ...messages)
const report = (...messages) => console.log(...messages);
const directions = {
L: { x: -1, y: 0 },
R: { x: 1, y: 0 },
U: { x: 0, y: 1 },
D: { x: 0, y: -1 }
}
function parseInstruction (string) {
const pattern = /([LRUD])(\d+)/
const [, direction, value] = string.match(pattern)
return { direction, value }
}
function mark (map, position, index) {
const key = `${position.x},${position.y}`
map[key] = (map[key] === undefined || map[key] === index) ? index : 'X'
}
function highlight (map, position, wireIndex, stepCount) {
const key = `${position.x},${position.y}`
if (!map[key]) {
map[key] = {
x: position.x,
y: position.y,
wires: {}
}
}
map[key].wires[wireIndex] = map[key].wires[wireIndex] || []
map[key].wires[wireIndex].push(stepCount)
}
function parseKey (key) {
const values = key.split(',').map(n => Number.parseInt(n))
return { x: values[0], y: values[1] }
}
async function renderMap (map, filename) {
const boundary = {
left: 0,
right: 0,
top: 0,
bottom: 0
}
Object.keys(map)
.map(parseKey)
.forEach(key => {
boundary.left = Math.min(boundary.left, key.x)
boundary.right = Math.max(boundary.right, key.x)
boundary.top = Math.max(boundary.top, key.y)
boundary.bottom = Math.min(boundary.bottom, key.y)
})
report('Boundary', boundary)
if (Math.abs(boundary.left) + Math.abs(boundary.right) > 1000 && Math.abs(boundary.top) + Math.abs(boundary.bottom) > 1000) {
return report('Boundary too big to contemplate rendering.')
}
const lines = []
for (let j = boundary.bottom; j <= boundary.top; j++) {
const line = []
for (let i = boundary.left; i <= boundary.right; i++) {
const code = map[`${i},${j}`]
line.push(code === undefined ? '.' : code)
}
lines.push(line)
}
const output = lines.map(line => line.join('')).join('\n')
await write(fromHere(filename), output)
}
async function run () {
const input = (await read(fromHere('input.txt'), 'utf8')).trim()
await solveForFirstStar(input)
await solveForSecondStar(input)
}
async function solveForFirstStar (input) {
const wires = input.split('\n')
const map = {}
wires.forEach((wireInput, index) => {
const instructions = wireInput.split(',').filter(n => n).map(parseInstruction)
report('Wire Instructions', instructions)
const position = { x: 0, y: 0 }
mark(map, position, 'O')
instructions.forEach(instruction => {
const direction = directions[instruction.direction]
let distance = 0
while (distance < instruction.value) {
position.x = position.x + direction.x
position.y = position.y + direction.y
mark(map, position, index)
distance = distance + 1
}
})
})
map['0,0'] = 'O'
renderMap(map, 'output.txt')
const intersections = Object.entries(map)
.filter(x => x[1] === 'X')
.map(x => parseKey(x[0]))
.map(key => {
const distance = Math.abs(key.x) + Math.abs(key.y)
return {
key,
distance
}
})
.sort((a, b) => {
return a.distance - b.distance
})
report('Intersections', intersections)
const solution = intersections[0].distance
report('Solution 1:', solution)
}
async function solveForSecondStar (input) {
const wires = input.split('\n')
const map = {}
wires.forEach((wireInput, index) => {
const instructions = wireInput.split(',').filter(n => n).map(parseInstruction)
report('Wire Instructions', instructions)
const position = { x: 0, y: 0 }
let stepCount = 0
highlight(map, position, 'O', stepCount)
instructions.forEach(instruction => {
const direction = directions[instruction.direction]
let distance = 0
while (distance < instruction.value) {
stepCount = stepCount + 1
position.x = position.x + direction.x
position.y = position.y + direction.y
highlight(map, position, index, stepCount)
distance = distance + 1
}
})
})
const intersections = Object.values(map)
.filter(x => Object.keys(x.wires).length > 1)
.map(x => {
report(x.wires)
x.intersectionDistance = x.wires[0][0] + x.wires[1][0]
return x
})
.sort((a, b) => {
return a.intersectionDistance - b.intersectionDistance
})
report('Intersections', intersections)
const solution = intersections[0].intersectionDistance
report('Solution 2:', solution)
}
// run()