-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
63 lines (58 loc) · 1.39 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
import run from 'aocrunner'
const parseInput = (rawInput: string): string[] => rawInput.trim().split('\n')
const items = [...' abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ']
const part1 = (rawInput: string) => {
const input = parseInput(rawInput).map((v) => [
v.substring(0, v.length / 2),
v.substring(v.length / 2, v.length),
])
let prioritySum = 0
for (const sack of input) {
const commonItem = [...sack[0]].find((c) => sack[1].includes(c))
prioritySum += items.indexOf(commonItem!)
}
return prioritySum.toString()
}
const part2 = (rawInput: string) => {
const input = parseInput(rawInput)
let prioritySum = 0
for (let i = 0; i < input.length; i += 3) {
const badgeItem = [...input[i]].find(
(item) => input[i + 1].includes(item) && input[i + 2].includes(item)
)
prioritySum += items.indexOf(badgeItem!)
}
return prioritySum.toString()
}
run({
part1: {
tests: [
{
input: `vJrwpWtwJgWrhcsFMMfFFhFp
jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL
PmmdzqPrVvPwwTWBwg
wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn
ttgJtRGJQctTZtZT
CrZsJsPPZsGzwwsLwLmpwMDw`,
expected: '157',
},
],
solution: part1,
},
part2: {
tests: [
{
input: `vJrwpWtwJgWrhcsFMMfFFhFp
jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL
PmmdzqPrVvPwwTWBwg
wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn
ttgJtRGJQctTZtZT
CrZsJsPPZsGzwwsLwLmpwMDw`,
expected: '70',
},
],
solution: part2,
},
trimTestInputs: true,
// onlyTests: true,
})