-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
61 lines (57 loc) · 1.51 KB
/
index.ts
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
import run from "aocrunner"
import { chain, join, lensIndex, map, pipe, repeat, set, split, splitEvery, trim } from "ramda"
import { inRange } from "ramda-adjunct"
const getInstructions = pipe(
trim,
split("\n"),
map(trim),
chain((x) => (x.startsWith("addx") ? ["addx 0", x] : [x])),
map(split(" ")),
)
async function part1(input: string): Promise<number> {
const [x, _] = getInstructions(input).reduce(
([strength, register], x, idx) => [
(idx + 1 - 20) % 40 === 0 ? strength + (idx + 1) * register : strength,
x.length === 1 ? register : register + parseInt(x[1], 10),
],
[0, 1],
)
return x
}
async function part2(input: string): Promise<string> {
const [_, crt] = getInstructions(input).reduce(
([register, crt], x, idx) => {
const [row, col] = [Math.floor(idx / 40), idx % 40]
const draw = inRange(register - 1, register + 2, col) ? "█" : " "
return [
x.length === 1 ? register : register + parseInt(x[1], 10),
set(lensIndex(row), set(lensIndex(col), draw, crt[row]), crt),
]
},
[1, splitEvery(40, repeat(" ", 40 * 6))] as [number, string[][]],
)
console.log("Part 2:", "\n" + join("\n", map(join(""), crt)))
return "PAPJCBHP"
}
run({
part1: {
tests: [
// {
// input: ``,
// expected: "",
// },
],
solution: part1,
},
part2: {
tests: [
// {
// input: ``,
// expected: "",
// },
],
solution: part2,
},
trimTestInputs: true,
onlyTests: false,
})