From e73bc3ecb840ab6b9b09aa62b47a3f7012792709 Mon Sep 17 00:00:00 2001 From: sloane <1699281+sloanelybutsurely@users.noreply.github.com> Date: Mon, 11 Dec 2023 11:21:14 -0500 Subject: [PATCH] solve 2023 11.2 --- 2023/lib/2023/11.ex | 60 ++++++++++++++++++++++++++++----------------- README.md | 2 +- 2 files changed, 38 insertions(+), 24 deletions(-) diff --git a/2023/lib/2023/11.ex b/2023/lib/2023/11.ex index 4b2ff73..fe268f0 100644 --- a/2023/lib/2023/11.ex +++ b/2023/lib/2023/11.ex @@ -3,6 +3,42 @@ import AOCHelpers aoc 2023, 11 do def p1(input) do + input + |> read_input() + |> calculate_distances() + end + + def p2(input) do + input + |> read_input() + |> calculate_distances(1_000_000) + end + + def calculate_distances({galaxies, empty_columns, empty_rows}, expansion \\ 2) do + distance = make_distance_fn(empty_rows, empty_columns, expansion) + + for [a, b] <- combinations(galaxies, 2), reduce: 0 do + acc -> acc + distance.(a, b) + end + end + + def make_distance_fn(expanded_rows, expanded_columns, expansion) do + fn {x_a, y_a}, {x_b, y_b} -> + x_dist = abs(x_b - x_a) + y_dist = abs(y_b - y_a) + + x_range = to_range(x_a, x_b) + y_range = to_range(y_a, y_b) + + expanded_x = Enum.count(expanded_columns, &(&1 in x_range)) + expanded_y = Enum.count(expanded_rows, &(&1 in y_range)) + + x_dist - expanded_x + expanded_x * expansion + (y_dist - expanded_y) + + expanded_y * expansion + end + end + + def read_input(input) do {grid, {x_bounds, y_bounds}} = to_grid(input) empty_columns = @@ -16,29 +52,7 @@ aoc 2023, 11 do |> Enum.filter(&match?({_, "#"}, &1)) |> Enum.map(&elem(&1, 0)) - distance = make_distance_fn(empty_rows, empty_columns) - - for [a, b] <- combinations(galaxies, 2), reduce: 0 do - acc -> acc + distance.(a, b) - end - end - - def p2(_input) do - end - - def make_distance_fn(expanded_rows, expanded_columns) do - fn {x_a, y_a}, {x_b, y_b} -> - x_dist = abs(x_b - x_a) - y_dist = abs(y_b - y_a) - - x_range = to_range(x_a, x_b) - y_range = to_range(y_a, y_b) - - expanded_x = Enum.count(expanded_columns, &(&1 in x_range)) - expanded_y = Enum.count(expanded_rows, &(&1 in y_range)) - - x_dist + expanded_x + y_dist + expanded_y - end + {galaxies, empty_columns, empty_rows} end def row(grid, y, x_bounds) do diff --git a/README.md b/README.md index 6f4b1b7..307b285 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ 1. [2020] **17/50** 🌟 1. [2021] **43/50** 🌟 1. [2022] **14/50** 🌟 -1. [2023] **16/50** 🌟 +1. [2023] **17/50** 🌟 [2015]: ./2015 [2017]: ./2017