summary refs log tree commit diff stats
path: root/lib/day1.ml
diff options
context:
space:
mode:
Diffstat (limited to 'lib/day1.ml')
-rw-r--r--lib/day1.ml25
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