Browse Source

day three

Getty Ritter 1 year ago
parent
commit
1c668d671a
5 changed files with 435 additions and 0 deletions
  1. 14 0
      03/dune
  2. 323 0
      03/input
  3. 47 0
      03/lib.ml
  4. 14 0
      03/main.ml
  5. 37 0
      03/test.ml

+ 14 - 0
03/dune

@@ -0,0 +1,14 @@
+(library
+  (name Lib)
+  (modules Lib)
+  (libraries base stdio core))
+
+(executable
+ (name main)
+ (modules Main)
+ (libraries Lib base stdio))
+
+(executable
+ (name test)
+ (modules Test)
+ (libraries Lib oUnit))

+ 323 - 0
03/input

@@ -0,0 +1,323 @@
+...#...#..#....#..#...#..##..#.
+.#..#.....#.#............###...
+.#...###....#.............##..#
+...##...##....#.....##..#.##...
+.....###.#.###..##.#.##.......#
+#...##.....#..........#..#.#.#.
+......##.......##..#....#.#....
+....#.###.##..#.#..##.##....#.#
+.......#.......###.#.#.##.....#
+.........#.#....#..........#.#.
+.#...##.....##.........#..#....
+.##....#.#.#...##......#.......
+##.#.#..#....#....#....#...#.#.
+##....#.#..##......#....##...#.
+....#..#..##..#.###.......#.#..
+.....##....###...........#.#.##
+#.....##.........#....##......#
+........###.#..#....#....#.....
+...#.......#.##..#.###......#..
+...............#..#....#.##....
+..#..###..#.#..#.........##..#.
+####..#..####..................
+#...####...#.......#.#.#...#...
+......###.....#......#..#..#...
+#...#.....##.....#.#..##...#.#.
+#...........##.......#.........
+.#..#.........#.#..##....#.....
+........##...#................#
+........#.###.#.###.#.#.##..##.
+.#....##.....#...##.#..#.#.....
+..#..#.....###....##.#....#.#.#
+#......##.##...##..#.........#.
+#..#..#.....#.....#.........#..
+#....#.#...###.........#...#...
+.#.#.....##......#.#......#....
+..##......##...#.#.#.#.........
+..#......#.....##.###.#.#..#...
+....#..#.......#..#..#.....#...
+.#.#.....#...#..........#......
+#.#..#...........#.#.##.#...#.#
+..#.#....###...#...#.....#.#...
+....##.#.###....####.......#...
+.....##....#.......#..#..#....#
+...##..#.#.#.#......#......#...
+...##...#....#...#......###...#
+........#..#.#.....#.###.......
+..#..##.#....#.#.........#...#.
+.....#.####....#.##.........#..
+......#...#...#.....#......###.
+.##.....#....#..#.#....#.....#.
+...........#...#....##..#...#..
+.....#....#.....#...##..#...#.#
+.#...#.........#.......#...#..#
+...#..#...#........#......#....
+..#..#####.#.....#.#....#...#.#
+...#.......#.#....#...##..#..#.
+####..#.#.###.#.#..............
+.##........#...#.#....#..#.....
+..#..............#.#..##...#.##
+.###.#.....#.#.....##.#......##
+....###.....#...#...#.#..#.....
+....###.#.##.......#....#...#..
+#..#...#......##..#.....#.#...#
+....#.#.........#..............
+#.##.##...#..#.#.#.....#...#.##
+#...#...#......#...........##..
+#.#.#......#............#.#....
+.#.#..######...#.#.........#.##
+..#.#..#...#......#............
+....#.....#......##..#.....#...
+.##............#....##..#......
+.#.#.#...#.##.............###.#
+#.#...#...#.....#....#.#.#.....
+........#..#......##.##.#.....#
+.....#.....#.#####...#....#....
+.#...#......#.........#.#......
+...#...#..##.....##....#..#....
+....#....##..#.........#.......
+..#........##..#.#........#....
+...#...##...........#...#....#.
+.....##.........#..#....#..#.#.
+#..#....##..#...##.....#..##.#.
+..#.#.#.#...#...#.....#.#....#.
+.......#.###...#.#.......#.#...
+....#..#..#.###.#.....###..#.#.
+.#..##......#..#..#....#.####..
+..##...........#...#.........#.
+......#..#...#..........#......
+....#..........#......##...#...
+....#..#.##........#.#...##.#..
+#.##......#........##.#...#...#
+#..#....#.....###........##....
+...........##.....##..#....#.##
+..#....#..#..#......#.#.....#..
+#....#.##....#.....##.......#..
+.#.....#.#..............#.##..#
+.#..#..#...#...#....#.#.....#..
+...###...##.#...#..#........#..
+#...#.##.#.....#.#....#..#.....
+#.....###.#.......#.#..#.#..##.
+....#..#..##.......###.#...#...
+.#...####...............#.....#
+.#.##.#.....#.....#.#......##.#
+#...........#.##....###.##....#
+...............#..........#....
+.....#..#.##.###.#.............
+...##.............#.....#.#..#.
+....#.#...#.#..#..#..#....#....
+..#.......#..........#...#...#.
+...............#.#.#...###....#
+....#...#.##....#..##....#.....
+........#.#.##.........##.##.##
+#.....###.......#.#....#..#..##
+.#..#...#......#.#..##.......#.
+#.....#.#........#.##..#..#....
+.###..##.#.......#......###....
+.#...###.....#.....#....###...#
+........##.##......#.#....#...#
+.#....#..#.........#..##...##..
+.......#.......##.#..#..##.....
+#..##..##......#.#......#.##...
+..#..###..#...#....#..#...#....
+#.............#.####.........##
+..#..................#...#..#..
+..#......#........##.......#.#.
+.#.#.#.#..###.....#....#.#.....
+...#.##.###.......#....#.......
+................##...#.....#...
+..#.###.#...#.####....#..#..#..
+..#....###....##..#.#.........#
+.#..#.#.....#........#....##...
+.....#..#......#..#..##.#.#....
+.#..#.........##....##......#..
+.....#.#...#...#.#...#.#...#.#.
+..#..#...#...#...##.#..###.....
+..#..##......#..##.#...##......
+.......#..##....##.#......#..#.
+..#......#.#.....#.##....##....
+..#....#......#......##........
+....##.#.#....#.......#.##.....
+#.....#...###....#....#...#....
+............#.#..#...#...#..#..
+..##.............##....#.......
+.#.......#.##.#......#....##...
+...##............#....#..#...#.
+.##.####.....#.#..###.#....#.##
+....##.#........#..#...#.......
+...#...###.##...........##..#..
+..##..##....#...#..#..........#
+..#.........#.#...##..........#
+.......##....#.#...##.....#..#.
+.............#.....#.#.......#.
+#.......#..##..##...##.#.......
+..............#.....#.#..#...##
+........##..#.....#...#...#.#..
+###.#.................#........
+...#........#...#.#######..#..#
+...#.##...##.#.#..######...#...
+#.......#..#....#..#.##.....#..
+#..#....##....#.##.......#....#
+#...#..#.#.#...#..#.##..#......
+....#..##....#..#.#...........#
+.##..#.#.............###.......
+#....##......#..#..#.....###...
+..#..........#...###.#.........
+.####......#....#......#.#....#
+..#....#.#.#......#....#.......
+.....#.....#....#....#####....#
+.##..........#...#.###....#....
+....##.....##......#...#.#.....
+.#...#...#..#.#.#...#####......
+...#.##..####.##.##.......##...
+............#.......#..........
+.#..##.#..#####........#..#...#
+#......##..##..##.........##...
+....#....#.............#.#....#
+###..#.....#.....#.#...#..#.###
+#...#.......##......#....#.#.#.
+...#......#..#...#....#...###.#
+....#....##.......#....#......#
+............#......##.##.....#.
+...#.........#......#....##..##
+.....##....##...#..###...#..#..
+.......##.#..........#.##.##...
+....##...........#.#..#..#.##.#
+#...#..##.##.#....#....#.#.....
+...##.#.....#..#..#..###....##.
+#.##.#..#..#.#.............#...
+..#.#.............###.....#....
+...#..#....#..#.....#.#..#..#..
+...#.....##.#...........#..##.#
+.........#.#.##..#..#.#...#....
+...#..##..#...#...###.##.#..#..
+.#..##...##......##..##........
+......##....##.#.##.#.#........
+...#..................#.....#..
+.##................#.#..#..###.
+.##.##.....#................#..
+.....#.#..........#...#..#.#..#
+.............#......#..#.#..#..
+...#...##..#........#....#.....
+#......#........##.##...##.....
+##..#..##....#...#............#
+..##..##.##....##..##........#.
+...#....#.#.#.#....#.#...##....
+....#...##..##.#.##...#..#...#.
+#..#....##.#.....#.......#...##
+##.#....#.............#..#.....
+.##..#..#.#.....#.......#.#..#.
+.......#..#...##...#...###..#..
+..........#...#.#..##.....#...#
+..#....#...........#####....#..
+#....#..#.......##.............
+.........##..#####.......##....
+#..#..........#.....###...#..#.
+.#.#.#..#...#.......##...#####.
+.....#....#.###...#.......#....
+#.#.....##...###....###....#...
+.#.....#..#.#.#........#...#...
+.##.#.#.#......#....###....#...
+.#..##..####......###......#...
+......#.#.#.#.#...#...####.##..
+.#........##..#.....#....#....#
+.....###......##..#....#.......
+#.#.##...#.#......###..........
+........#.#...#..#......#....#.
+..##...##.........#.......#.#..
+..#.##....#...##.....#.###.....
+.........#..#.#....#....#.#.##.
+#.........#......#..#.......#..
+...#...##.......#.........#....
+............#......#...........
+##.....#.....#.#...#.....#.....
+..#.#...#..#...#.#...........#.
+#.#.#..#..#...##.#...#.#.....#.
+.#..###.#..##.#.....#.....#....
+##....##....#.......##..##.....
+.#..#...........###..........#.
+.#..#..#..........###..#.......
+#..###......#............##...#
+#......#........#..#..#..#.#...
+.......#.###...#.##............
+.##....#.......#.#...##.....#.#
+....#..#.#.......#.#...........
+##....#.###.#....#.#..##.#....#
+..#..#..#....#...#........##...
+...#...##....#..#.#...#..#.....
+......#..#......#....#.......#.
+#.#..............#...###...#..#
+...#....#..#..........#.#...#..
+#.....##..##.....#........#....
+.#...##..#.#..............#....
+##.#....#..##...#..#.####.#..#.
+.....#.......#.#.#.#..#.....###
+...#.##....#.#........##.......
+#...#.#...#.#..###..##.##...#.#
+###..............#.#.###.......
+...###..#.#..#....##...###.#...
+......##...........#...#..#...#
+.#..#.........##.......#..#...#
+.#.......###......##...#...#...
+.#......##...#........#.......#
+.#..#.....#.........#.#........
+#...#.#.....#...#..##.........#
+......##.#......##.#..##.#.....
+...............#.#..#....#....#
+#....#..#..#..#.#.....##...##..
+#.#......#.###......#..#...####
+.#.#..#...#...#.#..#.##.##.#.#.
+.....#.#...###...#.#.....##....
+...#..#.#..........##.#....#.#.
+...#..#.#.##.....###.##.#....#.
+..........#..###......#..#.#...
+###.....#..###..#...#..###.#...
+..#..#.....##.#.#..###.......#.
+....#....##........##..........
+.......#..........#...#......#.
+.#........#.#.#.#.#.......#....
+.#..#.......##..##....#.#...#..
+.#.#.#.......#..#..............
+#.#....#.#...#.#.#.....#.#...##
+.....#..........##..#.......#..
+.##......#.#....#.#.......#....
+..#.##....#.##.#...#...........
+...##......##..##.............#
+..........##.#.#..#..........#.
+.##....#..#..#.#....##.#...#.#.
+...........#....#.....#.#..#...
+.#.....#....##..#.........#....
+.....#.....#...#....#...#.###.#
+..#....#....#.....#...#......#.
+.....##..#.............#...#...
+........#..#.......#.#.......#.
+#...###..#.##.#...###...##..##.
+....##..#.......#...#.#........
+.#...#.#.##....####........#..#
+.#...#.#.####.##.#.............
+#..##...#....#...#.#.#.#.##..#.
+.#.......#........#.....###....
+#.#.....#....#..#....#..#....#.
+...#..#...#.....#.........##...
+.#....#......###...#....#.#.#..
+#.#........#......#...#....##..
+.....#..#......#..#..#......#..
+.#.....#..#.##.#.#.#...#......#
+##........#..#.#..#...#.####...
+..........##....#.#..#.#....#..
+#.##..#..#....#..#....##..#.#.#
+..#......#.......#...##..#.....
+##...#.........#......#......#.
+.#.....................#..#.##.
+.#.......#........#.#.#..##.#..
+..#..........#........#..##.#..
+.#...#...#.........##.#.#.#....
+....#....#.###.#....###....#.##
+....##......##........##.#.##..
+....#.#......#.##.#...#.##.....
+....#....#..#.#..###.#.#.......
+....#......#..#.#.......#..##..
+.....#..#.#.##.##..##.....#.#..
+...#....................##.....
+#.....#...##...#.#.............
+..#.#...#.#.#.....##..#....#...

