Last active
December 2, 2022 14:46
-
-
Save jacobhyphenated/2c90361220426c72e6e5b4c644381ace to your computer and use it in GitHub Desktop.
Advent of Code 2022 Day2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// I'll probably stop sharing my interface file soon. | |
// Added some changes for reading the input file | |
interface Day<T> { | |
fun getInput(): T | |
fun part1(input: T): Number | |
fun part2(input: T): Number | |
fun run() { | |
val input = getInput() | |
var start = System.nanoTime() | |
println("Part 1: ${part1(input)} (${(System.nanoTime() - start) / 1000000.0}ms)") | |
start = System.nanoTime() | |
println("Part 2: ${part2(input)} (${(System.nanoTime() - start) / 1000000.0}ms)") | |
} | |
fun readInputFile(day: String): String { | |
return this.javaClass.classLoader.getResource("$day/input.txt")!! | |
.readText() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Day2: Day<List<String>> { | |
override fun getInput(): List<String> { | |
return readInputFile("day2").lines() | |
} | |
override fun part1(input: List<String>): Number { | |
return input.map { | |
val (move1, move2) = it.split(" ") | |
Pair(parseFirstMove(move1), parseSecondMove(move2)) | |
}.sumOf { scoreRound(it) } | |
} | |
override fun part2(input: List<String>): Number { | |
return input.map { | |
val (move1, expectedResult) = it.split(" ") | |
val firstMove = parseFirstMove(move1) | |
Pair(firstMove, determineSecondMove(expectedResult, firstMove)) | |
}.sumOf { scoreRound(it) } | |
} | |
private fun scoreRound(round: Pair<Move,Move>): Int { | |
val (opponentMove, yourMove) = round | |
return roundResultScore(opponentMove, yourMove) + yourMove.points | |
} | |
private fun parseFirstMove(move: String): Move { | |
return when (move) { | |
"A" -> Move.ROCK | |
"B" -> Move.PAPER | |
"C" -> Move.SCISSORS | |
else -> throw NotImplementedError("Invalid character $move") | |
} | |
} | |
private fun parseSecondMove(move: String): Move { | |
return when (move) { | |
"X" -> Move.ROCK | |
"Y" -> Move.PAPER | |
"Z" -> Move.SCISSORS | |
else -> throw NotImplementedError("Invalid character $move") | |
} | |
} | |
private fun determineSecondMove(expectedResult: String, firstMove: Move): Move { | |
return when(expectedResult) { | |
"X" -> when (firstMove) { | |
Move.PAPER -> Move.ROCK | |
Move.SCISSORS -> Move.PAPER | |
Move.ROCK -> Move.SCISSORS | |
} | |
"Y" -> firstMove | |
"Z" -> when (firstMove) { | |
Move.PAPER -> Move.SCISSORS | |
Move.SCISSORS -> Move.ROCK | |
Move.ROCK -> Move.PAPER | |
} | |
else -> throw NotImplementedError("Invalid character $expectedResult") | |
} | |
} | |
private fun roundResultScore(opponent: Move, you: Move): Int { | |
return when(opponent){ | |
Move.SCISSORS -> when (you) { | |
Move.SCISSORS -> 3 | |
Move.ROCK -> 6 | |
Move.PAPER -> 0 | |
} | |
Move.PAPER -> when (you) { | |
Move.SCISSORS -> 6 | |
Move.PAPER -> 3 | |
Move.ROCK -> 0 | |
} | |
Move.ROCK -> when (you) { | |
Move.SCISSORS -> 0 | |
Move.ROCK -> 3 | |
Move.PAPER -> 6 | |
} | |
} | |
} | |
} | |
enum class Move(val points: Int) { | |
ROCK(1), | |
PAPER(2), | |
SCISSORS(3); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment