added 2024 part 1

This commit is contained in:
2025-06-05 22:45:37 +02:00
parent 66007c5389
commit f1b41dc9c7
33 changed files with 2225 additions and 0 deletions

View File

@@ -0,0 +1,99 @@
import java.io.File
fun main() {
val filenames = listOf("example.txt", "input.txt")
filenames.forEach { filename ->
val input = readInput(filename)
val (map, guard) = generateMap(input)
println("${filename.dropLast(4)} Guard Step Count: ${getWalkCount(map, guard)}")
//println("${filename.dropLast(4)} Incorrect Middle Sum: $incorrect")
}
}
fun readInput(filename: String) = File("src/$filename").readText()
fun generateMap(input: String): Pair<List<List<Pair<String, Boolean>>>, Pair<Int, Int>> {
var guardCord = Pair(0, 0)
val map = input.lines().mapIndexed { x, line ->
line.trim().split("").mapIndexed { y, tile ->
when (tile) {
"^" -> {
guardCord = Pair(x, y)
Pair(tile, false)
}
else -> Pair(tile, false)
}
}
}
return Pair(map, guardCord)
}
fun getWalkCount(map: List<List<Pair<String, Boolean>>>, guardCord: Pair<Int, Int>): Int {
val mutableMap = map.map { it.toMutableList() }.toMutableList()
var count = 1
var direction = Direction.UP
var currentLocation = guardCord
var outOfBounds = false
while (!outOfBounds){
try {
when (direction){
Direction.UP -> {
val front = mutableMap[currentLocation.first - 1][currentLocation.second]
if (front.first == "#") {
direction = Direction.RIGHT
} else {
if (!mutableMap[currentLocation.first][currentLocation.second].second){
count = count.inc()
mutableMap[currentLocation.first][currentLocation.second] = mutableMap[currentLocation.first][currentLocation.second].copy(second = true, first = "@")
}
currentLocation = Pair(currentLocation.first - 1, currentLocation.second)
}
}
Direction.DOWN -> {
val front = mutableMap[currentLocation.first + 1][currentLocation.second]
if (front.first == "#") {
direction = Direction.LEFT
} else {
if (!mutableMap[currentLocation.first][currentLocation.second].second){
count = count.inc()
mutableMap[currentLocation.first][currentLocation.second] = mutableMap[currentLocation.first][currentLocation.second].copy(second = true, first = "@")
}
currentLocation = Pair(currentLocation.first + 1, currentLocation.second)
}
}
Direction.RIGHT -> {
val front = mutableMap[currentLocation.first][currentLocation.second + 1]
if (front.first == "#") {
direction = Direction.DOWN
} else {
if (!mutableMap[currentLocation.first][currentLocation.second].second){
count = count.inc()
mutableMap[currentLocation.first][currentLocation.second] = mutableMap[currentLocation.first][currentLocation.second].copy(second = true, first = "@")
}
currentLocation = Pair(currentLocation.first, currentLocation.second + 1)
}
}
Direction.LEFT -> {
val front = mutableMap[currentLocation.first][currentLocation.second - 1]
if (front.first == "#") {
direction = Direction.UP
} else {
if (!mutableMap[currentLocation.first][currentLocation.second].second){
count = count.inc()
mutableMap[currentLocation.first][currentLocation.second] = mutableMap[currentLocation.first][currentLocation.second].copy(second = true, first = "@")
}
currentLocation = Pair(currentLocation.first, currentLocation.second - 1)
}
}
}
//println(mutableMap.map { it.map { it.first }.joinToString("") }.joinToString("\n"))
} catch (e: IndexOutOfBoundsException) {
outOfBounds = true
}
}
return count
}
enum class Direction(){
UP,
DOWN,
RIGHT,
LEFT
}

View File

@@ -0,0 +1,10 @@
....#.....
.........#
..........
..#.......
.......#..
..........
.#..^.....
........#.
#.........
......#...

View File