+ 47 - 0
03/lib.ml

@@ -0,0 +1,47 @@
+(* open Base *)
+open Core
+
+module PairCmp = struct
+  module T = struct
+    type t = int * int
+    let compare x y = Tuple2.compare ~cmp1:Int.compare ~cmp2:Int.compare x y
+    let sexp_of_t = Tuple2.sexp_of_t Int.sexp_of_t Int.sexp_of_t
+    let t_of_sexp = Tuple2.t_of_sexp Int.t_of_sexp Int.t_of_sexp
+  end
+  include T
+  include Comparable.Make(T)
+end
+
+let all_slopes = [
+    (1, 1);
+    (3, 1);
+    (5, 1);
+    (7, 1);
+    (1, 2);
+  ]
+
+type map = {
+    height: int;
+    width: int;
+    content: (PairCmp.t, bool, PairCmp.comparator_witness) Map.t;
+  }
+
+let parse_map str =
+  let lines = String.split str ~on:'\n' in
+  let assoc =
+    List.mapi lines ~f:(fun row line ->
+        List.mapi (String.to_list line) ~f:(fun col value ->
+            ((col, row), phys_equal value '#'))) in
+  {
+    height = List.length assoc;
+    width = List.length (List.nth_exn assoc 0);
+    content = Map.of_alist_exn (module PairCmp) (List.join assoc);
+  }
+
+let count_trees_in_slope map (dx, dy) =
+  let rec helper x y =
+    if y >= map.height then 0
+    else if x >= map.width then helper (x - map.width) y
+    else if Map.find_exn map.content (x, y) then 1 + helper (x + dx) (y + dy)
+    else helper (x + dx) (y + dy)
+  in helper 0 0

