Day 2 part 2
This commit is contained in:
parent
9cfa03069e
commit
d924d15dd6
56
ocaml/2.ml
56
ocaml/2.ml
|
@ -3,47 +3,71 @@
|
||||||
type shape = Rock | Paper | Scissors
|
type shape = Rock | Paper | Scissors
|
||||||
type outcome = Win | Tie | Loss
|
type outcome = Win | Tie | Loss
|
||||||
|
|
||||||
let shape_of = function
|
let shape_of_char = function
|
||||||
| 'A' | 'X' -> Rock
|
| 'A' | 'X' -> Rock
|
||||||
| 'B' | 'Y' -> Paper
|
| 'B' | 'Y' -> Paper
|
||||||
| 'C' | 'Z' -> Scissors
|
| 'C' | 'Z' -> Scissors
|
||||||
| s -> failwith ("invalid shape: " ^ (String.make 1 s))
|
| s -> failwith ("invalid shape: " ^ (String.make 1 s))
|
||||||
;;
|
|
||||||
|
|
||||||
let score_of_shape = function
|
let score_of_shape = function
|
||||||
| Rock -> 1
|
| Rock -> 1
|
||||||
| Paper -> 2
|
| Paper -> 2
|
||||||
| Scissors -> 3
|
| Scissors -> 3
|
||||||
;;
|
|
||||||
|
|
||||||
let score_of_outcome = function
|
let score_of_outcome = function
|
||||||
| Loss -> 0
|
| Loss -> 0
|
||||||
| Tie -> 3
|
| Tie -> 3
|
||||||
| Win -> 6
|
| Win -> 6
|
||||||
;;
|
|
||||||
|
|
||||||
let outcome_of = function
|
let part1 line =
|
||||||
|
let outcome_of_round = function
|
||||||
| Rock, Scissors -> Win
|
| Rock, Scissors -> Win
|
||||||
| Paper, Rock -> Win
|
| Paper, Rock -> Win
|
||||||
| Scissors, Paper -> Win
|
| Scissors, Paper -> Win
|
||||||
| a, b when a = b -> Tie
|
| a, b when a = b -> Tie
|
||||||
| _ -> Loss
|
| _ -> Loss
|
||||||
;;
|
in
|
||||||
|
let op_move = shape_of_char (String.get line 0) in
|
||||||
let play_round line =
|
let my_move = shape_of_char (String.get line 2) in
|
||||||
let op_move = shape_of (String.get line 0) in
|
let outcome = outcome_of_round(my_move, op_move) in
|
||||||
let my_move = shape_of (String.get line 2) in
|
|
||||||
let outcome = outcome_of(my_move, op_move) in
|
|
||||||
let outcome_score = score_of_outcome outcome in
|
let outcome_score = score_of_outcome outcome in
|
||||||
let shape_score = score_of_shape my_move in
|
let shape_score = score_of_shape my_move in
|
||||||
outcome_score + shape_score
|
outcome_score + shape_score
|
||||||
|
|
||||||
|
let part2 line =
|
||||||
|
let outcome_of_char = function
|
||||||
|
| 'X' -> Loss
|
||||||
|
| 'Y' -> Tie
|
||||||
|
| 'Z' -> Win
|
||||||
|
| o -> failwith ("invalid outcome: " ^ (String.make 1 o))
|
||||||
|
in
|
||||||
|
let make_move = function
|
||||||
|
| Rock, Win -> Paper
|
||||||
|
| Rock, Loss -> Scissors
|
||||||
|
| Paper, Win -> Scissors
|
||||||
|
| Paper, Loss -> Rock
|
||||||
|
| Scissors, Win -> Rock
|
||||||
|
| Scissors, Loss -> Paper
|
||||||
|
| s, Tie -> s
|
||||||
|
in
|
||||||
|
let op_move = shape_of_char (String.get line 0) in
|
||||||
|
let outcome = outcome_of_char (String.get line 2) in
|
||||||
|
let my_move = make_move(op_move, outcome) in
|
||||||
|
let outcome_score = score_of_outcome outcome in
|
||||||
|
let shape_score = score_of_shape my_move in
|
||||||
|
outcome_score + shape_score
|
||||||
|
|
||||||
|
let play lines part rules =
|
||||||
|
let score = lines
|
||||||
|
|> List.map rules
|
||||||
|
|> List.fold_left (+) 0
|
||||||
|
in
|
||||||
|
Printf.printf "Part %d: %d\n" part score
|
||||||
;;
|
;;
|
||||||
|
|
||||||
let contents = read_file "inputs/2.txt" in
|
let contents = read_file "inputs/2.txt" in
|
||||||
let score = String.split_on_char '\n' contents
|
let lines = String.split_on_char '\n' contents
|
||||||
|> List.filter (fun l -> String.trim l <> "")
|
|> List.filter (fun l -> String.trim l <> "")
|
||||||
|> List.map play_round
|
|
||||||
|> List.fold_left (+) 0
|
|
||||||
in
|
in
|
||||||
|
play lines 1 part1;
|
||||||
Printf.printf "%d" score
|
play lines 2 part2
|
||||||
|
|
Loading…
Reference in New Issue