-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday02_part02.fs
47 lines (40 loc) · 1.99 KB
/
day02_part02.fs
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
module day02_part02
open AdventOfCode_2022.Modules.LocalHelper
let path = "day02/day02_input.txt"
let needwin = [[|"A"; "Z"|]; [|"B"; "Z"|]; [|"C"; "Z"|]]
let needdraw = [[|"A"; "Y"|]; [|"B"; "Y"|]; [|"C"; "Y"|]]
let needlost = [[|"A"; "X"|]; [|"B"; "X"|]; [|"C"; "X"|]]
let getPoint(figure: string) =
match figure with
| res when res = "A" || res = "X" -> 1
| res when res = "B" || res = "Y" -> 2
| res when res = "C" || res = "Z" -> 3
| _ -> 0
let getCounterPoint(figure: string) (result: int) =
match figure with
| res when res = "A" && result = -1 -> getPoint("C") // Rock vs Scissor -> Lost
| res when res = "A" && result = 0 -> getPoint("A") // Rock vs Rock -> Draw
| res when res = "A" && result = 1 -> getPoint("B") // Rock vs Paper -> Win
| res when res = "B" && result = -1 -> getPoint("A") // Paper vs Rock -> Lost
| res when res = "B" && result = 0 -> getPoint("B") // Paper vs Paper -> Draw
| res when res = "B" && result = 1 -> getPoint("C") // Paper vs Scissor -> Win
| res when res = "C" && result = -1 -> getPoint("B") // Scissor vs Paper -> Lost
| res when res = "C" && result = 0 -> getPoint("C") // Scissor vs Scissor -> Draw
| res when res = "C" && result = 1 -> getPoint("A") // Scissor vs Rock -> Win
| _ -> 0
let calculateRoundScore (round: int) (play: string[]) =
let result =
match needwin |> List.contains(play) with
| true -> (getCounterPoint (play.[0]) 1) + 6
| false ->
match needdraw |> List.contains(play) with
| true -> (getCounterPoint(play.[0]) 0) + 3
| false ->
match needlost |> List.contains(play) with
| true -> (getCounterPoint(play.[0]) -1) + 0
| false -> 0
result
let execute =
let inputLines = GetLinesFromFile(path) |> Array.toList
let rounds = inputLines |> List.map(fun l -> l.Split(" "))
rounds |> List.map(fun r -> calculateRoundScore 0 r) |> List.sum