2024 day 22 tidy up.
This commit is contained in:
@@ -1,15 +1,4 @@
|
|||||||
let next_secret secret =
|
(** Module describing a tuple of four integers, used for the map keys later. *)
|
||||||
let secret = secret * 64 lxor secret mod 16777216 in
|
|
||||||
let secret = secret / 32 lxor secret mod 16777216 in
|
|
||||||
let secret = secret * 2048 lxor secret mod 16777216 in
|
|
||||||
secret
|
|
||||||
|
|
||||||
let nth_secret n secret =
|
|
||||||
let r = Aoc.apply_n n next_secret secret in
|
|
||||||
r
|
|
||||||
|
|
||||||
let part1 n nums = List.map (nth_secret n) nums |> List.fold_left ( + ) 0
|
|
||||||
|
|
||||||
module Int4Tuple = struct
|
module Int4Tuple = struct
|
||||||
type t = int * int * int * int
|
type t = int * int * int * int
|
||||||
|
|
||||||
@@ -17,16 +6,31 @@ module Int4Tuple = struct
|
|||||||
end
|
end
|
||||||
|
|
||||||
module Int4Map = Map.Make (Int4Tuple)
|
module Int4Map = Map.Make (Int4Tuple)
|
||||||
|
(** Map keyed by a tuple of 4 integers *)
|
||||||
|
|
||||||
|
(** [next_secret secret] returns the next secret value after [secret]. *)
|
||||||
|
let next_secret secret =
|
||||||
|
let secret = secret * 64 lxor secret mod 16777216 in
|
||||||
|
let secret = secret / 32 lxor secret mod 16777216 in
|
||||||
|
let secret = secret * 2048 lxor secret mod 16777216 in
|
||||||
|
secret
|
||||||
|
|
||||||
|
let part1 n nums =
|
||||||
|
List.map (Aoc.apply_n n next_secret) nums |> List.fold_left ( + ) 0
|
||||||
|
|
||||||
|
(** [secret_list n secret] returns a list containing the [n] secrets after
|
||||||
|
[secret]. *)
|
||||||
let secret_list n secret =
|
let secret_list n secret =
|
||||||
let rec impl s () = Seq.Cons (s, impl (next_secret s)) in
|
let rec impl s () = Seq.Cons (s, impl (next_secret s)) in
|
||||||
Seq.drop 1 (impl secret) |> Seq.take n |> List.of_seq
|
Seq.drop 1 (impl secret) |> Seq.take n |> List.of_seq
|
||||||
|
|
||||||
let rec find_sequence_costs map =
|
(** [find_sequence_values map lst] updates [map] to contain the value of the
|
||||||
|
sale for the first occurance in each sequence of 4 differences in [lst]. *)
|
||||||
|
let rec find_sequence_values map =
|
||||||
let update_value amt = function None -> Some amt | x -> x in
|
let update_value amt = function None -> Some amt | x -> x in
|
||||||
function
|
function
|
||||||
| a :: b :: c :: d :: e :: t ->
|
| a :: b :: c :: d :: e :: t ->
|
||||||
find_sequence_costs
|
find_sequence_values
|
||||||
(Int4Map.update (b - a, c - b, d - c, e - d) (update_value e) map)
|
(Int4Map.update (b - a, c - b, d - c, e - d) (update_value e) map)
|
||||||
(b :: c :: d :: e :: t)
|
(b :: c :: d :: e :: t)
|
||||||
| _ -> map
|
| _ -> map
|
||||||
@@ -40,19 +44,17 @@ let part2 n secrets =
|
|||||||
| None, None -> None
|
| None, None -> None
|
||||||
in
|
in
|
||||||
let costs =
|
let costs =
|
||||||
List.map (secret_list n) secrets
|
List.map (secret_list n) secrets (* list of lists of secrets *)
|
||||||
|> List.map (List.map (fun x -> x mod 10))
|
|> List.map (List.map (fun x -> x mod 10)) (* list of lists of values *)
|
||||||
|> List.map (find_sequence_costs Int4Map.empty)
|
|> List.map (find_sequence_values Int4Map.empty) (* sequence -> value map *)
|
||||||
|> List.fold_left
|
|> List.fold_left (* merge maps - adding values of same key *)
|
||||||
(fun acc map -> Int4Map.merge merge_values acc map)
|
(fun acc map -> Int4Map.merge merge_values acc map)
|
||||||
Int4Map.empty
|
Int4Map.empty
|
||||||
in
|
in
|
||||||
Int4Map.fold (fun _ v acc -> max acc v) costs 0
|
Int4Map.fold (fun _ v acc -> max acc v) costs 0 (* find max value *)
|
||||||
|
|
||||||
let read_file fname = Aoc.strings_of_file fname |> List.map int_of_string
|
let read_file fname = Aoc.strings_of_file fname |> List.map int_of_string
|
||||||
|
|
||||||
let _ =
|
let _ =
|
||||||
Aoc.main read_file
|
Aoc.main read_file
|
||||||
[ (string_of_int, part1 2000); (string_of_int, part2 2000) ]
|
[ (string_of_int, part1 2000); (string_of_int, part2 2000) ]
|
||||||
|
|
||||||
(* 424 too low *)
|
|
||||||
|
Reference in New Issue
Block a user