-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
53 lines (45 loc) · 1.36 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
import { Examples, Solution } from '~types'
import { pipe, repeat, transpose, xprod } from 'ramda'
const expandRow = (universe: string[][]): string[][] =>
universe.map((row) => (row.some((c) => c === '#') ? row : repeat('*', row.length)))
const expand = pipe(expandRow, transpose, expandRow, transpose)
export const parse = (input: string) => expand(input.split('\n').map((line) => line.split('')))
const solve =
(n = 2) =>
(input: ReturnType<typeof parse>) => {
const galaxies = input.flatMap((line, y) =>
line.flatMap((c, x) => (c === '#' ? [[x, y]] : [])),
) as [number, number][]
const pairs = xprod(galaxies, galaxies)
return (
pairs.reduce((sum, [[px, py], [qx, qy]]) => {
let [x, y] = [px, py]
while (x !== qx || y !== qy) {
if (x !== qx) x += px > qx ? -1 : 1
else y += py > qy ? -1 : 1
sum += input[y][x] === '*' ? n : 1
}
return sum
}, 0) / 2
)
}
export const p1: Solution<typeof parse> = solve(2)
export const p2: Solution<typeof parse> = solve(1000000)
export const p1ex: Examples = [
{
expected: 374,
input: `
...#......
.......#..
#.........
..........
......#...
.#........
.........#
..........
.......#..
#...#.....
`,
},
]
export const p2ex: Examples = [{ expected: 82000210, input: p1ex[0].input }]