Skip to content

Commit

Permalink
day 23
Browse files Browse the repository at this point in the history
  • Loading branch information
kgeri committed Dec 23, 2022
1 parent 33485bc commit 1f8c4ed
Show file tree
Hide file tree
Showing 2 changed files with 157 additions and 0 deletions.
82 changes: 82 additions & 0 deletions src/main/kotlin/me/gergo/Aoc23.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package me.gergo

import java.io.File

fun main() {
val elves = File("src/main/resources/input23.txt")
.readLines()
.mapIndexed { y, line ->
line.mapIndexedNotNull { x, c -> if (c == '#') Coordinate(x, y) else null }
}.flatten()
.toHashSet()
val simulateUntil = Int.MAX_VALUE

val directions = listOf(::canMoveNorth, ::canMoveSouth, ::canMoveWest, ::canMoveEast)
val moves = listOf(Coordinate::north, Coordinate::south, Coordinate::west, Coordinate::east)

val proposals = mutableMapOf<Coordinate, MutableList<Coordinate>>()
fun propose(target: Coordinate, elf: Coordinate) = proposals.computeIfAbsent(target) { mutableListOf() }.add(elf)

var standing: Int
for (i in 0 until simulateUntil) {

// Proposal round
proposals.clear()
standing = 0
for (elf in elves) {
val canMove = directions.map { it(elf, elves) }
if (canMove.all { it }) standing++ // Could move anywhere, so we don't
else {
for (d in canMove.indices) { // Trying to move in priority order
if (canMove[(i + d) % directions.size]) {
val move = moves[(i + d) % moves.size]
propose(move(elf), elf)
break
}
}
}
}

if (standing == elves.size) {
println("Elves stopped moving at round ${i + 1}")
break
}

// Move round
proposals
.filter { it.value.size == 1 }
.forEach { (target, proposers) ->
elves.remove(proposers[0])
elves.add(target)
}
}

// Calculating the rectangle
val minX = elves.minOf { it.x }
val minY = elves.minOf { it.y }
val maxX = elves.maxOf { it.x }
val maxY = elves.maxOf { it.y }
val result1 = (maxX - minX + 1) * (maxY - minY + 1) - elves.size
println("Empty ground tiles after round 10: $result1")
}

private fun Coordinate.north() = Coordinate(x, y - 1)
private fun Coordinate.south() = Coordinate(x, y + 1)
private fun Coordinate.west() = Coordinate(x - 1, y)
private fun Coordinate.east() = Coordinate(x + 1, y)

private fun canMoveNorth(c: Coordinate, elves: Set<Coordinate>) = !elves.contains(Coordinate(c.x - 1, c.y - 1))
&& !elves.contains(Coordinate(c.x, c.y - 1))
&& !elves.contains(Coordinate(c.x + 1, c.y - 1))

private fun canMoveSouth(c: Coordinate, elves: Set<Coordinate>) = !elves.contains(Coordinate(c.x - 1, c.y + 1))
&& !elves.contains(Coordinate(c.x, c.y + 1))
&& !elves.contains(Coordinate(c.x + 1, c.y + 1))

private fun canMoveEast(c: Coordinate, elves: Set<Coordinate>) = !elves.contains(Coordinate(c.x + 1, c.y - 1))
&& !elves.contains(Coordinate(c.x + 1, c.y))
&& !elves.contains(Coordinate(c.x + 1, c.y + 1))

