just keep the strings
This commit is contained in:
parent
e0b2dfe566
commit
3c2e61176a
1 changed files with 10 additions and 22 deletions
32
day3.ml
32
day3.ml
|
@ -1,38 +1,26 @@
|
||||||
type space = Tree | Free
|
type map = string array
|
||||||
type map = space array array
|
|
||||||
|
|
||||||
let (.%()) arr i = arr.(i mod Array.length arr)
|
let (.%[]) str i = str.[i mod String.length str]
|
||||||
|
let (+=) r x = r := !r + x
|
||||||
|
|
||||||
let iter_map ?(step_x=1) ?(step_y=1) f arr =
|
let iter_map ?(step_x=1) ?(step_y=1) f arr =
|
||||||
let i = ref 0 in
|
let i = ref 0 in let j = ref 0 in
|
||||||
let j = ref 0 in
|
|
||||||
while !i < Array.length arr do
|
while !i < Array.length arr do
|
||||||
f arr.(!i).%(!j);
|
f arr.(!i).%[!j];
|
||||||
i := !i + step_x;
|
i += step_x; j += step_y
|
||||||
j := !j + step_y
|
|
||||||
done
|
done
|
||||||
|
|
||||||
|
|
||||||
exception Unknown_char of char
|
let read_file = Bracket.infile_lines ~line:Fun.id ~of_seq:Array.of_seq
|
||||||
|
|
||||||
let make_line str =
|
|
||||||
Array.init (String.length str)
|
|
||||||
(fun i -> match str.[i] with
|
|
||||||
| '#' -> Tree
|
|
||||||
| '.' -> Free
|
|
||||||
| c -> raise (Unknown_char c))
|
|
||||||
|
|
||||||
let read_file = Bracket.infile_lines ~line:make_line ~of_seq:Array.of_seq
|
|
||||||
|
|
||||||
|
|
||||||
let count_trees ?step_x ?step_y map =
|
let count_trees ?step_x ?step_y map =
|
||||||
let count = ref 0 in
|
let count = ref 0 in
|
||||||
iter_map ?step_x ?step_y (function Tree -> incr count | _ -> ()) map;
|
iter_map ?step_x ?step_y (fun c -> if c = '#' then incr count) map;
|
||||||
!count
|
!count
|
||||||
|
|
||||||
let main1 input =
|
let main1 input =
|
||||||
Format.printf "%d trees\n"
|
Format.printf "%d\n" (count_trees ~step_y:3 (read_file input))
|
||||||
(count_trees ~step_y:3 (read_file input))
|
|
||||||
|
|
||||||
let main2 input =
|
let main2 input =
|
||||||
let map = read_file input in
|
let map = read_file input in
|
||||||
|
@ -47,7 +35,7 @@ let main = Misc.main 3 [|main1; main2|]
|
||||||
|
|
||||||
let%expect_test _ =
|
let%expect_test _ =
|
||||||
main1 "../../data/day3";
|
main1 "../../data/day3";
|
||||||
[%expect{| 244 trees |}]
|
[%expect{| 244 |}]
|
||||||
|
|
||||||
let%expect_test _ =
|
let%expect_test _ =
|
||||||
main2 "../../data/day3";
|
main2 "../../data/day3";
|
||||||
|
|
Loading…
Add table
Reference in a new issue