-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day11.kt
38 lines (33 loc) · 1000 Bytes
/
Day11.kt
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
package com.github.ephemient.aoc2023
class Day11(input: String) {
private val xs: List<Int>
private val ys: List<Int> = buildList {
xs = input.lines().map { line ->
var count = 0
for ((i, char) in line.withIndex()) {
if (char == '#') {
while (lastIndex < i) add(0)
this[i]++
count++
}
}
count
}
}
fun part1(): Long = solve(2)
fun part2(): Long = solve(1000000)
internal fun solve(n: Long): Long = solve1(xs, n) + solve1(ys, n)
private fun solve1(data: List<Int>, n: Long): Long {
var total = 0L
for ((i, a) in data.withIndex()) {
if (a == 0) continue
var m = 0L
for (j in i + 1..data.lastIndex) {
val b = data[j]
m += if (b == 0) n else 1
total += m * a * b
}
}
return total
}
}