Skip to content

Commit fb24e36

Browse files
committed
Up to day 19
1 parent 41c4c42 commit fb24e36

26 files changed

+1175
-49
lines changed

package-lock.json

Lines changed: 26 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"name": "joshthompson-adventofcode",
33
"version": "1.0.0",
44
"dependencies": {
5+
"js-csp": "^1.0.1",
56
"static-server": "^3.0.0"
67
},
78
"main": "static-server.js",

src/2015/day09.js

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,30 @@
11
AdventOfCode.day09 = {
22

3-
part1: input => {
4-
let distances = {}
3+
process: input => {
4+
let network = {}
55
input.split('\n').forEach(text => {
6-
let data = text.match(/([a-zA-Z]*) to ([a-zA-Z]*) = ([0-9]*)/i)
7-
distances[data[1]] = distances[data[1]] ? distances[data[1]] : {}
8-
distances[data[2]] = distances[data[2]] ? distances[data[2]] : {}
9-
distances[data[1]][data[2]] = distances[data[2]][data[1]] = parseInt(data[3])
6+
let d = text.match(/([a-zA-Z]*) to ([a-zA-Z]*) = ([0-9]*)/i)
7+
network[d[1]] = network[d[1]] ? network[d[1]] : {}
8+
network[d[2]] = network[d[2]] ? network[d[2]] : {}
9+
network[d[1]][d[2]] = network[d[2]][d[1]] = parseInt(d[3])
10+
})
11+
console.log(network)
12+
return network
13+
},
14+
15+
findShortestRoute: (map) => {
16+
_.forEach(distances, distance => {
17+
let route = [city]
18+
routes.push(route)
1019
})
11-
console.log(distances)
20+
},
1221

13-
_.forEach(distances, () => {
14-
22+
part1: input => {
23+
let map = AdventOfCode.day09.process(input)
24+
let routes = {}
25+
let shortest = false
26+
_.forEach(map, (distances, city) => {
27+
AdventOfCode.day09.findShortestRoute(map)
1528
})
1629
},
1730

src/2017/day01.js

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,70 @@
11
AdventOfCode.day01 = {
22

3-
part1: input => {
3+
part1: async (input, render) => {
44
let numbers = input.split('').map(i => parseInt(i))
55
numbers.push(numbers[0])
66
let sum = 0
77

88
for (let i = 0; i < numbers.length - 1; i++) {
99
sum += numbers[i] === numbers[i + 1] ? numbers[i] : 0
10+
if (render) await AdventOfCode.day01.render(numbers, i, sum)
1011
}
1112
return sum
1213
},
1314

14-
part2: input => {
15+
part2: async (input, render) => {
1516
let numbers = input.split('').map((i) => parseInt(i))
1617
let sum = 0
1718

1819
for (let i = 0; i < numbers.length - 1; i++) {
19-
j = (i + numbers.length / 2) % numbers.length
20+
let j = (i + numbers.length / 2) % numbers.length
2021
sum += numbers[i] === numbers[j] ? numbers[i] : 0
22+
if (render) await AdventOfCode.day01.render2(numbers, i, sum)
2123
}
2224
return sum
25+
},
26+
27+
renderHtml: "<canvas id='canvas1_1' style='height: 80px;'></canvas><canvas id='canvas1_2' style='height:95px;'></div>",
28+
29+
render: async (numbers, i, sum) => {
30+
let c = document.getElementById('canvas1_1').getContext('2d')
31+
let w = c.canvas.width = document.getElementById('canvas1_1').clientWidth
32+
let h = c.canvas.height = document.getElementById('canvas1_1').clientHeight
33+
34+
c.fillStyle = numbers[i] === numbers[i + 1] ? '#0F0' : '#FF0'
35+
c.fillRect(10, 20, 30, 25)
36+
c.fillStyle = '#000'
37+
38+
for (let num = i; num < numbers.length; num++) {
39+
c.fillText(numbers[num], (num - i) * 15 + 15, 35)
40+
}
41+
42+
c.fillText(`Part 1`, 15, 10)
43+
c.fillText(`Sum: ${sum}`, 15, 60)
44+
45+
await sleep(10)
46+
},
47+
48+
render2: async (numbers, i, sum) => {
49+
let c = document.getElementById('canvas1_2').getContext('2d')
50+
let w = c.canvas.width = document.getElementById('canvas1_2').clientWidth
51+
let h = c.canvas.height = document.getElementById('canvas1_2').clientHeight
52+
53+
54+
let j = (i + numbers.length / 2) % numbers.length
55+
c.fillStyle = numbers[i] === numbers[j] ? '#0F0' : '#FF0'
56+
c.fillRect(10, 20, 15, 45)
57+
c.fillStyle = '#000'
58+
59+
for (let num = i; num < numbers.length; num++) {
60+
c.fillText(numbers[num], (num - i) * 15 + 15, 35)
61+
c.fillText(numbers[(num + numbers.length / 2) % numbers.length], (num - i) * 15 + 15, 55)
62+
}
63+
64+
c.fillText(`Part 1`, 15, 10)
65+
c.fillText(`Sum: ${sum}`, 15, 80)
66+
67+
await sleep(10)
2368
}
2469

2570
}

src/2017/day03.js

Lines changed: 100 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,65 @@
11
AdventOfCode.day03 = {
22

3-
part1: n => {
3+
part1: (n, render) => {
4+
// Functions
5+
let sideFn = level => 2 * level + 1
6+
let startFn = level => Math.pow(sideFn(level - 1), 2) + 1
7+
8+
// Which level of the spiral we're on
9+
// Also the numbers of intial outward steps
10+
let level = Math.floor(Math.ceil(Math.sqrt(n)) / 2)
11+
let side = sideFn(level)
12+
let start = startFn(level)
13+
let offset = level - 1 // Offset from lowest value in level to middle
14+
15+
let distance = n
16+
17+
for (let i = 0; i < 4; i++) { // Loop each side
18+
let side_middle = start + offset + (side - 1) * i // Middle value for this side
19+
let side_distance = Math.abs(side_middle - n)
20+
distance = Math.min(distance, side_distance) // Keep shortest side distance
21+
}
22+
23+
if (render) AdventOfCode.day03.render(parseInt(n), level, distance, 0)
24+
25+
return level + distance
26+
},
27+
28+
part1_2: n => {
429
let level = Math.floor(Math.ceil(Math.sqrt(n)) / 2)
530
let side = 2 * level + 1
631
let highest = Math.pow(side, 2)
732
while (n + side < highest) n += side
833
return level + Math.abs(highest - (side - 1) / 2 - n)
934
},
1035

36+
processMove: (grid, x, y) => {
37+
let g = (x, y) => grid[x + ',' + y] ? grid[x + ',' + y] : 0 // value of cell in grid
38+
let c = (x, y) => !g(x, y) // is cell clear
39+
40+
// NORTH EAST SOUTH WEST
41+
if ( c(x, y-1) && c(x+1, y) && c(x, y+1) && c(x-1, y)) x += 1 // Everything is empty
42+
43+
else if ( c(x, y-1) && c(x+1, y) && !c(x, y+1) && c(x-1, y)) x -= 1 // Just south is blocked
44+
else if ( c(x, y-1) && !c(x+1, y) && !c(x, y+1) && c(x-1, y)) x -= 1 // Just south and east are blocked
45+
46+
else if ( c(x, y-1) && !c(x+1, y) && c(x, y+1) && c(x-1, y)) y += 1 // Just east is blocked
47+
else if ( !c(x, y-1) && !c(x+1, y) && c(x, y+1) && c(x-1, y)) y += 1 // Just north and east are blocked
48+
49+
else if ( !c(x, y-1) && c(x+1, y) && c(x, y+1) && c(x-1, y)) x += 1 // Just north is blocked
50+
else if ( !c(x, y-1) && c(x+1, y) && c(x, y+1) && !c(x-1, y)) x += 1 // Just north and west are blocked
51+
52+
else if ( c(x, y-1) && c(x+1, y) && c(x, y+1) && !c(x-1, y)) y -= 1 // Just west is blocked
53+
else if ( c(x, y-1) && c(x+1, y) && !c(x, y+1) && !c(x-1, y)) y -= 1 // Just south and west are blocked
54+
55+
return {x: x, y: y}
56+
},
57+
1158
part2: input => {
1259
let grid = {
1360
'0,0': 1
1461
}
15-
let g = (x, y) => grid[x + ',' + y] ? grid[x + ',' + y] : 0// value of cell in grid
62+
let g = (x, y) => grid[x + ',' + y] ? grid[x + ',' + y] : 0 // value of cell in grid
1663
let c = (x, y) => !g(x, y) // is cell clear
1764
let set = (x, y, value) => grid[x + ',' + y] = value
1865

@@ -22,21 +69,9 @@ AdventOfCode.day03 = {
2269

2370
while (value < input) {
2471
// Move to next cell
25-
26-
// NORTH EAST SOUTH WEST
27-
if ( c(x, y-1) && c(x+1, y) && c(x, y+1) && c(x-1, y)) x += 1 // Everything is empty
28-
29-
else if ( c(x, y-1) && c(x+1, y) && !c(x, y+1) && c(x-1, y)) x -= 1 // Just south is blocked
30-
else if ( c(x, y-1) && !c(x+1, y) && !c(x, y+1) && c(x-1, y)) x -= 1 // Just south and east are blocked
31-
32-
else if ( c(x, y-1) && !c(x+1, y) && c(x, y+1) && c(x-1, y)) y += 1 // Just east is blocked
33-
else if ( !c(x, y-1) && !c(x+1, y) && c(x, y+1) && c(x-1, y)) y += 1 // Just north and east are blocked
34-
35-
else if ( !c(x, y-1) && c(x+1, y) && c(x, y+1) && c(x-1, y)) x += 1 // Just north is blocked
36-
else if ( !c(x, y-1) && c(x+1, y) && c(x, y+1) && !c(x-1, y)) x += 1 // Just north and west are blocked
37-
38-
else if ( c(x, y-1) && c(x+1, y) && c(x, y+1) && !c(x-1, y)) y -= 1 // Just west is blocked
39-
else if ( c(x, y-1) && c(x+1, y) && !c(x, y+1) && !c(x-1, y)) y -= 1 // Just south and west are blocked
72+
let pos = AdventOfCode.day03.processMove(grid, x, y)
73+
x = pos.x
74+
y = pos.y
4075

4176
// Calculate total
4277
value = g(x-1, y-1) + g(x+0, y-1) + g(x+1, y-1)
@@ -47,6 +82,54 @@ AdventOfCode.day03 = {
4782

4883
return value
4984

85+
},
86+
87+
renderHtml: '<canvas id="canvas3_1" style="height: 500px;"></canvas>',
88+
89+
render: async (n, level, distance, frame) => {
90+
let c = document.getElementById('canvas3_1').getContext('2d')
91+
let w = c.canvas.width = document.getElementById('canvas3_1').clientWidth
92+
let h = c.canvas.height = document.getElementById('canvas3_1').clientHeight
93+
let max = 2000
94+
c.font = '8px Verdana'
95+
while (!n || parseInt(n) <= 1 || parseInt(n) > max) n = parseInt(prompt(`Number to find (Integer from 2 - ${max})`))
96+
console.log(parseInt(n))
97+
let grid = {}
98+
let size = 22
99+
let x = 0
100+
let y = 0
101+
let i = 1
102+
103+
// Draw grid
104+
while (i <= n) {
105+
let pos = AdventOfCode.day03.processMove(grid, x, y)
106+
x = pos.x
107+
y = pos.y
108+
c.fillText(i, w/2 + x * size, h/2 + y * size / 2)
109+
grid[x + ',' + y] = i+1
110+
i++
111+
await sleep(1)
112+
}
113+
114+
let xa = x / Math.abs(x)
115+
let ya = y / Math.abs(y)
116+
let cx = 0
117+
let cy = 0
118+
let colour = 0
119+
for (cx = 0; Math.abs(cx) <= Math.abs(x) + (x>0?-2:0); cx += xa) {
120+
c.fillStyle = `hsla(${137*colour++}, 100%, 50%, 0.5)`
121+
c.fillRect(w/2 + (cx + 0.8) * size, h/2 + (cy - 0.8) * size / 2, size, size / 2)
122+
await sleep(25)
123+
}
124+
for (cy = 0; Math.abs(cy) <= Math.abs(y); cy += ya) {
125+
c.fillStyle = `hsla(${137*colour++}, 100%, 50%, 0.5)`
126+
c.fillRect(w/2 + (cx + 0.8) * size, h/2 + (cy - 0.8) * size / 2, size, size / 2)
127+
await sleep(25)
128+
}
129+
c.fillRect = w/2 - 5
130+
131+
132+
50133
}
51134

52135
}

src/2017/day10.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ AdventOfCode.day10 = {
66
return list[0] * list[1]
77
},
88

9-
part2: input => {
9+
part2: (input, rounds) => {
1010
let lengths = input.split('').map(c => c.charCodeAt(0)).concat(17, 31, 73, 47, 23)
11-
let list = AdventOfCode.day10.process(lengths, 64)
11+
let list = AdventOfCode.day10.process(lengths, rounds ? rounds : 64)
1212
let dense = []
1313
while (list.length) dense.push(list.splice(0, 16).reduce((a, b) => a ^ b))
1414
return dense.map(x => x.toString(16)).map(x => x.length === 1 ? '0' + x : x).join('')
@@ -37,4 +37,6 @@ AdventOfCode.day10 = {
3737
return list
3838
}
3939

40-
}
40+
}
41+
42+
AdventOfCode.knotHash = (input, rounds) => AdventOfCode.day10.part2(input, rounds) // For use in other days tasks

0 commit comments

Comments
 (0)