Skip to content

Commit 12c6823

Browse files
feat[2020-day-01]: multiply matching expenses for part 1 solution
1 parent a623438 commit 12c6823

File tree

3 files changed

+267
-0
lines changed

3 files changed

+267
-0
lines changed

2020/day-01/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require('./solution')

2020/day-01/input.txt

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
1539
2+
1914
3+
1866
4+
1407
5+
1706
6+
1423
7+
1834
8+
1700
9+
1573
10+
1486
11+
1743
12+
1394
13+
1693
14+
1705
15+
1530
16+
1811
17+
1626
18+
1473
19+
1901
20+
1481
21+
1527
22+
1841
23+
1891
24+
1750
25+
1343
26+
1899
27+
401
28+
1896
29+
1627
30+
1593
31+
1541
32+
874
33+
1484
34+
1210
35+
1692
36+
1963
37+
1964
38+
1780
39+
671
40+
1862
41+
1393
42+
1309
43+
1740
44+
1831
45+
1932
46+
1185
47+
1979
48+
1504
49+
1663
50+
1610
51+
1494
52+
1511
53+
1103
54+
1738
55+
1816
56+
1871
57+
1545
58+
1595
59+
1784
60+
1412
61+
1815
62+
1998
63+
1783
64+
1770
65+
1426
66+
1699
67+
1416
68+
1880
69+
1612
70+
1989
71+
1360
72+
1869
73+
1762
74+
1690
75+
1999
76+
1990
77+
1521
78+
1730
79+
703
80+
1463
81+
1670
82+
1472
83+
1413
84+
1669
85+
1502
86+
1548
87+
1475
88+
1694
89+
1314
90+
1980
91+
980
92+
1667
93+
890
94+
1569
95+
1456
96+
1406
97+
1924
98+
1973
99+
1965
100+
1533
101+
1827
102+
2000
103+
1847
104+
1520
105+
1729
106+
1512
107+
1555
108+
1566
109+
1505
110+
1672
111+
1169
112+
1835
113+
1850
114+
1493
115+
1861
116+
1288
117+
1675
118+
1676
119+
1556
120+
1320
121+
1757
122+
1870
123+
1642
124+
1903
125+
1372
126+
1967
127+
1894
128+
176
129+
1908
130+
1418
131+
1535
132+
1487
133+
1496
134+
1491
135+
1611
136+
1970
137+
1758
138+
1563
139+
1766
140+
1629
141+
1937
142+
1763
143+
1829
144+
1772
145+
1632
146+
1517
147+
1736
148+
1971
149+
1721
150+
1716
151+
1429
152+
1408
153+
1560
154+
1958
155+
1359
156+
1890
157+
1825
158+
1536
159+
1819
160+
1697
161+
1887
162+
1832
163+
2005
164+
892
165+
1471
166+
1425
167+
1677
168+
1673
169+
1128
170+
1878
171+
1062
172+
1470
173+
1875
174+
1854
175+
1518
176+
1568
177+
1919
178+
256
179+
1532
180+
1711
181+
1944
182+
1344
183+
1330
184+
1636
185+
1957
186+
1709
187+
1551
188+
1983
189+
1674
190+
1671
191+
1959
192+
1760
193+
1689
194+
1767
195+
1477
196+
1589
197+
1897
198+
1144
199+
1982
200+
1544

2020/day-01/solution.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+
const path = require('path')
3+
const filePath = path.join(__dirname, 'input.txt')
4+
const { validateRecords } = require('./expenseValidation')
5+
const { inputToArray } = require('../../2018/inputParser')
6+
7+
fs.readFile(filePath, { encoding: 'utf8' }, (err, initData) => {
8+
if (err) throw err
9+
10+
initData = inputToArray(initData.trim())
11+
.map(el => Number(el))
12+
13+
const resetInput = () => {
14+
// Deep copy to ensure we aren't mutating the original data
15+
return JSON.parse(JSON.stringify(initData))
16+
}
17+
18+
const part1 = () => {
19+
const data = resetInput()
20+
const results = validateRecords(data)
21+
22+
return results[0] * results[1]
23+
}
24+
25+
// const part2 = ({ target, maxNoun, maxVerb }) => {
26+
// // Helper for running the program with specified noun and verb inputs
27+
// const tryProgram = ({
28+
// noun,
29+
// verb
30+
// }) => {
31+
// const data = resetInput()
32+
// data[1] = noun
33+
// data[2] = verb
34+
// runProgram({ data })
35+
// console.debug(`Running with noun:${noun} and verb:${verb} produces ${data[0]}`)
36+
// return Number(data[0])
37+
// }
38+
39+
// // Manipulate and loop through attempts for Part 2
40+
// let noun = -1
41+
// while (noun <= maxNoun) {
42+
// let verb = -1
43+
// noun++
44+
// while (verb <= maxVerb) {
45+
// verb++
46+
// const output = tryProgram({
47+
// noun,
48+
// verb
49+
// })
50+
// // Break the search loop on success
51+
// if (output === target) {
52+
// return 100 * noun + verb
53+
// }
54+
// }
55+
// }
56+
// }
57+
58+
const answers = []
59+
answers.push(part1())
60+
// answers.push(part2())
61+
62+
answers.forEach((ans, idx) => {
63+
console.info(`-- Part ${idx + 1} --`)
64+
console.info(`Answer: ${ans}`)
65+
})
66+
})

0 commit comments

Comments
 (0)