-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
135 lines (118 loc) · 3.16 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import run from "aocrunner"
import {
add,
append,
divide,
head,
length,
lensIndex,
lensProp,
match,
modulo,
multiply,
over,
pluck,
range,
reduce,
sort,
view,
} from "ramda"
import { floor } from "ramda-adjunct"
import lcm from "lcm"
type Item = number
type Monkey = {
items: Item[]
op: (x: Item) => Item
test: number
trueMonkey: number
falseMonkey: number
inspections: number
}
type Transfer = [Item, number]
const square = (x: number) => x * x
const double = (x: number) => x + x
const parseMonkeys = (input: string): Monkey[] =>
input
.trim()
.split("\n\n")
.map((block) => {
const lines = block.split("\n").map((x) => x.trim())
const items = match(/\d+/g, lines[1]).map((x) => parseInt(x, 10))
const opNum = head(match(/\d+/g, lines[2]).map((x) => parseInt(x, 10)))
const isMult = lines[2].includes("*")
const op = isMult ? (opNum ? multiply(opNum) : square) : opNum ? add(opNum) : double
const test = match(/\d+/g, lines[3]).map((x) => parseInt(x, 10))[0]
const trueMonkey = match(/\d+/g, lines[4]).map((x) => parseInt(x, 10))[0]
const falseMonkey = match(/\d+/g, lines[5]).map((x) => parseInt(x, 10))[0]
return {
items,
op,
test,
trueMonkey,
falseMonkey,
inspections: 0,
}
})
const runMonkey = (monkey: Monkey, lcm: number): Transfer[] =>
monkey.items.reduce((acc, item) => {
const result = lcm ? modulo(monkey.op(item), lcm) : floor(divide(monkey.op(item), 3))
return append(
[result, modulo(result, monkey.test) ? monkey.falseMonkey : monkey.trueMonkey],
acc,
)
}, [] as Transfer[])
const resetMonkey =
(inspections: number) =>
(monkey: Monkey): Monkey => ({
...monkey,
inspections: inspections + monkey.inspections,
items: [],
})
const runMonkeys = (monkeys: Monkey[], lcm: number): Monkey[] =>
monkeys.reduce((acc, _, idx) => {
const transfers = runMonkey(view(lensIndex(idx), acc), lcm)
return over(
lensIndex(idx),
resetMonkey(length(transfers)),
transfers.reduce(
(acc2, [item, monkeyIdx]) =>
over(lensIndex(monkeyIdx), over(lensProp("items"), append(item)), acc2),
acc,
),
)
}, monkeys)
const simulate = (monkeys: Monkey[], rounds: number, lcm: number): Monkey[] =>
reduce((acc, _) => runMonkeys(acc, lcm), monkeys, range(0, rounds))
const monkeyBusiness = (monkeys: Monkey[]): number => {
const top: number[] = sort((l, r) => r - l, pluck("inspections", monkeys))
return multiply(top[0], top[1])
}
async function part1(input: string): Promise<number> {
return monkeyBusiness(simulate(parseMonkeys(input), 20, 0))
}
async function part2(input: string): Promise<number> {
const monkeys = parseMonkeys(input)
return monkeyBusiness(simulate(monkeys, 10000, pluck("test", monkeys).reduceRight(lcm, 1)))
}
run({
part1: {
tests: [
// {
// input: ``,
// expected: "",
// },
],
solution: part1,
},
part2: {
tests: [
// {
// input: ``,
// expected: "",
// },
],
solution: part2,
},
trimTestInputs: true,
onlyTests: false,
})