2020 day 10

This commit is contained in:
tristan 2020-12-10 22:07:29 +00:00
parent 1590f4be6e
commit bb6d22cd13
7 changed files with 259 additions and 0 deletions

104
2020/10/input.txt Normal file
View file

@ -0,0 +1,104 @@
152
18
146
22
28
133
114
67
19
37
66
14
90
163
26
149
71
106
46
143
145
12
151
105
58
130
93
49
74
83
129
122
63
134
86
136
166
169
159
3
178
88
103
97
110
53
125
128
9
15
78
1
50
87
56
89
60
139
113
43
36
118
170
96
135
23
144
153
150
142
95
180
35
179
80
13
115
2
171
32
70
6
72
119
29
79
27
47
107
73
162
172
57
40
48
100
64
59
175
104
156
94
77
65

18
2020/10/part1.js Normal file
View file

@ -0,0 +1,18 @@
const fs = require('fs');
const data = fs.readFileSync('input.txt', 'utf-8');
const lines = data.split('\n').map(num => parseInt(num)).sort((a,b)=>(a-b));
lines.unshift(0); // add the outlet with joltage 0
lines.push(lines[lines.length-1]+3); // your device has a joltage 3 above the highest
let threes = 0; // keep track of differences for puzzle solution
let ones = 0;
for (let i = 1; i < lines.length; i++) {
switch (lines[i]-lines[i-1]) {
case 3:
threes++;
break;
case 1:
ones++;
break;
}
}
console.log(ones * threes)

69
2020/10/part2.js Normal file
View file

@ -0,0 +1,69 @@
const fs = require('fs');
const data = fs.readFileSync('input.txt', 'utf-8');
const lines = data.split('\n').map(num => parseInt(num)).sort((a,b)=>(a-b));
lines.unshift(0);
lines.push(lines[lines.length-1]+3);
// convert to linked list
let last = {};
let i = 0
do {
let entry = {
value: lines[i],
prev: last
};
last.next = entry;
last = entry;
i++;
} while (i < lines.length)
// find removeable adaptors
let removeableAdaptors = [];
const lastVal = last
last = last.prev
while (last.prev) {
if (last.next.value - last.prev.value <= 3){
removeableAdaptors.push(last)
}
last = last.prev
}
// divide removeable adaptors into groups that don't interfere with one another when removed
// this changes the runtime with the given input from a over a month to less than a millisecond
// however, in a worst case scenareo, where every adaptor is removeable, this would still take trillions of years.
let adaptorGroups = []
function makeGroups(removeableAdaptors) {
for (let i = removeableAdaptors.length-1; i>-1; i--) {
// if the gap between two removeable adaptors > 2 then you know they will not intefere with each other
if (i == 0 || removeableAdaptors[i-1].value - removeableAdaptors[i].value > 2) {
adaptorGroups.push(removeableAdaptors.slice(i))
makeGroups(removeableAdaptors.slice(0, i))
break;
}
}
}
makeGroups(removeableAdaptors)
function findArrangements (removeableAdaptors, s = 0) {
total = 1
// for each adaptor in the group
for (let i = s; i < removeableAdaptors.length; i++){
// if you can remove this adaptor
if (removeableAdaptors[i].next.value-removeableAdaptors[i].prev.value <= 3) {
// remove the adaptor and find arrangements with it gone
removeableAdaptors[i].prev.next = removeableAdaptors[i].next
total += findArrangements (removeableAdaptors, i+1)
// add the adaptor back for following tests
removeableAdaptors[i].prev.next = removeableAdaptors[i]
}
}
return total
}
console.time('time')
// multipy the amount of arrangements in each group to get the total amount of arrangements.
answer = adaptorGroups.reduce((a, g) => {return a*findArrangements(g)},1)
console.timeEnd('time')
console.log(answer)

11
2020/10/test1.txt Normal file
View file

@ -0,0 +1,11 @@
16
10
15
5
1
11
7
19
6
12
4

31
2020/10/test2.txt Normal file
View file

@ -0,0 +1,31 @@
28
33
18
42
31
14
46
20
48
47
24
23
49
45
19
38
39
11
1
32
25
35
8
17
7
9
4
2
34
10
3

View file

@ -0,0 +1,25 @@
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

View file

@ -50,6 +50,7 @@ if (advent) {
7. [:star: :star:](https://adventofcode.com/2020/day/7 "see puzzle")
8. [:star: :star:](https://adventofcode.com/2020/day/8 "see puzzle")
9. [:star: :star:](https://adventofcode.com/2020/day/9 "see puzzle")
10. [:star: :star:](https://adventofcode.com/2020/day/10 "see puzzle")
## Languages Used
* Python