Skip to content

Commit ed78d2b

Browse files
committed
Update with some 2020 and 2019 solutions
1 parent fb24e36 commit ed78d2b

File tree

3,019 files changed

+114318
-89
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

3,019 files changed

+114318
-89
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
node_modules/*
1+
node_modules/*
2+
.DS_Store

2019/01/index.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const fs = require('fs')
2+
3+
fs.readFile(`${__dirname}/input.txt`, (_e, data) => {
4+
const modules = data.toString().split('\n').filter(line => line.length).map(Number)
5+
console.log('Part 1', totalFuel1(modules))
6+
console.log('Part 2', totalFuel2(modules))
7+
})
8+
9+
function totalFuel1(modules) {
10+
return modules.map(m => Math.floor(m / 3) - 2).reduce((a, b) => a + b, 0)
11+
}
12+
13+
function totalFuel2(modules) {
14+
return modules.map(m => {
15+
let f = 0;
16+
let n = m;
17+
while (n > 0) {
18+
n = Math.max(0, Math.floor(n / 3) - 2)
19+
f += n
20+
}
21+
return f
22+
}).reduce((a, b) => a + b, 0)
23+
}

2019/01/input.txt

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
103910
2+
133712
3+
82560
4+
91679
5+
98354
6+
89007
7+
93288
8+
132363
9+
91373
10+
83666
11+
55958
12+
90387
13+
100869
14+
98127
15+
120197
16+
86931
17+
60370
18+
143999
19+
71541
20+
115662
21+
51287
22+
81624
23+
58307
24+
60408
25+
141664
26+
89781
27+
127772
28+
132353
29+
101220
30+
104001
31+
140488
32+
58072
33+
75764
34+
120003
35+
82386
36+
77603
37+
130604
38+
86672
39+
120987
40+
80334
41+
67674
42+
52918
43+
98041
44+
102541
45+
97612
46+
50436
47+
129998
48+
84854
49+
101867
50+
82039
51+
108966
52+
80708
53+
54588
54+
86854
55+
89607
56+
71869
57+
126093
58+
89460
59+
86558
60+
77651
61+
53295
62+
132188
63+
137266
64+
97370
65+
114620
66+
86691
67+
147199
68+
147299
69+
72616
70+
142654
71+
88610
72+
104030
73+
64256
74+
54867
75+
76532
76+
145081
77+
102335
78+
72987
79+
72684
80+
148155
81+
59739
82+
85954
83+
141001
84+
125171
85+
107764
86+
141622
87+
89536
88+
92435
89+
69038
90+
84518
91+
119700
92+
119801
93+
81677
94+
125317
95+
72683
96+
128905
97+
93666
98+
75633
99+
117361
100+
82295

2019/02/index.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const fs = require('fs')
2+
3+
fs.readFile(`${__dirname}/input.txt`, (_e, data) => {
4+
const cmds = data.toString().split(',').filter(v => v.trim().length).map(Number)
5+
console.log('Part 1', run(cmds, 12, 02))
6+
console.log('Part 2', findInput(cmds, 19690720))
7+
})
8+
9+
function run(cmds, noun, verb) {
10+
cmds = [...cmds]
11+
cmds[1] = noun
12+
cmds[2] = verb
13+
let pos = 0
14+
while (true) {
15+
switch (cmds[pos]) {
16+
case 1: cmds[cmds[pos + 3]] = cmds[cmds[pos + 1]] + cmds[cmds[pos + 2]]; break
17+
case 2: cmds[cmds[pos + 3]] = cmds[cmds[pos + 1]] * cmds[cmds[pos + 2]]; break
18+
case 99: return cmds[0]
19+
default: throw `Unknown code: ${cmds[pos]}`
20+
}
21+
pos += 4
22+
}
23+
}
24+
25+
function findInput(cmds, value) {
26+
for (let noun = 0; noun <= 99; noun++) {
27+
for (let verb = 0; verb <= 99; verb++) {
28+
if (run(cmds, noun, verb) === value) {
29+
return 100 * noun + verb
30+
}
31+
}
32+
}
33+
}

2019/02/input.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1,0,0,3,1,1,2,3,1,3,4,3,1,5,0,3,2,6,1,19,1,5,19,23,1,13,23,27,1,6,27,31,2,31,13,35,1,9,35,39,2,39,13,43,1,43,10,47,1,47,13,51,2,13,51,55,1,55,9,59,1,59,5,63,1,6,63,67,1,13,67,71,2,71,10,75,1,6,75,79,1,79,10,83,1,5,83,87,2,10,87,91,1,6,91,95,1,9,95,99,1,99,9,103,2,103,10,107,1,5,107,111,1,9,111,115,2,13,115,119,1,119,10,123,1,123,10,127,2,127,10,131,1,5,131,135,1,10,135,139,1,139,2,143,1,6,143,0,99,2,14,0,0

2019/03/index.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
const fs = require('fs')
2+
3+
fs.readFile(`${__dirname}/input.txt`, (_e, data) => {
4+
const wires = data.toString().split('\n').filter(line => line.length).map(wire => wire.split(','))
5+
const results = processWires(wires)
6+
console.log('Part 1', results.closest)
7+
console.log('Part 2', results.quickest)
8+
})
9+
10+
function processWires(wires) {
11+
let grid = [[]]
12+
let closest = 999999
13+
let quickest = 999999
14+
wires.forEach((wire, id) => {
15+
let x = 0
16+
let y = 0
17+
let t = 0
18+
grid[x][y] = id
19+
wire.forEach(w => {
20+
const direction = w[0]
21+
const distance = parseInt(w.substr(1))
22+
for (let i = 0; i < distance; i++) {
23+
t++
24+
if (direction === 'R') x++
25+
else if (direction === 'L') x--
26+
else if (direction === 'D') y++
27+
else if (direction === 'U') y--
28+
const v = set(grid, x, y, id, t)
29+
if (v.intersect && Math.abs(x) + Math.abs(y) < closest) {
30+
closest = Math.abs(x) + Math.abs(y)
31+
}
32+
if (v.intersect && v.totalTime < quickest) {
33+
quickest = v.totalTime
34+
}
35+
}
36+
})
37+
})
38+
grid[0][0] = '*'
39+
return { closest, quickest }
40+
}
41+
42+
function get(grid, x, y) {
43+
if (grid[x] === undefined) {
44+
grid[x] = []
45+
}
46+
if (grid[x][y] === undefined) {
47+
grid[x][y] = null
48+
}
49+
return grid[x][y]
50+
}
51+
52+
function set(grid, x, y, id, time) {
53+
const v = get(grid, x, y)
54+
if (v === null) {
55+
grid[x][y] = {
56+
wires: [{ id, time }],
57+
intersect: false,
58+
totalTime: time
59+
}
60+
} else if (!v.wires.find(w => w.id === id)) {
61+
grid[x][y].wires.push({ id, time })
62+
grid[x][y].intersect = true
63+
grid[x][y].totalTime += time
64+
}
65+
return grid[x][y]
66+
}

2019/03/input.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
R990,U803,R777,U157,R629,D493,R498,D606,R344,U241,L708,D403,R943,U961,L107,D755,R145,D77,L654,D297,L263,D904,R405,U676,R674,U139,L746,U935,R186,U433,L739,D774,R470,D459,R865,D209,L217,U525,R747,D218,R432,U769,L876,D477,R606,D161,R991,D338,R647,D958,R777,D148,R593,D873,L95,U707,R468,U518,R845,U285,R221,U771,R989,D107,R44,U833,L343,D420,R468,D954,L604,D270,L691,U401,R850,U70,R441,U461,R638,D743,R65,U673,L999,U110,R266,U759,R768,U569,L250,D577,R247,U420,L227,U437,L80,D647,L778,U935,R585,U35,L735,D201,R694,U635,L597,U215,R743,D542,L701,U946,L503,U589,R836,D687,L444,U409,L473,U132,L570,U374,R193,D908,L800,U294,L252,U851,R947,D647,L37,D20,L27,U620,L534,D356,L291,U611,L128,D670,L364,U200,L749,D708,R776,U99,R606,D999,L810,D373,R212,D138,R856,D966,L206,D23,L860,D731,L914,U716,L212,U225,R766,U348,L220,D69,L766,D15,L557,U71,R734,D295,R884,D822,R300,D152,L986,D170,R764,U24,R394,D710,L860,U830,L305,U431,R201,D44,R882,U667,R37,D727,R916,U460,L834,D771,R373,U96,L707,D576,R607,D351,R577,D200,L402,U364,L32,D512,L152,D283,L232,U804,R827,U352,R104,D323,L254,U273,L451,D967,R739,D53,L908,D866,R998,U897,L581,U538,R206,U644,L70,D17,L481,U912,L377,D922,L286,U547,R35,U292,L318,U256,R79,D52,R92,U160,R763,U428,R663,D634,R212,D325,R460,U142,L375,U382,R20,D321,L220,D578,R915,D465,L797,D849,L281,D491,L911,D624,R800,U629,L675,U428,L219,U694,R680,U350,R113,D903,L22,D683,L787,D1,R93,U315,L562,U756,R622,D533,L587,D216,L933,U972,R506,U536,R797,U828,L12,D965,L641,U165,R937,D675,R259
2+
L998,D197,L301,D874,L221,U985,L213,D288,R142,D635,R333,D328,R405,D988,L23,D917,R412,D971,R876,U527,R987,D884,R39,D485,L971,U200,R931,U79,L271,U183,R354,D18,R346,D866,L752,D204,L863,U784,R292,U676,R811,U721,L53,U983,L993,U822,R871,U539,L782,D749,R417,U667,R882,U467,R321,U894,R912,U756,L102,U154,L57,D316,R200,U372,L44,U406,L426,D613,R847,U977,R303,U469,R509,U839,L633,D267,L487,D976,R325,U399,L359,U161,L305,U935,R522,D848,R784,D273,L337,D55,L266,U406,L858,D650,L176,D124,R231,U513,L462,U328,L674,D598,R568,D742,L39,D438,L643,D254,R577,U519,R325,U124,R91,U129,L79,D52,R480,D46,R129,D997,R452,D992,L721,U490,L595,D666,R372,D198,R813,U624,L469,U59,R578,U184,R117,D749,L745,U302,R398,D951,L683,D360,R476,D788,R70,U693,R295,D547,L61,U782,R440,D818,L330,D321,L968,U622,R160,U571,L886,D43,L855,U272,R530,D267,L312,D519,L741,D697,R206,U148,L445,U857,R983,D192,L788,U826,R805,U932,R888,D250,L682,D52,R406,D176,R984,D637,L947,D416,L687,U699,L544,D710,L933,D171,L357,D134,L968,D538,R496,D240,L730,U771,R554,U708,R265,D748,L839,U668,L333,U335,R526,U809,L653,D6,R234,D130,R871,U911,R538,U372,L960,D535,L196,U236,L966,D185,L166,U789,L885,U453,R627,D586,R501,U222,L280,U124,R755,D159,L759,U78,R669,D889,L150,D888,L71,D917,L126,D97,L138,U726,R160,D971,R527,D988,R455,D413,R539,U923,R258,U734,L459,D954,R877,U613,R343,D98,R238,U478,R514,U814,L274,U119,L958,U698,R761,U693,R367,D111,L800,D531,L91,U616,R208,D255,R169,U145,R671,U969,L468,U566,R589,D455,R323,D303,R374,D890,R377,D262,L40,U85,L719

2019/04/index.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Puzzle input: 264360-746325
2+
const min = 264360
3+
const max = 746325
4+
let total1 = 0
5+
let total2 = 0
6+
7+
function check(i) {
8+
const pw = `${i}`.split('').map(Number)
9+
const sorted = [...pw].sort((a, b) => a - b)
10+
const rule1 = pw.find((c, i) => i > 0 && c === pw[i - 1])
11+
const rule2 = rule1 && pw.every((c, i) => c === sorted[i])
12+
const rule3 = rule1 && rule2 && pw.find((c, i) => c === pw[i - 1] && c !== pw[i - 2] && c !== pw[i + 1])
13+
return [ rule2, rule3 ]
14+
}
15+
16+
for (let i = min; i <= max; i++) {
17+
const ch = check(i)
18+
total1 += ch[0] ? 1 : 0
19+
total2 += ch[1] ? 1 : 0
20+
}
21+
22+
console.log('Part 1', total1)
23+
console.log('Part 2', total2)

2019/05/index.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const fs = require('fs')
2+
const Intcode = require('../shared/intcode')
3+
4+
fs.readFile(`${__dirname}/input.txt`, (_e, data) => {
5+
const program = data.toString().split(',').filter(v => v.trim().length).map(Number)
6+
try {
7+
const output1 = Intcode.run(program, [1])
8+
console.log('Part 1', output1[output1.length - 1])
9+
const output2 = Intcode.run(program, [5])
10+
console.log('Part 2', output2[0])
11+
} catch (error) {
12+
console.log('ERROR!', error)
13+
}
14+
})

2019/05/input.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3,225,1,225,6,6,1100,1,238,225,104,0,1102,45,16,225,2,65,191,224,1001,224,-3172,224,4,224,102,8,223,223,1001,224,5,224,1,223,224,223,1102,90,55,225,101,77,143,224,101,-127,224,224,4,224,102,8,223,223,1001,224,7,224,1,223,224,223,1102,52,6,225,1101,65,90,225,1102,75,58,225,1102,53,17,224,1001,224,-901,224,4,224,1002,223,8,223,1001,224,3,224,1,224,223,223,1002,69,79,224,1001,224,-5135,224,4,224,1002,223,8,223,1001,224,5,224,1,224,223,223,102,48,40,224,1001,224,-2640,224,4,224,102,8,223,223,1001,224,1,224,1,224,223,223,1101,50,22,225,1001,218,29,224,101,-119,224,224,4,224,102,8,223,223,1001,224,2,224,1,223,224,223,1101,48,19,224,1001,224,-67,224,4,224,102,8,223,223,1001,224,6,224,1,223,224,223,1101,61,77,225,1,13,74,224,1001,224,-103,224,4,224,1002,223,8,223,101,3,224,224,1,224,223,223,1102,28,90,225,4,223,99,0,0,0,677,0,0,0,0,0,0,0,0,0,0,0,1105,0,99999,1105,227,247,1105,1,99999,1005,227,99999,1005,0,256,1105,1,99999,1106,227,99999,1106,0,265,1105,1,99999,1006,0,99999,1006,227,274,1105,1,99999,1105,1,280,1105,1,99999,1,225,225,225,1101,294,0,0,105,1,0,1105,1,99999,1106,0,300,1105,1,99999,1,225,225,225,1101,314,0,0,106,0,0,1105,1,99999,7,226,677,224,102,2,223,223,1005,224,329,1001,223,1,223,8,226,677,224,1002,223,2,223,1005,224,344,101,1,223,223,8,226,226,224,1002,223,2,223,1006,224,359,101,1,223,223,1008,677,226,224,1002,223,2,223,1005,224,374,1001,223,1,223,108,677,677,224,1002,223,2,223,1005,224,389,1001,223,1,223,1107,226,677,224,1002,223,2,223,1006,224,404,101,1,223,223,1008,226,226,224,102,2,223,223,1006,224,419,1001,223,1,223,7,677,226,224,1002,223,2,223,1005,224,434,101,1,223,223,1108,226,226,224,1002,223,2,223,1005,224,449,101,1,223,223,7,226,226,224,102,2,223,223,1005,224,464,101,1,223,223,108,677,226,224,102,2,223,223,1005,224,479,1001,223,1,223,1007,677,226,224,1002,223,2,223,1006,224,494,1001,223,1,223,1007,677,677,224,1002,223,2,223,1006,224,509,1001,223,1,223,107,677,677,224,1002,223,2,223,1005,224,524,101,1,223,223,1108,226,677,224,102,2,223,223,1006,224,539,1001,223,1,223,8,677,226,224,102,2,223,223,1005,224,554,101,1,223,223,1007,226,226,224,102,2,223,223,1006,224,569,1001,223,1,223,107,677,226,224,102,2,223,223,1005,224,584,1001,223,1,223,108,226,226,224,102,2,223,223,1006,224,599,1001,223,1,223,107,226,226,224,1002,223,2,223,1006,224,614,1001,223,1,223,1108,677,226,224,1002,223,2,223,1005,224,629,1001,223,1,223,1107,677,677,224,102,2,223,223,1005,224,644,1001,223,1,223,1008,677,677,224,102,2,223,223,1005,224,659,101,1,223,223,1107,677,226,224,1002,223,2,223,1006,224,674,101,1,223,223,4,223,99,226

0 commit comments

Comments
 (0)