Skip to content

Commit e41ce1c

Browse files
author
Emre Akgül (CarTelSol)
committed
finish day08
1 parent b15449f commit e41ce1c

File tree

3 files changed

+92
-2
lines changed

3 files changed

+92
-2
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
![Kotlin](https://img.shields.io/badge/Kotlin-grey?logo=Kotlin&style=for-the-badge)
2-
![](https://img.shields.io/badge/📅%20days-04-005060?style=for-the-badge)
3-
![](https://img.shields.io/badge/⭐%20stars-06-005060?style=for-the-badge)
2+
![](https://img.shields.io/badge/📅%20days-08-005060?style=for-the-badge)
3+
![](https://img.shields.io/badge/⭐%20stars-16-005060?style=for-the-badge)
44

55
# Advent-of-Code 2023
66

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package tr.emreone.adventofcode.days
2+
3+
import tr.emreone.kotlin_utils.Resources
4+
import tr.emreone.kotlin_utils.automation.Day
5+
import tr.emreone.kotlin_utils.lcm
6+
import java.lang.IllegalStateException
7+
8+
class Day08 : Day(
9+
8,
10+
2023,
11+
"Haunted Wasteland",
12+
session = Resources.resourceAsString("session.cookie")
13+
) {
14+
15+
private fun parseInput(): Pair<List<Int>, Map<String, List<String>>> {
16+
val command = inputAsList[0].toCharArray().map { if (it == 'L') 0 else 1 }
17+
val maps = inputAsList.drop(2).associate {
18+
val (key, value) = it.split("=")
19+
20+
val destinations = value
21+
.trim()
22+
.trimStart('(')
23+
.trimEnd(')')
24+
.split(',')
25+
.map {
26+
it.trim()
27+
}
28+
29+
key.trim() to destinations
30+
}
31+
32+
return command to maps
33+
}
34+
35+
override fun part1(): Int {
36+
val (command, maps) = parseInput()
37+
var currentPosition ="AAA"
38+
39+
var counter = 0
40+
val totalCommands = command.size
41+
while(currentPosition != "ZZZ") {
42+
val index = (counter.mod(totalCommands))
43+
// print("%6d: $currentPosition to ($index -> ${ if (command[index] == 0) 'L' else 'R'}) ".format(counter))
44+
currentPosition = maps[currentPosition]?.get(command[index]) ?: throw IllegalStateException()
45+
// println(currentPosition)
46+
counter++
47+
}
48+
49+
return counter
50+
}
51+
52+
override fun part2(): Long {
53+
val (command, maps) = parseInput()
54+
val startPositions = maps.keys.filter { it.endsWith('A') }.toMutableList()
55+
56+
val totalCommands = command.size
57+
58+
val steps = startPositions.map {
59+
var currentPosition = it
60+
61+
var counter = 0L
62+
while(!currentPosition.endsWith('Z')) {
63+
val index = (counter.mod(totalCommands))
64+
currentPosition = maps[currentPosition]?.get(command[index]) ?: throw IllegalStateException()
65+
counter++
66+
}
67+
68+
counter
69+
}
70+
71+
return steps.lcm()
72+
}
73+
74+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package tr.emreone.adventofcode.days
2+
3+
import tr.emreone.kotlin_utils.Resources
4+
import tr.emreone.kotlin_utils.automation.solve
5+
6+
fun main() {
7+
solve<Day08>(false) {
8+
Resources.resourceAsList("day08_1_example.txt")
9+
.joinToString("\n") part1 6
10+
}
11+
12+
solve<Day08>(false) {
13+
Resources.resourceAsList("day08_2_example.txt")
14+
.joinToString("\n") part2 6
15+
}
16+
}

0 commit comments

Comments
 (0)