-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay02.fsx
87 lines (71 loc) · 1.78 KB
/
Day02.fsx
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
open System.IO
let input = File.ReadAllLines "data/input02.txt"
type Move =
| Rock
| Paper
| Scissors
type Round = { Player: Move; Opponent: Move }
let toMove =
function
| 'A'
| 'X' -> Rock
| 'B'
| 'Y' -> Paper
| 'C'
| 'Z' -> Scissors
| _ -> failwith "invalid move"
let toRound (str: string) =
{ Player = toMove str[2]
Opponent = toMove str[0] }
let roundScore round =
let shapeScore =
match round.Player with
| Rock -> 1
| Paper -> 2
| Scissors -> 3
let outcomeScore =
match (round.Player, round.Opponent) with
// Win
| Rock, Scissors
| Scissors, Paper
| Paper, Rock -> 6
// Draw
| p, o when p = o -> 3
// Loss
| _ -> 0
shapeScore + outcomeScore
// Part One
let totalScore = input |> Array.sumBy (toRound >> roundScore)
type Outcome =
| Win
| Loss
| Draw
type Round' = { Result: Outcome; Opponent: Move }
let toOutcome =
function
| 'X' -> Loss
| 'Y' -> Draw
| 'Z' -> Win
| _ -> failwith "invalid outcome"
let toRound' (str: string) =
{ Result = toOutcome str[2]
Opponent = toMove str[0] }
// Reduction of the second problem to the first
let inferRound round' =
let move =
match round'.Result with
| Win ->
match round'.Opponent with
| Rock -> Paper
| Paper -> Scissors
| Scissors -> Rock
| Loss ->
match round'.Opponent with
| Rock -> Scissors
| Paper -> Rock
| Scissors -> Paper
| Draw -> round'.Opponent
{ Player = move
Opponent = round'.Opponent }
// Part Two
let revisedTotalScore = input |> Array.sumBy (toRound' >> inferRound >> roundScore)