+ 14 - 0
03/main.ml

@@ -0,0 +1,14 @@
+open Base
+
+let main() =
+  let source = Stdio.In_channel.read_all "input" in
+  let map = Lib.parse_map (String.strip source) in
+  let count = Lib.count_trees_in_slope map (3, 1) in
+  let all_counts =
+    List.map ~f:(Lib.count_trees_in_slope map) Lib.all_slopes in
+  let product =
+    List.fold ~init:1 ~f:(fun x y -> x * y) all_counts in
+  Stdio.printf "part one: you would hit %d trees\n" count;
+  Stdio.printf "part two: you would hit %d trees\n" product
+
+let () = main()

+ 37 - 0
03/test.ml

@@ -0,0 +1,37 @@
+open Base
+open OUnit2
+
+let sample = {|..##.......
+#...#...#..
+.#....#..#.
+..#.#...#.#
+.#...##..#.
+..#.##.....
+.#.#.#....#
+.#........#
+#.##...#...
+#...##....#
+.#..#...#.#|}
+
+let test_parse_map map _ =
+  let _ = Lib.parse_map map in ()
+
+let test_slope map slope exp _ =
+  let map = Lib.parse_map map in
+  assert_equal (Lib.count_trees_in_slope map slope) exp
+
+let test_slopes map slopes exp _ =
+  let map = Lib.parse_map map in
+  let count = Lib.count_trees_in_slope map in
+  let counts = List.map ~f:count slopes in
+  let product = List.fold ~init:1 ~f:(fun x y -> x * y) counts in
+  assert_equal product exp
+
+let () =
+  run_test_tt_main
+    ("day three" >:::
+       [
+         "parse map" >:: test_parse_map sample;
+         "test map" >:: test_slope sample (3, 1) 7;
+         "test map" >:: test_slopes sample Lib.all_slopes 336;
+    ])