Mercurial > lbo > hg > aoc22
changeset 40:4ff5d91ffc8c
Add comments for Day 3
author | Lewin Bormann <lbo@spheniscida.de> |
---|---|
date | Sun, 03 Dec 2023 15:11:01 +0100 |
parents | 148faba1454d |
children | d4baa11fdac8 |
files | 2023/day03.ml |
diffstat | 1 files changed, 8 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/2023/day03.ml Sun Dec 03 12:39:11 2023 +0100 +++ b/2023/day03.ml Sun Dec 03 15:11:01 2023 +0100 @@ -95,14 +95,17 @@ let pr = parse_line line lineno in Sexp.to_string_hum (List.sexp_of_t sexp_of_item pr) + (* fold over string lines and parse each line into `item list` *) let lines_folder (count, lines) line = let parsed = parse_line line count in (count + 1, parsed :: lines) + (* read all lines from ch *) let read_lines ch = let _, all_lines = In_channel.fold_lines ch ~init:(0, []) ~f:lines_folder in all_lines + (* build a hashmap of position -> symbol for all symbols in lines *) let build_symbol_map all_lines = let symmap = create_position_tbl () in let f = function @@ -114,11 +117,13 @@ List.iter ~f:ff all_lines; symmap + (* return all number items from item list list (list of parsed lines) *) let all_numbers all_lines = let f = function Number _ -> true | _ -> false in let ff = List.filter ~f in List.concat (List.map ~f:ff all_lines) + (* print symbol map for lines received on ch *) let _debug_symbol_map ch = let all_lines = read_lines ch in let symmap = build_symbol_map all_lines in @@ -128,6 +133,7 @@ (fun (a, b) -> List [ sexp_of_position a; Atom (Char.to_string b) ]) alist) + (* create a counting sequence *) let count_seq (from : int) (upto : int) : int Sequence.t = let f c = if Int.equal c upto then None @@ -135,6 +141,7 @@ in Sequence.unfold ~init:from ~f + (* generate adjacent positions for number at x, y with number of digits. *) let adjacent_positions { x; y } digits = let corners = [ @@ -156,6 +163,7 @@ in List.concat [ corners; upper; lower ] + (* check if there is a symbol at any adjacent position in the symbol map *) let rec check_adjacent_symbol symmap = function | [] -> false | (x, y) :: poss ->