-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay04PrintingDepartment.kt
More file actions
43 lines (36 loc) · 1.56 KB
/
Day04PrintingDepartment.kt
File metadata and controls
43 lines (36 loc) · 1.56 KB
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
package adventofcode.year2025
import adventofcode.Puzzle
import adventofcode.PuzzleInput
import adventofcode.common.spatial.Grid2d
import adventofcode.common.spatial.Point2d
class Day04PrintingDepartment(
customInput: PuzzleInput? = null,
) : Puzzle(customInput) {
private fun parseInput(): Grid2d<Char> = Grid2d(input)
private fun Grid2d<Char>.accessibleRollsOfPaper(removedRolls: Set<Point2d> = emptySet()) =
points
.filter { position -> this[position] == ROLL_OF_PAPER }
.filterNot { position -> position in removedRolls }
.filter { position ->
val adjacentRollsOfPaper =
neighborsOf(position, includeDiagonals = true)
.filterNot { neighbor -> neighbor in removedRolls }
.map { neighbor -> this[neighbor] }
.count { neighbor -> neighbor == ROLL_OF_PAPER }
adjacentRollsOfPaper < MAX_ADJACENT_ROLLS_OF_PAPER
}
override fun partOne() = parseInput().accessibleRollsOfPaper().size
override fun partTwo() =
parseInput()
.let { grid ->
generateSequence(emptySet<Point2d>()) { removedRolls ->
val rollsToRemove = grid.accessibleRollsOfPaper(removedRolls)
(removedRolls + rollsToRemove).takeIf { rollsToRemove.isNotEmpty() }
}
}.last()
.size
companion object {
private const val ROLL_OF_PAPER = '@'
private const val MAX_ADJACENT_ROLLS_OF_PAPER = 4
}
}