1
0
Fork 0

solve 2023 11.2

This commit is contained in:
sloane 2023-12-11 11:21:14 -05:00
parent 7ac6190bc3
commit e73bc3ecb8
No known key found for this signature in database
2 changed files with 38 additions and 24 deletions

View file

@ -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

View file

@ -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