-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
150 lines (140 loc) · 2.88 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import run from 'aocrunner'
type XY = [number, number]
type Elf = {
at: XY
propose: string | null
}
const parseInput = (rawInput: string) => {
const elves: Map<string, Elf> = new Map()
rawInput
.trim()
.split('\n')
.forEach((line, y) => {
;[...line].forEach((char, x) => {
if (char === '#') elves.set(grid([x, y]), { at: [x, y], propose: null })
})
})
return elves
}
const grid = (xy: XY) => xy[0] + ':' + xy[1]
const xy = (grid: string): XY => grid.split(':').map((v) => +v) as XY
const norths: XY[] = [
[-1, -1],
[0, -1],
[1, -1],
]
const souths: XY[] = [
[-1, 1],
[0, 1],
[1, 1],
]
const wests: XY[] = [
[-1, -1],
[-1, 0],
[-1, 1],
]
const easts: XY[] = [
[1, -1],
[1, 0],
[1, 1],
]
const allAround: XY[] = [...norths, ...souths, ...wests, ...easts]
const addGrid = (a: XY, b: XY): XY => [a[0] + b[0], a[1] + b[1]]
const checkEmpty = (elves: Map<string, Elf>, from: XY, dirs: XY[]) =>
dirs.every((dir) => !elves.has(grid(addGrid(from, dir))))
const doRound = (elves: Map<string, Elf>, dirChecks: XY[][]): boolean => {
let settled = true
const proposeMap: Map<string, number> = new Map()
for (const [, elf] of elves) {
const alone = checkEmpty(elves, elf.at, allAround)
if (alone) continue
settled = false
for (const dirCheck of dirChecks) {
if (checkEmpty(elves, elf.at, dirCheck)) {
elf.propose = grid(addGrid(elf.at, dirCheck[1]))
proposeMap.set(elf.propose, (proposeMap.get(elf.propose) ?? 0) + 1)
break
}
}
}
if (settled) return true
for (const [elfPrevGrid, elf] of elves) {
if (elf.propose === null) continue
if (proposeMap.get(elf.propose)! === 1) {
elf.at = xy(elf.propose)
elves.delete(elfPrevGrid)
elves.set(grid(elf.at), elf)
}
elf.propose = null
}
dirChecks.push(dirChecks.shift()!)
return false
}
const part1 = (rawInput: string) => {
const elves = parseInput(rawInput)
const dirChecks = [norths, souths, wests, easts]
for (let i = 0; i < 10; i++) {
doRound(elves, dirChecks)
}
let minX = Infinity
let minY = Infinity
let maxX = -Infinity
let maxY = -Infinity
for (const [
,
{
at: [x, y],
},
] of elves) {
if (x < minX) minX = x
if (x > maxX) maxX = x
if (y < minY) minY = y
if (y > maxY) maxY = y
}
const rectArea = (maxX - minX + 1) * (maxY - minY + 1)
return (rectArea - elves.size).toString()
}
const part2 = (rawInput: string) => {
const elves = parseInput(rawInput)
const dirChecks = [norths, souths, wests, easts]
let round = 1
while (true) {
const settled = doRound(elves, dirChecks)
if (settled) return round.toString()
round++
}
}
run({
part1: {
tests: [
{
input: `....#..
..###.#
#...#.#
.#...##
#.###..
##.#.##
.#..#..`,
expected: '110',
},
],
solution: part1,
},
part2: {
tests: [
{
input: `....#..
..###.#
#...#.#
.#...##
#.###..
##.#.##
.#..#..`,
expected: '20',
},
],
solution: part2,
},
trimTestInputs: true,
// onlyTests: true,
})