Skip to content

Commit d222be9

Browse files
committed
day 1, day 2
1 parent 1bed16d commit d222be9

File tree

7 files changed

+4872
-0
lines changed

7 files changed

+4872
-0
lines changed

Advent22.iml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src/main/kotlin" isTestSource="false" />
7+
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
8+
<sourceFolder url="file://$MODULE_DIR$/src/test/kotlin" isTestSource="true" />
9+
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
10+
</content>
11+
<orderEntry type="inheritedJdk" />
12+
<orderEntry type="sourceFolder" forTests="false" />
13+
<orderEntry type="library" name="KotlinJavaRuntime" level="project" />
14+
</component>
15+
</module>

src/main/kotlin/Day01.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
object Day01 {
2+
private val input = Resource.parseAsString("1.txt")
3+
private val totals = input.split("\n\n").map{
4+
it.lines().sumOf { line -> line.toInt() }
5+
}
6+
7+
fun solvePart1(): Int {
8+
return totals.max()
9+
}
10+
11+
fun solvePart2(): Int {
12+
return totals.sortedDescending().slice(0..2).sum()
13+
}
14+
}

src/main/kotlin/Day02.kt

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
object Day02 {
2+
enum class Sign(val score: Int) {
3+
ROCK(1), PAPER(2), SCISSORS(3)
4+
}
5+
6+
private fun mapInputToSign(input: Char): Sign {
7+
return when (input) {
8+
'A', 'X' -> Sign.ROCK
9+
'B', 'Y' -> Sign.PAPER
10+
'C', 'Z' -> Sign.SCISSORS
11+
else -> throw Error("Cannot convert to sign, wrong input.")
12+
}
13+
}
14+
15+
private val input = Resource.parseAsStringList("2.txt")
16+
17+
private fun getScore(playerSign: Sign, opponentSign: Sign): Int {
18+
val stateScore = when (playerSign.score - opponentSign.score) {
19+
-1, 2 -> 0
20+
0 -> 3
21+
1, -2 -> 6
22+
else -> throw Error("Could not get score. Check your input.")
23+
}
24+
25+
return stateScore + playerSign.score
26+
}
27+
28+
fun solvePart1(): Int {
29+
val games = input.map { it.split(" ").map { str -> mapInputToSign(str.first()) } }
30+
val scores = games.map { getScore(it[0], it[1]) }
31+
32+
return scores.sum()
33+
}
34+
35+
fun solvePart2(): Int {
36+
val scores = input.map {
37+
val (opponentChar, resultChar) = it.split(" ").map { str -> str.first() }
38+
val opponentSign = mapInputToSign(opponentChar)
39+
val playerSign = when (opponentSign to resultChar) {
40+
Sign.SCISSORS to 'X', Sign.PAPER to 'Y', Sign.ROCK to 'Z' -> Sign.PAPER
41+
Sign.PAPER to 'X', Sign.ROCK to 'Y', Sign.SCISSORS to 'Z' -> Sign.ROCK
42+
Sign.ROCK to 'X', Sign.SCISSORS to 'Y', Sign.PAPER to 'Z' -> Sign.SCISSORS
43+
else -> {
44+
throw Error("Could not convert result char to player sign. Check your input.")
45+
}
46+
}
47+
getScore(playerSign, opponentSign)
48+
49+
}
50+
51+
return scores.sum()
52+
}
53+
}

src/main/kotlin/Main.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
fun main() {
2+
val part1 = Day02.solvePart1()
3+
println(part1)
4+
5+
val part2 = Day02.solvePart2()
6+
println(part2)
7+
}

src/main/kotlin/Resource.kt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import java.io.File
2+
3+
internal object Resource {
4+
private fun getFile(filename: String): File {
5+
return File("src/main/resources/$filename")
6+
}
7+
8+
fun parseAsString(filename: String): String {
9+
return getFile(filename).readText()
10+
}
11+
12+
fun parseAsStringList(filename: String): List<String> {
13+
return getFile(filename).readLines()
14+
}
15+
16+
fun parseAsIntList(filename: String): List<Int?> {
17+
return parseAsStringList(filename).map{ it.toInt() }
18+
}
19+
}

0 commit comments

Comments
 (0)