diff options
| author | Alexandre Jesus <adbjesus@gmail.com> | 2025-12-07 11:39:04 +0000 |
|---|---|---|
| committer | Alexandre Jesus <adbjesus@gmail.com> | 2025-12-07 11:52:43 +0000 |
| commit | 71329a6615c753e7f90dd1f2457b413bfe86fe40 (patch) | |
| tree | 278782c10015d26721a980fe4a907ccb4534ae88 | |
| parent | 5f4461fe96d07bef69cb59f5ce7182848340a951 (diff) | |
| download | aoc2025-71329a6615c753e7f90dd1f2457b413bfe86fe40.tar.gz aoc2025-71329a6615c753e7f90dd1f2457b413bfe86fe40.zip | |
Day 7
| -rw-r--r-- | bin/main.ml | 2 | ||||
| m--------- | data | 0 | ||||
| -rw-r--r-- | lib/day07.ml | 38 |
3 files changed, 40 insertions, 0 deletions
diff --git a/bin/main.ml b/bin/main.ml index ebde0e3..fb1c28e 100644 --- a/bin/main.ml +++ b/bin/main.ml @@ -38,6 +38,8 @@ let day_part_fn day part = | 5, 2 -> Day05.part2 | 6, 1 -> Day06.part1 | 6, 2 -> Day06.part2 + | 7, 1 -> Day07.part1 + | 7, 2 -> Day07.part2 | _ -> failwith (Format.sprintf "Day %d, part %d, has not yet been implemented\n" day diff --git a/data b/data -Subproject fade3292f33ea16f360782f0c55d3ce2b72ea5a +Subproject 472b90e921b561b5e58effab41ea99690b5e5b9 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 |
