generated from kotlin-hands-on/advent-of-code-kotlin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay02.kt
90 lines (77 loc) · 2.47 KB
/
Day02.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package year2022.`02`
import readInput
fun main() {
fun part1(input: List<String>): Int {
return input
.map { it.split(" ") }
.map { Round(it.first(), it[1]) }
.sumOf { it.outcome() }
}
fun part2(input: List<String>): Int {
return input
.map { it.split(" ") }
.map { Round(it.first(), it[1]) }
.sumOf { it.outcome2() }
}
// test if implementation meets criteria from the description, like:
val testInput = readInput("Day02_test")
val part1Test = part2(testInput)
//println(part1Test)
check(part1Test == 12)
val input = readInput("Day02")
println(part1(input))
println(part2(input))
}
data class Round(
val opponent: String,
val outcome: String
) {
fun outcome(): Int {
val myRes = when (outcome) {
"X" -> 1 // rock
"Y" -> 2 //paper
"Z" -> 3 //scissors
else -> error("aaa")
}
// rock (A) - 1
// paper (B) - 2
// scissors (C) - 3
val youOutcome = when {
opponent == "A" && outcome == "X" -> 3
opponent == "A" && outcome == "Y" -> 6
opponent == "A" && outcome == "Z" -> 0
opponent == "B" && outcome == "X" -> 0
opponent == "B" && outcome == "Y" -> 3
opponent == "B" && outcome == "Z" -> 6
opponent == "C" && outcome == "X" -> 6
opponent == "C" && outcome == "Y" -> 0
opponent == "C" && outcome == "Z" -> 3
else -> error("illegal state")
}
return youOutcome + myRes
}
fun outcome2(): Int {
val myRes = when (outcome) {
"X" -> 0 //you lose
"Y" -> 3 //draw
"Z" -> 6 //you win
else -> error("Illegal State")
}
// rock (A) - 1
// paper (B) - 2
// scissors (C) - 3
val yourFigure = when {
opponent == "A" && outcome == "X" -> 3
opponent == "A" && outcome == "Y" -> 1
opponent == "A" && outcome == "Z" -> 2
opponent == "B" && outcome == "X" -> 1
opponent == "B" && outcome == "Y" -> 2
opponent == "B" && outcome == "Z" -> 3
opponent == "C" && outcome == "X" -> 2
opponent == "C" && outcome == "Y" -> 3
opponent == "C" && outcome == "Z" -> 1
else -> error("Illegal State")
}
return yourFigure + myRes
}
}