2024 day 14 part 2
This commit is contained in:
@@ -38,7 +38,23 @@ let loc_counts map p =
|
||||
let idx = get_quadrant p in
|
||||
IntMap.update idx update_count map
|
||||
|
||||
let part robots =
|
||||
let print_locs lst =
|
||||
let a = Array.make_matrix height width '.' in
|
||||
let rec impl = function
|
||||
| [] -> ()
|
||||
| (x, y) :: t ->
|
||||
if a.(y).(x) = '.' then a.(y).(x) <- '1'
|
||||
else a.(y).(x) <- char_of_int (1 + int_of_char a.(y).(x));
|
||||
impl t
|
||||
in
|
||||
impl lst;
|
||||
Array.iter
|
||||
(fun r ->
|
||||
Array.iter print_char r;
|
||||
print_newline ())
|
||||
a
|
||||
|
||||
let part1 robots =
|
||||
let counts =
|
||||
robots
|
||||
|> List.map normalize_velocity
|
||||
@@ -48,4 +64,23 @@ let part robots =
|
||||
in
|
||||
IntMap.fold (fun _ v acc -> acc * v) counts 1
|
||||
|
||||
let _ = Aoc.main robots_of_file [ (string_of_int, part) ]
|
||||
let find_tree max_n lst =
|
||||
let num_robots = List.length lst in
|
||||
let rec impl n =
|
||||
if n > max_n then failwith "None found"
|
||||
else
|
||||
let poses = List.map (calc_pos_after n) lst in
|
||||
if List.length (List.sort_uniq Aoc.IntPair.compare poses) = num_robots
|
||||
then (
|
||||
print_locs poses;
|
||||
n)
|
||||
else impl (n + 1)
|
||||
in
|
||||
impl 0
|
||||
|
||||
let part2 robots =
|
||||
let robots = List.map normalize_velocity robots in
|
||||
find_tree 100000 robots
|
||||
|
||||
let _ =
|
||||
Aoc.main robots_of_file [ (string_of_int, part1); (string_of_int, part2) ]
|
||||
|
Reference in New Issue
Block a user