Skip to content

Commit 0056af1

Browse files
committed
2022/Day 02
1 parent 87438fe commit 0056af1

File tree

4 files changed

+2542
-1
lines changed

4 files changed

+2542
-1
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@
1010
🎄 --- Day 01: Calorie Counting ---
1111
🌟 Part 1: 71502 (4.9ms)
1212
🌟 Part 2: 208191 (3.42ms)
13-
13+
14+
🎄 --- Day 02: Rock Paper Scissors ---
15+
🌟 Part 1: 9651 (4.71ms)
16+
🌟 Part 2: 10560 (2.86ms)
17+
1418
```
1519

1620
## Visualizations
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package me.grison.aoc.y2022
2+
3+
import me.grison.aoc.Day
4+
import me.grison.aoc.butLast
5+
6+
class Day02 : Day(2, 2022) {
7+
override fun title() = "Rock Paper Scissors"
8+
9+
override fun partOne() = solve()
10+
11+
override fun partTwo() = solve { round -> round.butLast() + roundEnding[round] }
12+
13+
private fun solve(inputMixer: (String) -> String = { it }): Int {
14+
return inputList
15+
.map { inputMixer(it) }
16+
.sumOf { outcomes.getValue(it) + shapeScores.getValue(it.last()) }
17+
}
18+
19+
// X: 🗿; Y: 📜; Z: ✂️
20+
private val shapeScores = mapOf('X' to 1, 'Y' to 2, 'Z' to 3).withDefault { 0 }
21+
22+
private val outcomes =
23+
mapOf(
24+
"A X" to 3, "B Y" to 3, "C Z" to 3, // draw
25+
"A Y" to 6, "B Z" to 6, "C X" to 6, // win
26+
).withDefault { 0 }
27+
28+
private val roundEnding = mapOf(
29+
// lose
30+
"A X" to 'Z' /* 🗿 -> ✂️ */, "B X" to 'X' /* 📜 -> ✂️ */, "C X" to 'Y' /* ✂️ -> 📜 */,
31+
// draw
32+
"A Y" to 'X' /* 🗿 -> 🗿 */, "B Y" to 'Y' /* 📜 -> 📜 */, "C Y" to 'Z' /* ✂️ -> ✂️ */,
33+
// win
34+
"A Z" to 'Y' /* 🗿 -> 📜 */, "B Z" to 'Z' /* 📜 -> ✂️ */, "C Z" to 'X' /* ✂️ -> 🗿 */
35+
)
36+
}

0 commit comments

Comments
 (0)