Compare commits
No commits in common. "fe417025bde01d2dc0660e9a1581c3ffec8a5c96" and "da30155acfe21f4b1ce0d1bded04fd618274a35b" have entirely different histories.
fe417025bd
...
da30155acf
|
@ -0,0 +1 @@
|
|||
inputs
|
2256
inputs/1.txt
2256
inputs/1.txt
File diff suppressed because it is too large
Load Diff
2500
inputs/2.txt
2500
inputs/2.txt
File diff suppressed because it is too large
Load Diff
17
ocaml/1.ml
17
ocaml/1.ml
|
@ -1,19 +1,12 @@
|
|||
#load "str.cma" ;;
|
||||
#use "lib/file_utils.ml" ;;
|
||||
|
||||
let top n calories =
|
||||
let topn = List.filteri (fun i _ -> i < n) calories in
|
||||
let numcal = List.fold_left (+) 0 topn in
|
||||
Printf.printf "Top %d: %d\n" n numcal
|
||||
;;
|
||||
|
||||
let order_desc a b = if a < b then 1 else -1 in
|
||||
let contents = read_file "inputs/1.txt" in
|
||||
let calories = Str.split (Str.regexp "\n\n") contents
|
||||
let maxcal = Str.split (Str.regexp "\n\n") contents
|
||||
|> List.map (String.split_on_char '\n')
|
||||
|> List.map (List.map (int_of_string))
|
||||
|> List.map (List.fold_left (+) 0)
|
||||
|> List.sort order_desc
|
||||
in
|
||||
top 1 calories;
|
||||
top 3 calories
|
||||
|> List.fold_left (max) 0
|
||||
in
|
||||
|
||||
Printf.printf "%d" maxcal
|
||||
|
|
73
ocaml/2.ml
73
ocaml/2.ml
|
@ -1,73 +0,0 @@
|
|||
#use "lib/file_utils.ml" ;;
|
||||
|
||||
type shape = Rock | Paper | Scissors
|
||||
type outcome = Win | Tie | Loss
|
||||
|
||||
let shape_of_char = function
|
||||
| 'A' | 'X' -> Rock
|
||||
| 'B' | 'Y' -> Paper
|
||||
| 'C' | 'Z' -> Scissors
|
||||
| s -> failwith ("invalid shape: " ^ (String.make 1 s))
|
||||
|
||||
let score_of_shape = function
|
||||
| Rock -> 1
|
||||
| Paper -> 2
|
||||
| Scissors -> 3
|
||||
|
||||
let score_of_outcome = function
|
||||
| Loss -> 0
|
||||
| Tie -> 3
|
||||
| Win -> 6
|
||||
|
||||
let part1 line =
|
||||
let outcome_of_round = function
|
||||
| Rock, Scissors -> Win
|
||||
| Paper, Rock -> Win
|
||||
| Scissors, Paper -> Win
|
||||
| a, b when a = b -> Tie
|
||||
| _ -> Loss
|
||||
in
|
||||
let op_move = shape_of_char (String.get line 0) in
|
||||
let my_move = shape_of_char (String.get line 2) in
|
||||
let outcome = outcome_of_round(my_move, op_move) in
|
||||
let outcome_score = score_of_outcome outcome in
|
||||
let shape_score = score_of_shape my_move in
|
||||
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 lines = String.split_on_char '\n' contents
|
||||
|> List.filter (fun l -> String.trim l <> "")
|
||||
in
|
||||
play lines 1 part1;
|
||||
play lines 2 part2
|
Loading…
Reference in New Issue