@@ -0,0 +1,130 @@
..................#................................................................#........#.....................................
...#...........#...................................................#........................................#.................#...
...................................#................#.#...............#.................................................#.........
.....#......#................#.....................................................................#..........#........#......#...
............................................................................................#.....................#...............
.....................#..##...#........................#.................#.......................#..#.........#.......#...#......#.
............##....................................##..................#...............................#....#......................
.............#............#.#..#.......#...........#..............#...............#.....#.........................................
....................#..........##.........#.........#................#............................................................
....#.......#................................................................#...#.#..........................................#.#.
..#..............#.....................................#..........#.#....#..............#..................#.#............#.......
.#.......#.........................#........................#..#.....#............................................................
........#...#......................#...........#......................................#....................#......................
.............................#..............#........##....#....................#......#....................#............#......#.
..............#..............#............#.......................##....#..........................................#.........#....
.............#............#..........#..........#...#.....................................................#.......................
..#........#.....#..........................................#................................................#...#.#...#..........
........#.....................#...#..#......#.........................................................#.....##.........#..........
..............................................................................................#...#...............................
..#.......................#..........................#......#...................................................................#.
.......#.........................#..............#.........#.............#.......................................#...#.............
..................#.................#.......................#....................#....................................#...........
...#...#.......#......##.#...............#....#..............#..........................................#....#..#.................
#................................#.......#.....#.......#.............#..................#........#................................
...........#..................................#.......................................................................#.#..#......
.......#........#.........................................#..........................#...........................#.....#..........
................#..#.....................#..#...........................#...........................................#.............
#...#.......................#................................................#................#................................#..
..........#...........................#.......#............................................................................#.....#
..................................#............................................................#..................................
......................#........#.............#....#.....#.......#..........................#..........................#...........
...........................##.................#.............#........................#...#..................#.....................
.............................................................................................................#....#..........#....
................#...................................#...........#..........#....................#............................#....
........#....#................................................#................##..#..................................#...........
...........#....................................................#...#.#......#....................................................
.......................#.........................................................^....................................#...........
..........................#.............##..#........#.#....#.......#....................#...........#.........#.....#............
........#...........................#..#..........................................................#...............................
....#....................................#....#.........................................#.........#...............................
...............#...................................#.....#.................................#....................................#.
......#...............................................................#.............................#........................#....
.........................#.....#.........................#.#...............#.........#.....#..................#..#.........#......
.........................................................#......#........##.......................#...#......#................#...
.......................................#.....#.................................#..................................................
..................#..................................#..........................................................................#.
...................#....#.........#.......#................#....................#.........................#......#.....#..#.......
................................#..............................................................................#....#.............
.....................#.#.............#.................#..........#.......#.........................................#.............
......#....................#....................................................................#...#.............................
........#........#...........#.##.............#........................#............#..............#.........#....#........#......
........................#.................................................................#.......................................
............................................................#.....#....................#............#.....#....#............#.....
...........#............#....#........................................................##............#..............#..............
...........#...........#.....#..............#..............##....#........#......#................................................
..#................................................................................#......................#......#................
....................#.........#......................#.............................#.......#.......................#.............#
..#...#....#......................................................................................................#...............
...........#................................................................................#...............................#.....
....................................#............#..........#.........................#..................................#.......#
...................#......#...................#............#......#........#................#.............#...........#.........#.
........................#..................................#........#..................#..........................................
............#..#...........#..................##....................#...........................................#.................
.......................................................................................#.....#............#.......................
...............................................#....#................................#..#..........................#..#...........
..............#..#.....#.........#....................#.......#..#..............#...............................#.....#...........
..........#............................#..........................................................................................
........................................#........#........................................................#.#.....................
#................#......................#..................................................................................#......
...............................................................................#................................#.................
...................................................................................................................#.#........#...
#........................................................................#......................................#.#...............
...#......#............#...................#.............#........#..........................................#......#.............
..............##....#...................................................#......................#..................................
...............#........................................................................#...........#...#.#.......................
..........#.#.....#................................#...........#........#.......#.................................................
#....................#......................................................................................#.....................
..............#...##...........................................#.............#.......#..................#..............#..........
.#.............................................#..............................................................................#...
...#................................#............................................#............................#...................
..............................#......................................................#..........................................#.
...................#.....................................#..................................#......#..............................
....#.#..........##.............#.......#........................##..........#.................#..................................
#..#................#........##....................................................................................#..............
........#...#.........................................................................#...........................................
......................................................#....#...................#..#.................................#.............
........#...............#........#..............................................#............................#....................
...#..............................#.........................................##.......................................#....#.......
...#................#..............#.......#......................................................................................
........#..................#.............................................................#...........................#....#.......
......................#.#..........#.......#..................................#..#...#............#...............#...............
.....#..................#....#..#......#.......................#.....................................................#............
.....#................................#.........................#..................#.#..........#...#............#.....#..........
....#...................#....#.#.........#........#..................................................................#....#..#....
........#...............#......................................#........#..............................#..........................
....#...#.#.....#..............#..................#..........#...#..#......................#...#..........#......................#
...........#...................................................................................................#................#.
.................................##..............#.............................#...............................#..................
...........#.................#.............##...................................#.......................#.................##......
.....#..................#......#........#.................................................#..#........................#..#........
......#.......#......................................................................#.#.........#....#...........................
..................#.#........................#......#................................................................#......#.....
.....................#............#...................#.....#....................................#.......#..........#.......#.....
#.............................#...............................#...#...............................................................
..##.........#........#......................................#.....................#...#.............#............#..#............
..........#.....................#..#....................#...#....................#....................#.....................#.....
...........#..........#...............#..#..........#..............................................#.#............................
............#...#...#.............#.........#.............................#...............#.......................................
.........#...........#...............................................................#............................................
.#..#.................................................................#............#.#.......................##...................
........##............#...................#................................#......................................................
............................................#..........................#..........................................................
#...................#...........#..............#...#............#.#........#.................#......#.............................
........#....................................#....................................................................................
...........#.........#..#...............#...........#.....##.#......#....##............#....#....#.#.........##.........#.........
..#......................#..........#.................#.................#......#.............................#........#...........
....................#.................#....#..............................#....#.............#....#....#.....................#....
............#.................#.................................#...............................#..#...........................#..
...................................................#.....#..............#.......#....................#.#..........................
..#.........##..#........#...................................................................................#............#.......
.....#..............................#......................................................................#........#.............
................................#........##.......#...............#..............#.......#...#.#........#.........................
..............................#.....................#.........................................#..................................#
...........#...................##.................#.................................#...............................#........#....
..............................#...##......#..................#...#................................#......#.......#...............#
.....................##......#......#.#.......#..............#......#.................#.........#...................#.............
..........................#................................#...........#..........................................................
...#.....#....................#.....................#...#.....#.............................#.#....#.....#.#.................#....
............................#...#........#......................................................................#.....#...........
............#..........##..................#.............................................................#.....#..#...............