private fun canMoveWest(c: Coordinate, elves: Set<Coordinate>) = !elves.contains(Coordinate(c.x - 1, c.y - 1))
&& !elves.contains(Coordinate(c.x - 1, c.y))
&& !elves.contains(Coordinate(c.x - 1, c.y + 1))
75 changes: 75 additions & 0 deletions src/main/resources/input23.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
...#######...####..#........#.#.##...##.##..#.##.####..####.##.##...###..##
##.###.#...##..#....###..#.##.###.##..##..###..#....##.#..##.####..#.#.#.##
..##.#.###.#......#...#....#...#.#.#...##......####..######.##....#####.##.
##.#####.#.###.####.#....#.....####.#####..##.....#.##...###...#.####..#...
...#.....#.#..#.######.###....#.....#.###..##..#.#.###..##..#.#......#..#..
.###.#.##.##.##.....##.........#....#.#.#..#....###..####..#.#.###..###..#.
.#####.......##.#..#...###.##...#.###...#..##.#.###.......##.#..#..##...##.
.###.#.#.###.#.#..#..###..###..###.########.###..#.##.#.##...##.##.###.#...
.....######.###.###.......####...##.#.##..#.#.##.#....##.###.#.###...###..#
..#..##...#..#...##.#.#..###...#.##.##.##..##.###.#.#..#...#.#..#......####
##..##.##..#.#.##...#.#.#...###.....#....###....#######.#####..#...#.##..##
####....######.#..##...#....#.##....##.#..#.##..##..#.#.....##..####..#....
....####..#.......##...#........#.##..#.#.#.##.##..##.##.....##..#...#.##.#
.####..######.##.#.#.#.##...###..##...######..#.#.#.####.######.#...###....
##..#.#...#.#.#.#.###.#.###.#.#.##.....#..##..##.##...###....#...#.###...#.
#####.##...###..#.#.######..#.###...###..#..###.....#....####..#.####...###
#.#.#....###.##...#.###.#.###.###..###.#.#.....#.##....#.##.##.#####..##..#
...##.#...##....####.#.##.#.####.###...#.##....#..##.###..#......#######.##
....#..#.#.#...##....#.####.##...#..#####...#.##..###..##...#..#.##.#.#.#.#
..#..#####.#..#.#.#..#.###.##...#........#.#.....#.##.....#.##.#..#.#.#.#..
..#.##.###....##.#..#.#####.....#.##.....#.#..###.###..#.#......#..#...#.##
##.#.#...#...#.##..#......###.#.#.####.#...#########.#..#..##...##...##.###
...#.###.#..#######.#.#.##.##..###.##.#.######.#.##.##.##.#########.#...#.#
#..#...##.##.######.#..#.####...####.###.#...##.####..##..#.#...##.#.#.##..
.##.###..#####.###.##..#.#.#.#....#.#....#...#....######.##.#...#...#.#...#
#..#.....##....#.#..##.###..#..####..######...##.#..#######....#.#..##.##..
##...#.##.#...#....#.##.##..#####...##.#.#.#.#...#...#.#....#.##..##..##..#
..##....#..###...#.#.#.##....#...##...###.##.#.....#..#####.#####.....##..#
.#.##..#.#.###..#...###.....##...###..#.#######.#..#.#.##.##..##.#.#..##.##
#.#...#..###..#...####.#.#..##.#..#....#.#.#.###.#.##.##.#.......#.##....##
.#.##.#.#..#.#.#.#.##..#.###..#....#..#...###.#.#.##..#.##..#..#.#..#...###
..#..#.##......#....##.#######..#..##.....#.#.##..#..#..#.#.#..#.#..#.##...
#.#.###.###..######.##....#####.####..#....##.##.##.....##..#..###...##..##
.##..#.#.###..##.....###.#.##.#..#.....#.##..#...#...###...#...##...#####.#
.....#....######..##.#..##..#...##.#.##.#..##...####.##..#.#.####.###......
#..##....#.#.#.#....##..#....##...##.#...###.##..#....#.#...#.#..###...#..#
...###.#.####..##...###.#.##.#..#.####.#.#.#####....####.#.#.#.##...#.##...
.#..##.#...#..##.######.#######.#.#...##...##..#.#.########..#..##.#..###..
..#####.####.#.#.##........###...#..###...##...#...#....####...##.#.#######
#.#####......###.#..###.....##..##...######.#..##.#.#.#######..##.####...#.
#.#.#.#..##..#.##..###....##.#.##.####..#.#####..###......#...#..#..###....
..#...#.##..##.#..#####.######...###...#...##..#.#####..#.###....#..#.#.#..
###..#.#####.##.#.######..##.#####...#...#####.#.#.#.#...#.#.#.#....##....#
#.#...###...#.#...####..........#####..###..#.#...####...#.#.###.#..####.##
..#..##.###.##...####.#....#..##.....####.##.#.#....#..#..##.#..##.#.#..###
..#......##.#.####..###.####..##.####.##...#.#.####.#.#..#..######.##.##.##
....###...#.##.####....#...#.#..#..##.#.####...##...#...#.#.##...##.##..#.#
######....#.#..####.#.#...#####.#.#..##.######.#.###.#.#.#....#...#....##..
....####...#..#.#.#.####.#..#..###..#..#.##.########...#..#.#.........#.#..
##....#.#.##.####.#.#..##.#..##.#.#.#.#.#.#..#######.##..#.##....####...###
##.#.#.##.#...###.#...#...##..#.#.#...#....#...#....#.#..###.##.##...##....
.####....#.##...#.#.#.....#.#.#.#..#.....###.##.#..#...#...###.#..#.#..##.#
##..#.#....##....#.##.##.#.###........#.#..#.#....#.#.###...####..##.####.#
.##.#..###..#.#...###.###.###.###.#.#...##.#.#.###.#.####.###.##...#.#.#...
.#.##.##..#.#.####..##.#####.#..#..####.###..###..####.....###.###.......#.
##.#..#.#..###..#.....#.##.....#.#.#.##..######.#..#.#...#...##..###..##.#.
##..##...#........##..##.######.#.####...#..#..####..#.##.#...#..#.##.#.##.
#..#.##..###.#......####..##.##.#....#.###.##..#.#.#.##.##..#..#.##..##....
.....#....#.#.##..#.##....####.####.....#.###.#.#..#.#.....#..##..##.###...
.#......##....#.##.#...#.#####...##..##.....#.##.##.....#..#..##.#...#####.
##.#...##...#....#.####.#.#....#.#..######..........####.....#.#.#......#..
#.#.#....###..#..####..#.##...##.#####.#...#.#...###...#..##..##.####.#...#
#...###.##.#..#.###..##...##.##..#...##.#....#.#.##.##....#.#....#...#####.
#....##..####.....#.##.#####.#...##..##....#...###...#...#..#.#...#..#...##
#..##.##.....##..#.###..........###...#.#...#..#..##..#...##.####.#....###.
.#....#..#..##.###..########.#......#...#..#..##.##....###.###.....#...##.#
##..###.#....#.#...##.#.###.#.#..#..#..###.#...#.##.#####.#.#..##..#.#..#.#
#.#.#...#....#####.####..#.##.####..#.#.#...#....#..#.###...###.###...#...#
.###..##.#..##..#..#...##.#.#...###..##.#.####.#....#.#...##....#....##..##
..#.##..####.##..##...##..#.###.......#..####.##..#.#......#...##..###.....
##....##.####.####..#.#.#..#####.....###.##..###..####.###.#####..#.....#.#
.####.#..#####.....#..######...##.#....####......######...###..###..#.#.###
.#..#.#.####.#.##........###.#.#.#.##.#..#...#......###.#.##.#..#.#...#...#
.#..#.##....#...#...#.##..#...##.....#..######..##.###...#..#..####..#.#.#.
##.#.#.#..#.##..#.#......#.######..#####...#.##.###.######.#...#.###.##...#

0 comments on commit 1f8c4ed

Please sign in to comment.