From 71329a6615c753e7f90dd1f2457b413bfe86fe40 Mon Sep 17 00:00:00 2001 From: Alexandre Jesus Date: Sun, 7 Dec 2025 11:39:04 +0000 Subject: Day 7 --- lib/day07.ml | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 lib/day07.ml (limited to 'lib') 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 + * + * 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 -- cgit v1.2.3