-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day25.kt
93 lines (81 loc) · 2.63 KB
/
Day25.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
package aoc2021.day25
import util.Coord
fun computeSeaCucumberMovements(inputMap: Map<Coord, Char>): Int {
var seaCucumbers = inputMap.toMutableMap()
var moves = 0
loop@ while (true) {
val newSeaCucumbers = runStep(seaCucumbers)
moves++
if (newSeaCucumbers == seaCucumbers) {
break@loop
} else {
seaCucumbers = newSeaCucumbers
}
}
return moves
}
fun runStep(seaCucumbers: MutableMap<Coord, Char>): MutableMap<Coord, Char> {
var newSeaCucumbers = mutableMapOf<Coord, Char>()
val eastFacing = seaCucumbers.filter { it.value == '>' }.keys
val southFacing = seaCucumbers.filter { it.value == 'v' }.keys
val (maxX, maxY) = Pair(seaCucumbers.keys.maxOf { it.x }, seaCucumbers.keys.maxOf { it.y })
eastFacing.forEach { efc ->
val nextMove = getNextMove(efc, Direction.EAST, seaCucumbers)
newSeaCucumbers[nextMove] = '>'
}
val seaCucumberMidStep = newSeaCucumbers.toMutableMap()
southFacing.forEach { sf -> seaCucumberMidStep[sf] = 'v' }
newSeaCucumbers = fillEmptySpaces(seaCucumberMidStep, maxX, maxY)
var newSeaCucumberMidStep = mutableMapOf<Coord, Char>()
southFacing.forEach { sfc ->
val nextMove = getNextMove(sfc, Direction.SOUTH, newSeaCucumbers)
newSeaCucumberMidStep[nextMove] = 'v'
}
newSeaCucumbers.filter { it.value == '>' }.keys.forEach { efc ->
newSeaCucumberMidStep[efc] = '>'
}
newSeaCucumberMidStep = fillEmptySpaces(newSeaCucumberMidStep, maxX, maxY)
return newSeaCucumberMidStep
}
fun getNextMove(coord: Coord, direction: Direction, seaCucumbers: Map<Coord, Char>): Coord {
when (direction) {
Direction.EAST -> {
var newEastCoord = Coord(coord.x + 1, coord.y)
if (seaCucumbers[newEastCoord] == null) {
newEastCoord = Coord(0, coord.y)
}
if (seaCucumbers[newEastCoord] == '.') {
return newEastCoord
}
}
Direction.SOUTH -> {
var newSouthCoord = Coord(coord.x, coord.y + 1)
if (seaCucumbers[newSouthCoord] == null) {
newSouthCoord = Coord(coord.x, 0)
}
if (seaCucumbers[newSouthCoord] == '.') {
return newSouthCoord
}
}
}
return coord
}
enum class Direction { EAST, SOUTH }
fun fillEmptySpaces(map: MutableMap<Coord, Char>, maxX: Int, maxY: Int): MutableMap<Coord, Char> {
(0 .. maxY).forEach { y ->
(0 .. maxX).forEach { x ->
if (map[Coord(x, y)] == null) map[Coord(x, y)] = '.'
}
}
return map
}
fun printMap(map: Map<Coord, Char>) {
val (maxX, maxY) = Pair(map.keys.maxOf { it.x }, map.keys.maxOf { it.y })
(0 .. maxY).forEach { y ->
(0 .. maxX).forEach { x ->
print(map[Coord(x, y)])
}
println()
}
println()
}