mirror of
https://github.com/seigler/advent-of-code-2020
synced 2025-07-25 23:36:10 +00:00
Day 1 solved
This commit is contained in:
parent
f3d555f01c
commit
93aea4f013
5 changed files with 291 additions and 1 deletions
2
.replit
Normal file
2
.replit
Normal file
|
@ -0,0 +1,2 @@
|
|||
language = "nodejs"
|
||||
run = "npm start"
|
200
solutions/day1/input.txt
Normal file
200
solutions/day1/input.txt
Normal file
|
@ -0,0 +1,200 @@
|
|||
1865
|
||||
1179
|
||||
1328
|
||||
1390
|
||||
322
|
||||
1999
|
||||
1713
|
||||
1808
|
||||
1380
|
||||
1727
|
||||
1702
|
||||
1304
|
||||
1481
|
||||
1334
|
||||
1728
|
||||
1953
|
||||
1413
|
||||
1753
|
||||
1723
|
||||
1379
|
||||
1532
|
||||
1918
|
||||
1490
|
||||
71
|
||||
1388
|
||||
1519
|
||||
807
|
||||
1427
|
||||
1729
|
||||
1952
|
||||
970
|
||||
1405
|
||||
1500
|
||||
1782
|
||||
1899
|
||||
1501
|
||||
1720
|
||||
1832
|
||||
1706
|
||||
1658
|
||||
1333
|
||||
486
|
||||
1670
|
||||
1674
|
||||
1290
|
||||
1893
|
||||
1382
|
||||
1761
|
||||
1945
|
||||
1607
|
||||
1319
|
||||
1508
|
||||
1464
|
||||
1733
|
||||
1917
|
||||
1483
|
||||
1620
|
||||
1677
|
||||
1835
|
||||
1810
|
||||
1385
|
||||
1840
|
||||
1705
|
||||
1994
|
||||
1695
|
||||
1599
|
||||
1681
|
||||
1566
|
||||
1153
|
||||
1672
|
||||
1373
|
||||
1679
|
||||
1714
|
||||
1283
|
||||
1950
|
||||
1197
|
||||
1696
|
||||
1936
|
||||
1218
|
||||
1910
|
||||
1786
|
||||
958
|
||||
1565
|
||||
1583
|
||||
1914
|
||||
1853
|
||||
1682
|
||||
1267
|
||||
1543
|
||||
1322
|
||||
1965
|
||||
1406
|
||||
860
|
||||
1754
|
||||
1867
|
||||
1393
|
||||
1404
|
||||
1191
|
||||
1861
|
||||
2007
|
||||
1613
|
||||
1938
|
||||
1340
|
||||
1227
|
||||
1628
|
||||
1826
|
||||
1638
|
||||
1970
|
||||
1993
|
||||
1748
|
||||
496
|
||||
1661
|
||||
1736
|
||||
1593
|
||||
1298
|
||||
1882
|
||||
1763
|
||||
1616
|
||||
1848
|
||||
92
|
||||
1338
|
||||
1707
|
||||
1587
|
||||
1996
|
||||
1708
|
||||
700
|
||||
1460
|
||||
1673
|
||||
1450
|
||||
1815
|
||||
1537
|
||||
1825
|
||||
1683
|
||||
1743
|
||||
1949
|
||||
1933
|
||||
1289
|
||||
1905
|
||||
1307
|
||||
1845
|
||||
1855
|
||||
1955
|
||||
1554
|
||||
1570
|
||||
1931
|
||||
1780
|
||||
1929
|
||||
1980
|
||||
1978
|
||||
1998
|
||||
1371
|
||||
1981
|
||||
1817
|
||||
1722
|
||||
1545
|
||||
1561
|
||||
1352
|
||||
1935
|
||||
1553
|
||||
1796
|
||||
1847
|
||||
1640
|
||||
1414
|
||||
1198
|
||||
1669
|
||||
1909
|
||||
1879
|
||||
1744
|
||||
1783
|
||||
1367
|
||||
1632
|
||||
1990
|
||||
1937
|
||||
1919
|
||||
1431
|
||||
2002
|
||||
1603
|
||||
1948
|
||||
1317
|
||||
1282
|
||||
1349
|
||||
1839
|
||||
1575
|
||||
1730
|
||||
1849
|
||||
1959
|
||||
1971
|
||||
2009
|
||||
1641
|
||||
1402
|
||||
1924
|
||||
1757
|
||||
1605
|
||||
1693
|
||||
1762
|
||||
283
|
||||
1525
|
||||
1964
|
||||
1715
|
||||
1602
|
45
solutions/day1/solution.js
Normal file
45
solutions/day1/solution.js
Normal file
|
@ -0,0 +1,45 @@
|
|||
const path = require('path')
|
||||
const { read, position } = require('promise-path')
|
||||
const fromHere = position(__dirname)
|
||||
const report = (...messages) => console.log(`[${require(fromHere('../../package.json')).logName} / ${__dirname.split(path.sep).pop()}]`, ...messages)
|
||||
|
||||
async function run () {
|
||||
const input = (await read(fromHere('input.txt'), 'utf8')).trim().split('\n').map(x => parseInt(x, 10))
|
||||
|
||||
await solveForFirstStar(input)
|
||||
await solveForSecondStar(input)
|
||||
}
|
||||
|
||||
function findSum (array, startIndex, sum, count = 2) {
|
||||
for (let i = 0; i < array.length; i++) {
|
||||
const n = array[i]
|
||||
if (count > 2) {
|
||||
const searchResult = findSum(array, i + 1, sum - n, count - 1)
|
||||
if (searchResult) {
|
||||
return [n, ...searchResult]
|
||||
}
|
||||
} else if (count === 2) {
|
||||
for (let j = startIndex + 1; j < array.length; j++) {
|
||||
if (n + array[j] === sum) {
|
||||
return [n, array[j]]
|
||||
}
|
||||
}
|
||||
} else {
|
||||
throw new Error('findSum: Invalid count')
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
async function solveForFirstStar (input) {
|
||||
const solution = findSum(input, 0, 2020, 2).reduce((acc, cur) => acc * cur, 1)
|
||||
report('Input:', input)
|
||||
report('Solution 1:', solution)
|
||||
}
|
||||
|
||||
async function solveForSecondStar (input) {
|
||||
const solution = findSum(input, 0, 2020, 3).reduce((acc, cur) => acc * cur, 1)
|
||||
report('Solution 2:', solution)
|
||||
}
|
||||
|
||||
run()
|
43
solutions/day1/viewer.html
Normal file
43
solutions/day1/viewer.html
Normal file
|
@ -0,0 +1,43 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Solution Viewer</title>
|
||||
<style>
|
||||
html, body { font-family: sans-serif; }
|
||||
pre { border-radius: 0.5em; padding: 0.5em; background: #eee; }
|
||||
</style>
|
||||
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
|
||||
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
|
||||
</head>
|
||||
<body>
|
||||
<div id="viewer">
|
||||
<h1>Solution Viewer ({{ solutionTitle }})</h1>
|
||||
<p>For interesting problems; this page can be used as a dynamic viewer.</p>
|
||||
<h3><a href="./input.txt">input.txt</a></h3>
|
||||
<pre><code>{{ inputText }}</code></pre>
|
||||
<h3><a href="./solution.js">solution.js</a></h3>
|
||||
<pre><code>{{ solutionText }}</code></pre>
|
||||
</div>
|
||||
<script>
|
||||
const app = new Vue({
|
||||
el: '#viewer',
|
||||
data: () => {
|
||||
return {
|
||||
solutionText: '[Loading]',
|
||||
inputText: '[Loading]'
|
||||
}
|
||||
},
|
||||
computed: {
|
||||
solutionTitle() {
|
||||
const parts = (document.location + '').split('/')
|
||||
return parts.reverse()[1]
|
||||
}
|
||||
},
|
||||
async mounted () {
|
||||
this.solutionText = (await axios.get('./solution.js')).data
|
||||
this.inputText = (await axios.get('./input.txt')).data
|
||||
}
|
||||
})
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
|
@ -12,7 +12,7 @@ async function run () {
|
|||
|
||||
async function solveForFirstStar (input) {
|
||||
const solution = 'UNSOLVED'
|
||||
report('Input:', input)
|
||||
// report('Input:', input);
|
||||
report('Solution 1:', solution)
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue