-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday1.js
224 lines (220 loc) · 1.76 KB
/
day1.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// Part 1
function twoNumbers(sumTotal, inputList) {
// const firstNumber = inputList.find(x=>(sumTotal-x in inputList));
// ^ Why does 81 in inputList return true?! Worked on the test lists
const firstNumber = inputList.find(x=>inputList.includes(sumTotal-x));
return firstNumber*(sumTotal-firstNumber);
}
// Part 2
function threeNumbers(sumTotal, inputList) {
const firstNumber = inputList.find(x=>twoNumbers(sumTotal-x,
inputList.slice(inputList.indexOf(x)+1)));
return firstNumber*twoNumbers(sumTotal-firstNumber,
inputList.slice(inputList.indexOf(firstNumber)+1));
}
// Interpret input
function inputToNumList(rawInput) {
return rawInput.split("\n").map(n=>parseInt(n));
}
const input = `1939
1585
1712
1600
1370
1447
1247
1446
1323
1713
1277
1946
1677
1428
1231
1481
1976
1709
1508
1668
1302
77
1351
1605
1999
1982
1583
1756
1957
1624
1745
1938
1784
1403
1642
1691
569
1762
1555
1937
1383
1897
1334
1965
1683
1475
1776
1791
1707
1987
1233
1416
1769
1345
1874
1255
1744
1944
1404
1360
1304
1417
1977
1656
790
1788
1353
1296
1673
1810
1684
1742
1425
1887
1444
1352
1229
1414
1493
1402
1947
1669
1412
1531
1474
1637
1314
1607
1829
1923
1949
1757
1307
1714
1748
1550
1372
1615
1235
1272
1408
1749
1687
1613
1528
1561
341
1308
1660
1667
1313
1991
1675
1394
1704
1303
1440
1592
1857
1752
1839
1397
1699
1426
1878
1759
1814
1096
372
1596
1500
1774
1627
1696
1851
1020
1819
1292
1616
1672
1279
1543
1526
1682
1568
1582
1921
922
1773
1482
1238
1973
1517
1909
409
1634
1468
1445
1801
1631
1407
1820
1603
1495
1333
1241
1849
82
1339
1413
90
1662
1291
1740
1340
1365
2003
1546
1621
1650
1518
1807
1382
1433
1968
1940
1986
1437
1651
1237
1862
1409
1200
2002
2009
1735
1487
1706
1643
1505`;
console.log(twoNumbers(2020, inputToNumList(input)));
console.log(threeNumbers(2020, inputToNumList(input)));