day2 - a bit overcomplicated :)
This commit is contained in:
parent
739b24dfbe
commit
efde27f8c8
3 changed files with 2566 additions and 0 deletions
4
bin/d2/dune
Normal file
4
bin/d2/dune
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
(executable
|
||||||
|
(public_name d2)
|
||||||
|
(name main)
|
||||||
|
(libraries core utils))
|
2500
bin/d2/input.txt
Normal file
2500
bin/d2/input.txt
Normal file
File diff suppressed because it is too large
Load diff
62
bin/d2/main.ml
Normal file
62
bin/d2/main.ml
Normal file
|
@ -0,0 +1,62 @@
|
||||||
|
type choice
|
||||||
|
= Rock
|
||||||
|
| Paper
|
||||||
|
| Scissors
|
||||||
|
|
||||||
|
let int_of_choice = function
|
||||||
|
| Rock -> 1
|
||||||
|
| Paper -> 2
|
||||||
|
| Scissors -> 3
|
||||||
|
|
||||||
|
let choice_of_string = function
|
||||||
|
| "A" | "X" -> Rock
|
||||||
|
| "B" | "Y" -> Paper
|
||||||
|
| "C" | "Z" -> Scissors
|
||||||
|
| _ -> failwith "invalid choice" ;;
|
||||||
|
|
||||||
|
type outcome
|
||||||
|
= Draw
|
||||||
|
| Win
|
||||||
|
| Lose
|
||||||
|
|
||||||
|
let int_of_outcome = function
|
||||||
|
| Lose -> 0
|
||||||
|
| Draw -> 3
|
||||||
|
| Win -> 6
|
||||||
|
|
||||||
|
let outcome_of_string = function
|
||||||
|
| "X" -> Lose
|
||||||
|
| "Y" -> Draw
|
||||||
|
| "Z" -> Win
|
||||||
|
| _ -> failwith "invalid choice"
|
||||||
|
|
||||||
|
let choice_of_outcome oponent = function
|
||||||
|
| Win -> if oponent = Rock then Paper else if oponent = Paper then Scissors else Rock
|
||||||
|
| Lose -> if oponent = Rock then Scissors else if oponent = Paper then Rock else Paper
|
||||||
|
| Draw -> if oponent = Rock then Rock else if oponent = Paper then Paper else Scissors
|
||||||
|
|
||||||
|
let outcome me oponent =
|
||||||
|
match int_of_choice me - int_of_choice oponent with
|
||||||
|
| -1 | 2 -> Lose
|
||||||
|
| 1 | -2 -> Win
|
||||||
|
| _ -> Draw
|
||||||
|
|
||||||
|
let solve f l = l
|
||||||
|
|> List.map (fun line -> line
|
||||||
|
|> String.split_on_char ' '
|
||||||
|
|> (fun l -> (List.hd l, List.tl l |> List.hd))
|
||||||
|
|> f
|
||||||
|
|> (fun (oponent, me) -> ((outcome me oponent) |> int_of_outcome) + int_of_choice me))
|
||||||
|
|> List.fold_left (+) 0
|
||||||
|
|
||||||
|
let d2_1 = solve (fun (oponent_s, me_s) ->
|
||||||
|
(choice_of_string oponent_s, choice_of_string me_s))
|
||||||
|
|
||||||
|
let d2_2 = solve (fun (oponent_s, outcome_s) ->
|
||||||
|
(choice_of_string oponent_s, outcome_of_string outcome_s)
|
||||||
|
|> (fun (oponent, outcome) -> (oponent, (choice_of_outcome oponent outcome))))
|
||||||
|
|
||||||
|
open Utils
|
||||||
|
|
||||||
|
let _ = "\n1: " ^ (string_of_int (read_file "bin/d2/input.txt" |> d2_1)) ^
|
||||||
|
"\n2: " ^ (string_of_int (read_file "bin/d2/input.txt" |> d2_2)) |> print_endline
|
Loading…
Reference in a new issue