diff options
author | Brian Chu <brianmchu42@gmail.com> | 2022-08-20 10:19:53 -0700 |
---|---|---|
committer | Brian Chu <brianmchu42@gmail.com> | 2022-08-20 10:19:53 -0700 |
commit | c9a6741617eb28834674a5ef84daae4366ac4101 (patch) | |
tree | d0bf55bc68b10d3d152ddbf43d7545cd9e94ed75 /lib/day1.ml | |
parent | 484c4a998f9782eb0b84ec9121d6718fa6d35c1d (diff) | |
download | AdventOfCode2018-c9a6741617eb28834674a5ef84daae4366ac4101.tar.gz |
set up solution skeletons, driver framework, and solution for day 1
Diffstat (limited to 'lib/day1.ml')
-rw-r--r-- | lib/day1.ml | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/lib/day1.ml b/lib/day1.ml new file mode 100644 index 0000000..748b529 --- /dev/null +++ b/lib/day1.ml @@ -0,0 +1,25 @@ +open Core + +(* Take text input from file and process accordingly *) +let process_input input = String.split_lines input |> List.map ~f:int_of_string + +(* Fill in solutions for each part *) +let part_1 input = + let freq_changes = process_input input in + List.fold freq_changes ~init:0 ~f:(+) + +let part_2 input = + let freq_changes = process_input input |> Sequence.cycle_list_exn in + let counter = Hashtbl.create (module Int) in + let increment key = Hashtbl.update_and_return counter key + ~f:(function + | Some num -> num + 1 + | None -> 1) in + Sequence.fold_until freq_changes ~init:0 + ~f:(fun sum freq -> + if (increment (sum + freq)) = 2 + then + Stop (sum + freq) + else + Continue (sum + freq)) + ~finish:(fun sum -> sum) \ No newline at end of file |