/
Day23.kt
167 lines (131 loc) · 5.61 KB
/
Day23.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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package com.akikanellis.adventofcode.year2022
import com.akikanellis.adventofcode.year2022.utils.Point
import com.akikanellis.adventofcode.year2022.utils.circularListIterator
object Day23 {
fun numberOfEmptyGroundTiles(input: String): Int {
var grove = Grove.of(input)
val directions = Direction.orderedDirections.circularListIterator()
repeat(10) { grove = grove.moveElves(directions.next()) }
return grove.emptyGroundTiles()
}
fun roundWhereNoElfMoves(input: String): Int {
var grove = Grove.of(input)
val directions = Direction.orderedDirections.circularListIterator()
var round = 0
do {
round++
grove = grove.moveElves(directions.next())
} while (grove.numberOfElvesThatMoved != 0)
return round
}
private data class Grove(
val elves: List<Elf>,
val numberOfElvesThatMoved: Int = 0
) {
private val positionToElf = elves.associateBy { it.position }
private val minX = elves.minOf { it.x }
private val maxX = elves.maxOf { it.x }
private val minY = elves.minOf { it.y }
private val maxY = elves.maxOf { it.y }
fun hasElfInAny(positions: List<Point>) = positions.any { it in positionToElf }
fun emptyGroundTiles(): Int {
val totalNumberOfTiles = (maxX - minX + 1) * (maxY - minY + 1)
return totalNumberOfTiles - elves.size
}
fun moveElves(direction: Direction): Grove {
val elvesToProposedPositions = elves
.map { elf -> Pair(elf, elf.proposedPosition(direction, this)) }
val numberOfElvesThatMoved = elvesToProposedPositions
.count { (elf, proposedPosition) -> elf.position != proposedPosition }
val duplicatePositions = elvesToProposedPositions
.map { (_, proposedPosition) -> proposedPosition }
.groupingBy { proposedPosition -> proposedPosition }
.eachCount()
.filter { (_, numberOfDuplicates) -> numberOfDuplicates > 1 }
.keys
val (movingElves, stationaryElves) = elvesToProposedPositions
.partition { (_, proposedPosition) -> proposedPosition !in duplicatePositions }
val movedElves = movingElves.map { (_, proposedPosition) -> Elf(proposedPosition) }
val stationedElves = stationaryElves.map { (originalElf, _) -> originalElf }
return Grove(
elves = movedElves + stationedElves,
numberOfElvesThatMoved = numberOfElvesThatMoved
)
}
companion object {
fun of(representation: String) = representation
.lines()
.flatMapIndexed { y, row ->
row.mapIndexedNotNull { x, tileType ->
if (tileType == '#') {
Elf(Point(x, y))
} else {
null
}
}
}
.let { Grove(it) }
}
}
private data class Elf(val position: Point) {
val x = position.x
val y = position.y
fun proposedPosition(startingDirection: Direction, grove: Grove): Point {
if (!grove.hasElfInAny(Direction.allAdjacentPositions(position))) return position
val directions = startingDirection.directionsOrder
for (direction in directions) {
if (!grove.hasElfInAny(direction.adjacentPositions(position))) {
return direction.nextPosition(position)
}
}
return position
}
}
private enum class Direction {
N {
override val directionsOrder by lazy { listOf(N, S, W, E) }
override fun adjacentPositions(position: Point) = listOf(
position.minusY(),
position.minusY().plusX(),
position.minusY().minusX()
)
override fun nextPosition(position: Point) = position.minusY()
},
S {
override val directionsOrder by lazy { listOf(S, W, E, N) }
override fun adjacentPositions(position: Point) = listOf(
position.plusY(),
position.plusY().plusX(),
position.plusY().minusX()
)
override fun nextPosition(position: Point) = position.plusY()
},
W {
override val directionsOrder by lazy { listOf(W, E, N, S) }
override fun adjacentPositions(position: Point) = listOf(
position.minusX(),
position.minusY().minusX(),
position.plusY().minusX()
)
override fun nextPosition(position: Point) = position.minusX()
},
E {
override val directionsOrder by lazy { listOf(E, N, S, W) }
override fun adjacentPositions(position: Point) = listOf(
position.plusX(),
position.minusY().plusX(),
position.plusY().plusX()
)
override fun nextPosition(position: Point) = position.plusX()
};
abstract val directionsOrder: List<Direction>
abstract fun adjacentPositions(position: Point): List<Point>
abstract fun nextPosition(position: Point): Point
companion object {
val orderedDirections = N.directionsOrder
fun allAdjacentPositions(position: Point) = orderedDirections
.flatMap { it.adjacentPositions(position) }
.distinct()
}
}
}