solve_2.ml 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. open Stdio
  2. open Str
  3. module CharSet = Set.Make(Char)
  4. module IntSet = Set.Make(Int)
  5. let input_info =
  6. let rec one_string inp nb_rows nb_columns =
  7. let line = In_channel.input_line In_channel.stdin in
  8. match line with
  9. | None -> (inp, (nb_rows, nb_columns))
  10. | Some x -> one_string (inp ^ x) (nb_rows + 1) (String.length x)
  11. in one_string "" 0 0
  12. ;;
  13. let input = (fst input_info);;
  14. let nb_rows = (fst (snd input_info));;
  15. let nb_columns =(snd (snd input_info));;
  16. let encode_movement direction pos =
  17. match direction with
  18. | '^' -> let new_pos = pos - nb_columns in
  19. if (new_pos < 0) then -1 else new_pos
  20. | 'v' -> let new_pos = (pos + nb_columns) in
  21. if (new_pos >= (nb_rows*nb_columns)) then -1 else new_pos
  22. | '<' -> let new_pos = (pos - 1) in
  23. if (new_pos mod nb_columns == nb_columns -1) then -1 else
  24. (if (new_pos < 0) then -1 else new_pos)
  25. | '>' -> let new_pos = (pos + 1) in
  26. if (new_pos mod nb_columns == 0) then -1 else
  27. (if (new_pos >= (nb_rows*nb_columns)) then -1 else new_pos)
  28. | _ -> -1 (*Not supposed to happen*)
  29. ;;
  30. let rec move_n direction pos n =
  31. if (n <= 0 || pos == -1) then pos
  32. else (move_n direction (encode_movement direction pos) (n-1))
  33. let place_antinode_up delta_x delta_y pos =
  34. let rec aux accum p =
  35. let pos_x = if (delta_x > 0) then (move_n '>' p delta_x) else (move_n '<' p (abs delta_x)) in
  36. if (pos_x == -1) then accum
  37. else
  38. (let final_pos = (move_n '^' pos_x delta_y) in
  39. if (final_pos == -1) then accum else (aux (final_pos::accum) final_pos))
  40. in
  41. aux [] pos
  42. ;;
  43. let place_antinode_down delta_x delta_y pos =
  44. let rec aux accum p =
  45. let pos_x = if (delta_x > 0) then (move_n '<' p delta_x) else (move_n '>' p (abs delta_x)) in
  46. if (pos_x == -1) then accum
  47. else
  48. (let final_pos = (move_n 'v' pos_x delta_y) in
  49. if (final_pos == -1) then accum else (aux (final_pos::accum) final_pos))
  50. in
  51. aux [] pos
  52. ;;
  53. let get_antennas =
  54. let rec aux pos accum =
  55. try
  56. let pos_antenna = search_forward (regexp {|[^.]|}) input pos in
  57. aux (pos_antenna + 1) (CharSet.add input.[pos_antenna] accum)
  58. with Not_found -> accum
  59. in
  60. aux 0 CharSet.empty
  61. ;;
  62. (* Get distance between two antennas
  63. requires pos1 < pos2;
  64. *)
  65. let distance pos1 pos2 =
  66. let y_pos1, x_pos1 = (pos1/nb_columns, pos1 mod nb_columns) in
  67. let y_pos2, x_pos2 = (pos2/nb_columns, pos2 mod nb_columns) in
  68. let x_diff, y_diff = (x_pos1 - x_pos2), (y_pos2 - y_pos1) in
  69. (x_diff, y_diff)
  70. ;;
  71. let create_antinodes antenna fixed_antenna_pos =
  72. let rec aux pos accum =
  73. try
  74. let pos_antenna2 = search_forward (regexp (String.make 1 antenna)) input (pos + 1) in
  75. let x_diff, y_diff = (distance fixed_antenna_pos pos_antenna2) in
  76. let anti_node1 = (place_antinode_up x_diff y_diff fixed_antenna_pos) in
  77. let anti_node2 = (place_antinode_down x_diff y_diff pos_antenna2) in
  78. let antenna_as_antinode_set = (IntSet.add pos_antenna2 accum) in
  79. let set_antinode1 = List.fold_left (fun acc p -> IntSet.add p acc) IntSet.empty anti_node1 in
  80. let set_antinode2 = List.fold_left (fun acc p -> IntSet.add p acc) set_antinode1 anti_node2 in
  81. aux (pos_antenna2) (IntSet.union (IntSet.union set_antinode1 set_antinode2) antenna_as_antinode_set)
  82. with Not_found -> if ((IntSet.cardinal accum) == 1) then (IntSet.remove fixed_antenna_pos accum) else accum
  83. in
  84. aux fixed_antenna_pos (IntSet.add fixed_antenna_pos IntSet.empty)
  85. ;;
  86. let create_all_antinodes antenna =
  87. let rec aux pos accum =
  88. try
  89. let pos = search_forward (regexp (String.make 1 antenna)) input pos in
  90. let set_positions = create_antinodes antenna pos in
  91. aux (pos+1) (IntSet.union accum set_positions)
  92. with Not_found -> accum
  93. in
  94. aux 0 IntSet.empty
  95. ;;
  96. let solve =
  97. let antinodes =
  98. CharSet.fold (fun c acc -> (IntSet.union (create_all_antinodes c) acc)) get_antennas IntSet.empty in
  99. (* printf "\n\nAntinodes : \n"; *)
  100. (* IntSet.iter (fun d -> printf "%d " d) antinodes; *)
  101. IntSet.cardinal antinodes
  102. ;;
  103. let () =
  104. (* printf "anitnodes for A\n"; *)
  105. (* IntSet.iter (fun p -> printf "%d " p) (create_antinodes 'A' 104); *)
  106. printf "Total: %d\n" solve;;