浏览代码

solution day 8 part 2

AbderFaiz 4 月之前
父节点
当前提交
3ebd9111b1
共有 2 个文件被更改,包括 11 次插入7 次删除
  1. 9 6
      8/solve_2.ml
  2. 2 1
      README.md

+ 9 - 6
8/solve_2.ml

@@ -59,7 +59,7 @@ let place_antinode_down delta_x delta_y pos =
 ;;
 
 let get_antennas =
-    let rec aux pos accum=
+  let rec aux pos accum =
     try
       let pos_antenna = search_forward (regexp {|[^.]|}) input pos in
       aux (pos_antenna + 1) (CharSet.add input.[pos_antenna] accum) 
@@ -86,12 +86,13 @@ let create_antinodes antenna fixed_antenna_pos =
       let x_diff, y_diff = (distance fixed_antenna_pos pos_antenna2) in
       let anti_node1 = (place_antinode_up x_diff y_diff fixed_antenna_pos) in
       let anti_node2 = (place_antinode_down x_diff y_diff pos_antenna2) in
+      let antenna_as_antinode_set = (IntSet.add pos_antenna2 accum) in
       let set_antinode1 = List.fold_left (fun acc p -> IntSet.add p acc) IntSet.empty anti_node1 in
       let set_antinode2 = List.fold_left (fun acc p -> IntSet.add p acc) set_antinode1 anti_node2 in
-      aux (pos_antenna2) (IntSet.union (IntSet.union set_antinode1 set_antinode2) accum)
-    with Not_found -> accum
+      aux (pos_antenna2) (IntSet.union (IntSet.union set_antinode1 set_antinode2) antenna_as_antinode_set)
+    with Not_found -> if ((IntSet.cardinal accum) == 1) then (IntSet.remove fixed_antenna_pos accum) else accum
   in
-  aux fixed_antenna_pos IntSet.empty
+  aux fixed_antenna_pos (IntSet.add fixed_antenna_pos IntSet.empty)
 ;;
 
 let create_all_antinodes antenna =
@@ -108,11 +109,13 @@ let create_all_antinodes antenna =
 let solve =
   let antinodes =
     CharSet.fold (fun c acc -> (IntSet.union (create_all_antinodes c) acc)) get_antennas IntSet.empty in
-  printf "Antinodes : \n";
-  IntSet.iter (fun d -> printf "%d " d) antinodes;
+  (* printf "\n\nAntinodes : \n"; *)
+  (* IntSet.iter (fun d -> printf "%d " d) antinodes; *)
   IntSet.cardinal antinodes
 ;;
 
 let () =
+  (* printf "anitnodes for A\n"; *)
+  (* IntSet.iter (fun p -> printf "%d " p) (create_antinodes 'A' 104); *)
   printf "Total: %d\n" solve;;
 

+ 2 - 1
README.md

@@ -49,7 +49,8 @@ replace `<part_number>` with 1 for Part 1 or 2 for Part 2
 | 5   | ✅     | ✅     |
 | 6   | ✅     | ✅     |
 | 7   | ✅     | ✅     |
-| 8   | ✅     | ⬜     |
+| 8   | ✅     | ✅     |
+| 9   | ⬜     | ⬜     |
 | ... | ...    | ...    |
 | 25  | ⬜     | ⬜     |