Skip to content

Instantly share code, notes, and snippets.

@Jonas164
Created December 2, 2022 08:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jonas164/effd472aebefd88e09f14b2c623ef928 to your computer and use it in GitHub Desktop.
Save Jonas164/effd472aebefd88e09f14b2c623ef928 to your computer and use it in GitHub Desktop.
AoC 2022 Day 2
package day2
import java.io.File
fun main() {
val list = day1.readFileLineByLine("src/main/kotlin/day2/input.txt");
var totalScore = 0;
list.forEach{
val tokens = it.split(" ");
totalScore += getWinScore(tokens[0],tokens[1]);
totalScore += getShapeScore(tokens[1]);
}
println("Part 1:$totalScore");
var totalScorePart2 = 0;
list.forEach{
val tokens = it.split(" ");
totalScorePart2 += getDesiredScore(tokens[0],tokens[1]);
}
println("Part 1:$totalScorePart2");
}
fun getDesiredScore(other:String, desired:String): Int{
when(other){
"A" -> return when(desired){
"X" -> 0 + 3;
"Y" -> 3 + 1;
"Z" -> 6 + 2;
else -> {
throw IllegalArgumentException("invalid args me=$desired")
}
}
"B" -> return when(desired) {
"X" -> 0 + 1;
"Y" -> 3 + 2;
"Z" -> 6 + 3;
else -> {
throw IllegalArgumentException("invalid args me=$desired")
}
}
"C" -> return when(desired) {
"X" -> 0 + 2;
"Y" -> 3 + 3;
"Z" -> 6 + 1;
else -> {
throw IllegalArgumentException("invalid args me=$desired")
}
}
else -> {
throw IllegalArgumentException("invalid args other=$other")
}
}
}
fun getWinScore(other: String, me: String): Int {
when (other) {
"A" -> return when (me) {
"X" -> 3;
"Y" -> 6;
"Z" -> 0;
else -> {
throw IllegalArgumentException("invalid args me=$me")
}
}
"B" -> return when (me) {
"X" -> 0;
"Y" -> 3
"Z" -> 6;
else -> {
throw IllegalArgumentException("invalid args me=$me")
}
}
"C" -> return when (me) {
"X" -> 6;
"Y" -> 0;
"Z" -> 3
else -> {
throw IllegalArgumentException("invalid args me=$me")
}
}
else -> {
throw IllegalArgumentException("invalid args other=$other")
}
}
}
fun getShapeScore(myShape: String):Int {
return when (myShape) {
"X" -> 1;
"Y" -> 2;
"Z" -> 3;
else -> {
throw IllegalArgumentException("invalid args myShape=$myShape")
}
}
}
fun readFileLineByLine(fileName: String): MutableList<String> {
val result = mutableListOf<String>();
File(fileName).forEachLine { result.add(it) }
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment