-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
80 lines (73 loc) · 1.44 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
import run from 'aocrunner'
type NumberContainer = { n: number }
const parseInput = (rawInput: string): NumberContainer[] =>
rawInput
.trim()
.split('\n')
.map((line) => ({ n: +line }))
const mix = (input: NumberContainer[], times = 1) => {
const mixed = [...input]
for (let i = 0; i < times; i++) {
for (const number of input) {
const mixedIndex = mixed.indexOf(number)
mixed.splice(mixedIndex, 1)
const newIndex = (mixedIndex + number.n) % mixed.length
mixed.splice(newIndex, 0, number)
}
}
return mixed
}
const getCoordinates = (mixed: NumberContainer[]) => {
const theZeroIndex = mixed.findIndex(({ n }) => n === 0)
return (
mixed[(theZeroIndex + 1000) % mixed.length].n +
mixed[(theZeroIndex + 2000) % mixed.length].n +
mixed[(theZeroIndex + 3000) % mixed.length].n
).toString()
}
const part1 = (rawInput: string) => {
const input = parseInput(rawInput)
const mixed = mix(input)
return getCoordinates(mixed)
}
const part2 = (rawInput: string) => {
const input = parseInput(rawInput)
const decryptKey = 811589153
const realInput = input.map(({ n }) => ({ n: n * decryptKey }))
const mixed = mix(realInput, 10)
return getCoordinates(mixed)
}
run({
part1: {
tests: [
{
input: `1
2
-3
3
-2
0
4`,
expected: '3',
},
],
solution: part1,
},
part2: {
tests: [
{
input: `1
2
-3
3
-2
0
4`,
expected: '1623178306',
},
],
solution: part2,
},
trimTestInputs: true,
// onlyTests: true,
})