-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday2-2.ml
61 lines (54 loc) · 1.54 KB
/
day2-2.ml
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
open Core
let input_file = "day2.input"
module RPS = struct
module Play = struct
type t = Rock | Paper | Scissors
let to_int play =
match play with
Rock -> 1
| Paper -> 2
| Scissors -> 3
let of_char c =
match c with
'A' -> Rock
| 'B' -> Paper
| 'C' -> Scissors
| _ -> raise (Failure "Unknown character.")
end
module Result = struct
type t = Lose | Draw | Win
let to_int res =
match res with
Lose -> 0
| Draw -> 3
| Win -> 6
let of_char c =
match c with
'X' -> Lose
| 'Y' -> Draw
| 'Z' -> Win
| _ -> raise (Failure "Unknown character.")
end
let get_play opp res =
match res with
Result.Lose -> (
match opp with
Play.Rock -> Play.Scissors
| Play.Scissors -> Play.Paper
| Play.Paper -> Play.Rock
)
| Result.Draw -> opp
| Result.Win -> (
match opp with
Play.Rock -> Play.Paper
| Play.Scissors -> Play.Rock
| Play.Paper -> Play.Scissors
)
end
let process_line score line =
let opp = String.get line 0 |> RPS.Play.of_char in
let res = String.get line 2 |> RPS.Result.of_char in (* format of input is "_ _" *)
score + RPS.((get_play opp res |> Play.to_int) + Result.to_int res)
let main =
let score = Stdio.In_channel.(fold_lines (create input_file) ~init:0 ~f:process_line) in
Stdio.Out_channel.print_endline (Int.to_string score)