Add day 2402.
This commit is contained in:
@@ -1,11 +1,7 @@
|
|||||||
(** [nums_from_string s] takes a string of space separated integers and gives
|
|
||||||
back a list of the integers. *)
|
|
||||||
let nums_from_string s = List.map int_of_string (Str.split (Str.regexp " +") s)
|
|
||||||
|
|
||||||
(** [pair_nums_from_string s] takes a string of two numbers separated by
|
(** [pair_nums_from_string s] takes a string of two numbers separated by
|
||||||
whitespace and returns the pair of the numbers *)
|
whitespace and returns the pair of the numbers *)
|
||||||
let pair_nums_from_string s =
|
let pair_nums_from_string s =
|
||||||
match nums_from_string s with
|
match Aoc.nums_from_string s with
|
||||||
| [ h; h' ] -> (h, h')
|
| [ h; h' ] -> (h, h')
|
||||||
| _ -> raise (Invalid_argument "pair_nums_from_string")
|
| _ -> raise (Invalid_argument "pair_nums_from_string")
|
||||||
|
|
||||||
@@ -19,9 +15,6 @@ let rev_split lst =
|
|||||||
in
|
in
|
||||||
impl [] [] lst
|
impl [] [] lst
|
||||||
|
|
||||||
(** [distance a b] returns the absolute difference between [a] and [b]. *)
|
|
||||||
let distance a b = abs (a - b)
|
|
||||||
|
|
||||||
(** [count lst n] counts the number of times [n] appears as an element in [lst].
|
(** [count lst n] counts the number of times [n] appears as an element in [lst].
|
||||||
*)
|
*)
|
||||||
let count lst n =
|
let count lst n =
|
||||||
@@ -33,12 +26,10 @@ let accumulate = List.fold_left ( + ) 0
|
|||||||
(** [lists_from_file fname] Read two lists of integers from [fname] and return
|
(** [lists_from_file fname] Read two lists of integers from [fname] and return
|
||||||
as a pair. *)
|
as a pair. *)
|
||||||
let lists_from_file fname =
|
let lists_from_file fname =
|
||||||
In_channel.with_open_text fname In_channel.input_lines
|
Aoc.strings_from_file fname |> List.map pair_nums_from_string |> rev_split
|
||||||
|> List.map pair_nums_from_string
|
|
||||||
|> rev_split
|
|
||||||
|
|
||||||
let day2401a a b =
|
let day2401a a b =
|
||||||
List.map2 distance (List.sort Int.compare a) (List.sort Int.compare b)
|
List.map2 Aoc.distance1 (List.sort Int.compare a) (List.sort Int.compare b)
|
||||||
|> accumulate
|
|> accumulate
|
||||||
|
|
||||||
let day2401b a b = List.map (count b) a |> List.map2 ( * ) a |> accumulate
|
let day2401b a b = List.map (count b) a |> List.map2 ( * ) a |> accumulate
|
||||||
|
36
bin/day2402.ml
Normal file
36
bin/day2402.ml
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
(** [is_monotonic pred lst] returns true if [pred] returns true when tested on
|
||||||
|
all consecutive elements of [lst]. *)
|
||||||
|
let rec is_monotonic pred = function
|
||||||
|
| [] | _ :: [] -> true
|
||||||
|
| h :: h' :: t -> pred h h' && is_monotonic pred (h' :: t)
|
||||||
|
|
||||||
|
let is_safe lst =
|
||||||
|
(is_monotonic ( < ) lst || is_monotonic ( > ) lst)
|
||||||
|
&& is_monotonic (fun a b -> Aoc.distance1 a b <= 3) lst
|
||||||
|
|
||||||
|
let is_safe_dampened lst =
|
||||||
|
let rec impl acc = function
|
||||||
|
| [] -> is_safe acc
|
||||||
|
| h :: t -> is_safe (acc @ t) || impl (acc @ [ h ]) t
|
||||||
|
in
|
||||||
|
impl [] lst
|
||||||
|
|
||||||
|
let nums_from_file fname =
|
||||||
|
Aoc.strings_from_file fname |> List.map Aoc.nums_from_string
|
||||||
|
|
||||||
|
let day2402a lsts = List.filter is_safe lsts |> List.length
|
||||||
|
let day2402b lsts = List.filter is_safe_dampened lsts |> List.length
|
||||||
|
|
||||||
|
let _ =
|
||||||
|
try
|
||||||
|
match Sys.argv with
|
||||||
|
| [| _; fname |] ->
|
||||||
|
let lines = nums_from_file fname in
|
||||||
|
Printf.printf "Part 1 = %d\n" (day2402a lines);
|
||||||
|
Printf.printf "Part 2 = %d\n" (day2402b lines)
|
||||||
|
| _ ->
|
||||||
|
Printf.printf "Usage: day2402 <fname>\n";
|
||||||
|
exit 1
|
||||||
|
with e ->
|
||||||
|
Printf.printf "An error occured: %s\n" (Printexc.to_string e);
|
||||||
|
exit 1
|
6
bin/dune
6
bin/dune
@@ -1,4 +1,4 @@
|
|||||||
(executables
|
(executables
|
||||||
(public_names day2401)
|
(public_names day2401 day2402)
|
||||||
(names day2401)
|
(names day2401 day2402)
|
||||||
(libraries str))
|
(libraries str aoc))
|
||||||
|
5
lib/aoc.ml
Normal file
5
lib/aoc.ml
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
let nums_from_string s = List.map int_of_string (Str.split (Str.regexp " +") s)
|
||||||
|
let distance1 a b = abs (a - b)
|
||||||
|
|
||||||
|
let strings_from_file fname =
|
||||||
|
In_channel.with_open_text fname In_channel.input_lines
|
10
lib/aoc.mli
Normal file
10
lib/aoc.mli
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
val nums_from_string : string -> int list
|
||||||
|
(** [nums_from_string s] takes a string of space separated integers and gives
|
||||||
|
back a list of the integers. *)
|
||||||
|
|
||||||
|
val distance1 : int -> int -> int
|
||||||
|
(** [distance1 a b] returns the absolute difference between [a] and [b]. *)
|
||||||
|
|
||||||
|
val strings_from_file : string -> string list
|
||||||
|
(** [strings_from_file fname] returns a list of strings from the file
|
||||||
|
[fname]. Each string represents a line from the file. *)
|
Reference in New Issue
Block a user