Skip to content

Commit 36d19bb

Browse files
committed
day 9: done
1 parent b618df4 commit 36d19bb

File tree

5 files changed

+61
-12
lines changed

5 files changed

+61
-12
lines changed

.submissions.json

+6-6
Original file line numberDiff line numberDiff line change
@@ -123,15 +123,15 @@
123123
},
124124
{
125125
"part1": {
126-
"solved": false,
127-
"result": null,
128-
"time": 0,
126+
"solved": true,
127+
"result": 1584748274,
128+
"time": 1.8013750000000073,
129129
"attempts": []
130130
},
131131
"part2": {
132-
"solved": false,
133-
"result": null,
134-
"time": 0,
132+
"solved": true,
133+
"result": 1026,
134+
"time": 2.163833000000011,
135135
"attempts": []
136136
}
137137
},

README.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
[![Day](https://badgen.net/badge/06/%E2%98%85%E2%98%85/green)](day-06/index.ts)
2020
[![Day](https://badgen.net/badge/07/%E2%98%85%E2%98%85/green)](day-07/index.ts)
2121
[![Day](https://badgen.net/badge/08/%E2%98%85%E2%98%85/green)](day-08/index.ts)
22-
![Day](https://badgen.net/badge/09/%E2%98%86%E2%98%86/gray)
22+
[![Day](https://badgen.net/badge/09/%E2%98%85%E2%98%85/green)](day-09/index.ts)
2323
![Day](https://badgen.net/badge/10/%E2%98%86%E2%98%86/gray)
2424
![Day](https://badgen.net/badge/11/%E2%98%86%E2%98%86/gray)
2525
![Day](https://badgen.net/badge/12/%E2%98%86%E2%98%86/gray)
@@ -103,9 +103,9 @@ Both parts: 8.93 ms
103103

104104
```
105105
Day 09
106-
Time part 1: -
107-
Time part 2: -
108-
Both parts: -
106+
Time part 1: 1.8 ms
107+
Time part 2: 2.16 ms
108+
Both parts: 3.97 ms
109109
```
110110

111111
```
@@ -221,8 +221,8 @@ Both parts: -
221221
```
222222

223223
```
224-
Total stars: 16/50
225-
Total time: 67832.73 ms
224+
Total stars: 18/50
225+
Total time: 67836.7 ms
226226
```
227227

228228
<!--/RESULTS-->

chart.png

224 Bytes
Loading

day-09/index.test.ts

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { expect, test } from 'bun:test'
2+
import { parse } from './index.ts'
3+
4+
test(`parse`, () => {
5+
expect(parse('')).toBeTruthy()
6+
})

day-09/index.ts

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { Examples, Solution } from '~types'
2+
import { curry } from 'ramda'
3+
4+
export const parse = (input: string) => input.replace(/\r/g, '')
5+
6+
export function predict(str: string): number {
7+
const history = str.split(' ').map(Number)
8+
9+
const seq = [history]
10+
11+
while (seq.at(-1).some((n) => n !== 0)) {
12+
const last = seq.at(-1)
13+
const next = last.slice(1).map((v, i) => v - last[i])
14+
15+
seq.push(next)
16+
}
17+
18+
seq.at(-1).push(0)
19+
20+
for (let i = seq.length - 2; i >= 0; i--) {
21+
seq[i].push(seq[i].at(-1) + seq[i + 1].at(-1))
22+
}
23+
24+
return seq[0].at(-1)
25+
}
26+
27+
const getExtrapolatedSum = curry((backwards = false, input: string) =>
28+
input
29+
.split('\n')
30+
.map((line) => (backwards ? line.split(' ').reverse().join(' ') : line))
31+
.map(predict)
32+
.reduce((acc, n) => acc + n, 0),
33+
)
34+
35+
export const p1: Solution<typeof parse> = getExtrapolatedSum
36+
37+
export const p2: Solution<typeof parse> = getExtrapolatedSum(true)
38+
39+
export const p1ex: Examples = [{ expected: 0, input: '' }]
40+
41+
export const p2ex: Examples = [{ expected: 0, input: '' }]
42+
43+
export const onlyEx = true

0 commit comments

Comments
 (0)