Last active
December 13, 2018 16:01
-
-
Save pivstone/bb85597dfcc4a18e737660b1ea14f9d7 to your computer and use it in GitHub Desktop.
[Elixir] adventofcode 2018 Day 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Part 1 | |
data = ["#1 @ 429,177: 12x27", "#2 @ 862,948: 20x11", "#3 @ 783,463: 22x20"] | |
data |> Enum.map(fn str -> | |
[_, data] = String.split(str, "@") | |
[point, range] = String.split(data, ":") | |
[top, right] = String.split(point, ",") | |
[xs, ys] = String.split(range, "x") | |
[top, right, xs, ys] | |
|> Enum.map(&String.trim/1) | |
|> Enum.map(&String.to_integer/1) | |
|> List.to_tuple() | |
end) |> Enum.map(fn {top, right, xs, ys} -> | |
Enum.map(0..(xs - 1), fn x -> | |
Enum.map(0..(ys - 1), fn y -> | |
{top + x, right + y} | |
end) | |
end) | |
end) |> List.flatten() |> Enum.reduce(%{}, fn x, acc -> | |
Map.update(acc, x, 0, &(&1 + 1)) | |
end)|> Enum.filter(fn {_, v} -> | |
v != 0 | |
end) |> Enum.count | |
# Part 2 | |
indices = tangle |> Enum.map(&elem(&1, 0)) | |
sub = | |
tangle |> Enum.map(fn {index, top, right, xs, ys} -> | |
Enum.map(0..(xs - 1), fn x -> | |
Enum.map(0..(ys - 1), fn y -> | |
{index, top + x, right + y} | |
end) | |
end) | |
end) |> List.flatten() |> Enum.reduce(%{}, fn {i,x,y}, acc -> | |
Map.update(acc, {x, y}, [i], &([i|&1])) | |
end) | |
tangle |> Enum.map(fn {index, top, right, xs, ys} -> | |
Enum.map(0..(xs - 1), fn x -> | |
Enum.map(0..(ys - 1), fn y -> | |
{index, top + x, right + y} | |
end) | |
end) | |
end) |> List.flatten() |> Enum.reduce(indices, fn {index, x, y}, acc -> | |
value = Map.get(sub, {x, y}) | |
if value != [index] do | |
List.delete(acc, index) | |
else | |
acc | |
end | |
end) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment