Simplify a bit by inlining a function
This commit is contained in:
parent
9345419941
commit
1e974c56ae
1 changed files with 12 additions and 18 deletions
|
@ -2,22 +2,19 @@
|
||||||
defmodule Day8 do
|
defmodule Day8 do
|
||||||
def part1({x, y, frequencies}) do
|
def part1({x, y, frequencies}) do
|
||||||
Enum.reduce(frequencies, MapSet.new(), fn antennas, antinodes ->
|
Enum.reduce(frequencies, MapSet.new(), fn antennas, antinodes ->
|
||||||
MapSet.union(antinodes, find_antinodes(antennas, x, y))
|
antennas
|
||||||
|
|> find_pairs()
|
||||||
|
|> Enum.flat_map(fn pair -> find_antinodes(pair, x, y) end)
|
||||||
|
|> MapSet.new()
|
||||||
|
|> MapSet.union(antinodes)
|
||||||
end)
|
end)
|
||||||
|> MapSet.size()
|
|> MapSet.size()
|
||||||
end
|
end
|
||||||
|
|
||||||
def find_antinodes(antennas, x, y) do
|
|
||||||
antennas
|
|
||||||
|> find_pairs()
|
|
||||||
|> Enum.flat_map(fn pair -> calc_antinodes(pair, x, y) end)
|
|
||||||
|> MapSet.new()
|
|
||||||
end
|
|
||||||
|
|
||||||
def find_pairs([_]), do: []
|
def find_pairs([_]), do: []
|
||||||
def find_pairs([a | rest]), do: for(b <- rest, do: {a, b}) ++ find_pairs(rest)
|
def find_pairs([a | rest]), do: for(b <- rest, do: {a, b}) ++ find_pairs(rest)
|
||||||
|
|
||||||
def calc_antinodes({{x_a, y_a}, {x_b, y_b}}, max_x, max_y) do
|
def find_antinodes({{x_a, y_a}, {x_b, y_b}}, max_x, max_y) do
|
||||||
dx = x_a - x_b
|
dx = x_a - x_b
|
||||||
dy = y_a - y_b
|
dy = y_a - y_b
|
||||||
antinode_1 = {x_a + dx, y_a + dy}
|
antinode_1 = {x_a + dx, y_a + dy}
|
||||||
|
@ -30,19 +27,16 @@ defmodule Day8 do
|
||||||
|
|
||||||
def part2({x, y, frequencies}) do
|
def part2({x, y, frequencies}) do
|
||||||
Enum.reduce(frequencies, MapSet.new(), fn antennas, antinodes ->
|
Enum.reduce(frequencies, MapSet.new(), fn antennas, antinodes ->
|
||||||
MapSet.union(antinodes, find_resonant_harmonics(antennas, x, y))
|
antennas
|
||||||
|
|> find_pairs()
|
||||||
|
|> Enum.flat_map(fn pair -> find_resonant_harmonics(pair, x, y) end)
|
||||||
|
|> MapSet.new()
|
||||||
|
|> MapSet.union(antinodes)
|
||||||
end)
|
end)
|
||||||
|> MapSet.size()
|
|> MapSet.size()
|
||||||
end
|
end
|
||||||
|
|
||||||
def find_resonant_harmonics(antennas, x, y) do
|
def find_resonant_harmonics({{x_a, y_a}, {x_b, y_b}}, max_x, max_y) do
|
||||||
antennas
|
|
||||||
|> find_pairs
|
|
||||||
|> Enum.flat_map(fn pair -> calc_resonant_harmonics(pair, x, y) end)
|
|
||||||
|> MapSet.new()
|
|
||||||
end
|
|
||||||
|
|
||||||
def calc_resonant_harmonics({{x_a, y_a}, {x_b, y_b}}, max_x, max_y) do
|
|
||||||
dx = x_a - x_b
|
dx = x_a - x_b
|
||||||
dy = y_a - y_b
|
dy = y_a - y_b
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue