-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday02.dwl
30 lines (26 loc) · 872 Bytes
/
day02.dwl
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
%dw 2.0
output application/json
import * from dw::core::Arrays
import * from dw::core::Strings
var parsed = lines(payload) map ($ splitBy " ")
type ABC = "A" | "B" | "C"
var winsAgainst = { A: "C", B: "A", C: "B" }
var losesAgainst = winsAgainst mapObject ($): $$
var XYZtoABC = { X: "A", Y: "B", Z: "C" }
var ABCtoScore = { A: 1, B: 2, C: 3 }
fun part1(opponent: ABC, myself: ABC): Number =
(
if (myself == opponent) 3
else if(winsAgainst[myself] == opponent) 6
else 0
) + ABCtoScore[myself]
fun part2(opponent: ABC, needsToEndAs: String): Number = needsToEndAs match {
case "X" -> 0 + ABCtoScore[winsAgainst[opponent]]
case "Y" -> 3 + ABCtoScore[opponent]
case "Z" -> 6 + ABCtoScore[losesAgainst[opponent]]
}
---
{
part1: sum(parsed map part1($[0], XYZtoABC[$[1]])),
part2: sum(parsed map part2($[0], $[1]))
}