summaryrefslogtreecommitdiffstats
path: root/lib/day07.ml
diff options
context:
space:
mode:
authorAlexandre Jesus <adbjesus@gmail.com>2025-12-07 11:39:04 +0000
committerAlexandre Jesus <adbjesus@gmail.com>2025-12-07 11:52:43 +0000
commit71329a6615c753e7f90dd1f2457b413bfe86fe40 (patch)
tree278782c10015d26721a980fe4a907ccb4534ae88 /lib/day07.ml
parent5f4461fe96d07bef69cb59f5ce7182848340a951 (diff)
downloadaoc2025-71329a6615c753e7f90dd1f2457b413bfe86fe40.tar.gz
aoc2025-71329a6615c753e7f90dd1f2457b413bfe86fe40.zip
Day 7
Diffstat (limited to 'lib/day07.ml')
-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