summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/day07.ml38
1 files changed, 38 insertions, 0 deletions
diff --git a/lib/day07.ml b/lib/day07.ml
new file mode 100644
index 0000000..56de649
--- /dev/null
+++ b/lib/day07.ml
@@ -0,0 +1,38 @@
+(*
+ * SPDX-FileCopyrightText: Copyright 2025 Alexandre Jesus <https://adbjesus.com>
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ *)
+
+let solve transition_fn answer_fn ch =
+ (* the following assumes tachyon splitters are not adjacent *)
+ (* it also assume that the input is a perfect grid *)
+ let lines = In_channel.input_lines ch in
+ let start = String.index (List.hd lines) 'S' in
+ let m = String.length (List.hd lines) in
+ let cur = Array.make m 0 in
+ cur.(start) <- 1;
+ List.iter (String.iteri (transition_fn cur)) (List.tl lines);
+ Printf.printf "%d\n" (answer_fn cur)
+
+let part1 ch =
+ let ans = ref 0 in
+ let transition_fn cur j c =
+ if c = '^' then (
+ cur.(j - 1) <- Int.logor cur.(j - 1) cur.(j);
+ cur.(j + 1) <- Int.logor cur.(j + 1) cur.(j);
+ ans := !ans + cur.(j);
+ cur.(j) <- 0)
+ in
+ let answer_fn _cur = !ans in
+ solve transition_fn answer_fn ch
+
+let part2 ch =
+ let transition_fn cur j c =
+ if c = '^' then (
+ cur.(j - 1) <- cur.(j - 1) + cur.(j);
+ cur.(j + 1) <- cur.(j + 1) + cur.(j);
+ cur.(j) <- 0)
+ in
+ let answer_fn cur = Array.fold_left ( + ) 0 cur in
+ solve transition_fn answer_fn ch