Compare commits

...

10 Commits

9 changed files with 149 additions and 62 deletions

3
.gitignore vendored
View File

@@ -1,2 +1,3 @@
_build/ _build/
input.txt _opam/
input*.txt

View File

@@ -13,5 +13,9 @@
], ],
"editor.formatOnSave": true "editor.formatOnSave": true
}, },
"sarif-viewer.connectToGithubCodeScanning": "off" "sarif-viewer.connectToGithubCodeScanning": "off",
"ocaml.sandbox": {
"kind": "opam",
"switch": "${workspaceFolder:ocaml-aoc}"
}
} }

View File

@@ -1,71 +1,39 @@
(** [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_ints_of_string s =
match (nums_from_string s) with match Aoc.ints_of_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")
(** [unzip lst] takes a list of pairs and returns a pair of lists. *) (** [rev_split lst] takes a list of pairs and returns a pair of lists. Is
let unzip lst = equivalent to List.split (List.rev lst) but more efficient (and tail
recursive). *)
let rev_split lst =
let rec impl acc acc' = function let rec impl acc acc' = function
| (h, h') :: t -> impl (h :: acc) (h' :: acc') t | (h, h') :: t -> impl (h :: acc) (h' :: acc') t
| _ -> (acc, acc') | _ -> (acc, acc')
in in
impl [] [] lst impl [] [] lst
(** [pairs_from_channel ch] returns the list of pairs given on the channel
*)
let pairs_from_channel ch =
let rec impl acc =
try (impl ((input_line ch) :: acc)) with
| End_of_file -> acc
in
impl [] |> List.map pair_nums_from_string
(** [pairs_from_file fname] returns the list of pairs given in the file *)
let pairs_from_file fname =
try
let ch = open_in fname in
pairs_from_channel ch
with
| _ -> failwith "pairs_from_file"
(** [distance a b] returns the absolute difference between [a] and [b]. *)
let distance a b =
abs (a - b)
let day2401a fname =
let (a, b) = unzip (pairs_from_file fname) in
let d = List.map2 distance (List.sort Int.compare a) (List.sort Int.compare b) in
List.fold_left ( + ) 0 d
(** [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 =
List.fold_left (fun acc x -> if x = n then (acc + 1) else acc) 0 lst List.fold_left (fun acc x -> if x = n then acc + 1 else acc) 0 lst
let day2401b fname =
let (a, b) = unzip (pairs_from_file fname) in
List.map (count b) a |>
List.map2 ( * ) a |>
List.fold_left ( + ) 0
let _ = try (** [accumulate lst] sums all the elements of [lst]. *)
begin let accumulate = List.fold_left ( + ) 0
match Sys.argv with
| [|_; fname|] -> (** [lists_from_file fname] Read two lists of integers from [fname] and return
Printf.printf "Part 1 = %d\n" (day2401a fname); as a pair. *)
Printf.printf "Part 2 = %d\n" (day2401b fname); let lists_of_file fname =
| _ -> Aoc.strings_of_file fname |> List.map pair_ints_of_string |> rev_split
Printf.printf "Usage: day2401 <fname>\n";
exit 1 let day2401a (a, b) =
end List.map2 Aoc.distance1 (List.sort Int.compare a) (List.sort Int.compare b)
with |> accumulate
| e ->
Printf.printf "An error occured: %s\n" (Printexc.to_string e); let day2401b (a, b) = List.map (count b) a |> List.map2 ( * ) a |> accumulate
exit 1
let _ =
Aoc.main lists_of_file
[ (string_of_int, day2401a); (string_of_int, day2401b) ]

25
bin/day2402.ml Normal file
View File

@@ -0,0 +1,25 @@
(** [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 ints_of_file fname =
Aoc.strings_of_file fname |> List.map Aoc.ints_of_string
let day2402a lsts = List.filter is_safe lsts |> List.length
let day2402b lsts = List.filter is_safe_dampened lsts |> List.length
let _ =
Aoc.main ints_of_file [ (string_of_int, day2402a); (string_of_int, day2402b) ]

46
bin/day2403.ml Normal file
View File

@@ -0,0 +1,46 @@
(** [matched_group_opt n s] returns [Some (Str.matched_group n s)] if the [n]th
group matched, or [None] if the group did not match. *)
let matched_group_opt n s = try Some (Str.matched_group n s) with _ -> None
(** [find_nums s] returns the list of instructions given in [s]. *)
let instrs_of_string s =
let r =
Str.regexp
{|do()\|don't()\|mul(\([0-9][0-9]?[0-9]?\),\([0-9][0-9]?[0-9]?\))|}
in
let rec impl acc pos =
try
let _ = Str.search_forward r s pos in
let p0 = Str.matched_group 0 s in
let p1 = matched_group_opt 1 s in
let p2 = matched_group_opt 2 s in
impl
((p0, Option.map int_of_string p1, Option.map int_of_string p2) :: acc)
(Str.match_end ())
with Not_found -> acc
in
List.rev (impl [] 0)
let instrs_of_file fname =
Aoc.strings_of_file fname |> List.map instrs_of_string |> List.concat
(** [mac_opt acc a b] returns [acc + a' * b'] if [a = Some a'] and
[b = Some b']. If either [a] or [b] are [None] then the result is [acc]. *)
let mac_opt acc a b =
acc + (Option.value a ~default:0 * Option.value b ~default:0)
let day2403a = List.fold_left (fun acc (_, a, b) -> mac_opt acc a b) 0
let day2403b lst =
let rec impl acc enabled = function
| [] -> acc
| ("do()", _, _) :: t -> impl acc true t
| ("don't()", _, _) :: t -> impl acc false t
| (_, a, b) :: t ->
if enabled then impl (mac_opt acc a b) true t else impl acc false t
in
impl 0 true lst
let _ =
Aoc.main instrs_of_file
[ (string_of_int, day2403a); (string_of_int, day2403b) ]

View File

@@ -1,4 +1,4 @@
(executables (executables
(public_names day2401) (public_names day2401 day2402 day2403)
(names day2401) (names day2401 day2402 day2403)
(libraries str)) (libraries str aoc))

22
lib/aoc.ml Normal file
View File

@@ -0,0 +1,22 @@
let ints_of_string s = List.map int_of_string (Str.split (Str.regexp " +") s)
let distance1 a b = abs (a - b)
let strings_of_file fname =
In_channel.with_open_text fname In_channel.input_lines
let main prep parts =
try
match Sys.argv with
| [| _; fname |] ->
let lines = prep fname in
let do_part i (fmt, fn) =
Printf.printf "Part %d = %s\n" (i + 1) (fmt (fn lines))
in
List.iteri do_part parts;
exit 0
| _ ->
Printf.printf "Usage: %s <fname>\n" Sys.executable_name;
exit 2
with e ->
Printf.printf "An error occured: %s\n" (Printexc.to_string e);
exit 1

18
lib/aoc.mli Normal file
View File

@@ -0,0 +1,18 @@
val ints_of_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_of_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. *)
val main : (string -> 'a) -> (('b -> string) * ('a -> 'b)) list -> unit
(** [main prep parts] executes an advent of code problem. [prep fname] should
be a function that returns the input from [fname]. Each elemet of
[parts] is a pair of functions. The first converts the output to a string
(for example [string_of_int]). The second executes the given part.
Output is given as if done by:
[print_string ( prep fname |> snd |> fst )] *)

3
lib/dune Normal file
View File

@@ -0,0 +1,3 @@
(library
(name aoc)
(libraries str))