mirror of
https://github.com/seigler/advent-of-code-2020
synced 2025-07-27 08:16:08 +00:00
Day 10
This commit is contained in:
parent
593288b359
commit
6aaf867952
3 changed files with 193 additions and 0 deletions
102
solutions/day10/input.txt
Normal file
102
solutions/day10/input.txt
Normal file
|
@ -0,0 +1,102 @@
|
|||
153
|
||||
17
|
||||
45
|
||||
57
|
||||
16
|
||||
147
|
||||
39
|
||||
121
|
||||
75
|
||||
70
|
||||
85
|
||||
134
|
||||
128
|
||||
115
|
||||
51
|
||||
139
|
||||
44
|
||||
65
|
||||
119
|
||||
168
|
||||
122
|
||||
72
|
||||
105
|
||||
31
|
||||
103
|
||||
89
|
||||
154
|
||||
114
|
||||
55
|
||||
25
|
||||
48
|
||||
38
|
||||
132
|
||||
157
|
||||
84
|
||||
71
|
||||
113
|
||||
143
|
||||
83
|
||||
64
|
||||
109
|
||||
129
|
||||
120
|
||||
100
|
||||
151
|
||||
79
|
||||
125
|
||||
22
|
||||
161
|
||||
167
|
||||
19
|
||||
26
|
||||
118
|
||||
142
|
||||
4
|
||||
158
|
||||
11
|
||||
35
|
||||
56
|
||||
18
|
||||
40
|
||||
7
|
||||
150
|
||||
99
|
||||
54
|
||||
152
|
||||
60
|
||||
27
|
||||
164
|
||||
78
|
||||
47
|
||||
82
|
||||
63
|
||||
46
|
||||
91
|
||||
32
|
||||
135
|
||||
3
|
||||
108
|
||||
10
|
||||
159
|
||||
127
|
||||
69
|
||||
110
|
||||
126
|
||||
133
|
||||
28
|
||||
15
|
||||
104
|
||||
138
|
||||
160
|
||||
98
|
||||
90
|
||||
144
|
||||
1
|
||||
2
|
||||
92
|
||||
41
|
||||
86
|
||||
66
|
||||
95
|
||||
12
|
48
solutions/day10/solution.js
Normal file
48
solutions/day10/solution.js
Normal file
|
@ -0,0 +1,48 @@
|
|||
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 => 1 * x).sort((a, b) => b - a)
|
||||
input.push(0)
|
||||
input.unshift(input[0] + 3)
|
||||
|
||||
solveForFirstStar(input)
|
||||
solveForSecondStar(input)
|
||||
}
|
||||
|
||||
function solveForFirstStar (input) {
|
||||
let ones = 0
|
||||
let threes = 0
|
||||
input.forEach((s, i, S) => {
|
||||
const diff = s - S[i + 1]
|
||||
if (diff === 1) {
|
||||
ones++
|
||||
}
|
||||
if (diff === 3) {
|
||||
threes++
|
||||
}
|
||||
})
|
||||
const solution = ones * threes
|
||||
report('Solution 1:', solution)
|
||||
}
|
||||
|
||||
function solveForSecondStar (input) {
|
||||
const choices = []
|
||||
input.forEach((s, i, S) => {
|
||||
choices[i] = i === 0 ? 1 : 0
|
||||
for (let j = 1; ; j++) {
|
||||
if (S[i - j] - s <= 3) {
|
||||
choices[i] += choices[i - j]
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
const solution = choices[choices.length - 1]
|
||||
report('Solution 2:', solution)
|
||||
}
|
||||
|
||||
run()
|
43
solutions/day10/viewer.html
Normal file
43
solutions/day10/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>
|
Loading…
Add table
Add a link
Reference in a new issue