-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDay11.kt
48 lines (36 loc) · 1.46 KB
/
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
39
40
41
42
43
44
45
46
47
48
/*
* Copyright (c) 2021 by Todd Ginsberg
*/
/**
* Advent of Code 2021, Day 11 - Dumbo Octopus
* Problem Description: http://adventofcode.com/2021/day/11
* Blog Post/Commentary: https://todd.ginsberg.com/post/advent-of-code/2021/day11/
*/
package com.ginsberg.advent2021
typealias OctopusCave = Map<Point2d, Int>
class Day11(input: List<String>) {
private val startingCave: OctopusCave = parseInput(input)
fun solvePart1(): Int =
startingCave.steps().take(100).sum()
fun solvePart2(): Int =
startingCave.steps().indexOfFirst { it == startingCave.size } + 1
private fun OctopusCave.steps(): Sequence<Int> = sequence {
val cave = this@steps.toMutableMap()
while (true) {
cave.forEach { (point, energy) -> cave[point] = energy + 1 }
do {
val flashersThisRound = cave.filterValues { it > 9 }.keys
flashersThisRound.forEach { cave[it] = 0 }
flashersThisRound
.flatMap { it.allNeighbors() }
.filter { it in cave && cave[it] != 0 }
.forEach { cave[it] = cave.getValue(it) + 1 }
} while (flashersThisRound.isNotEmpty())
yield(cave.count { it.value == 0 })
}
}
private fun parseInput(input: List<String>): OctopusCave =
input.flatMapIndexed { y, row ->
row.mapIndexed { x, energy -> Point2d(x, y) to energy.digitToInt() }
}.toMap()
}