diff options
author | Ben Harris <ben@tilde.team> | 2018-12-03 08:50:31 -0500 |
---|---|---|
committer | Ben Harris <ben@tilde.team> | 2018-12-03 13:00:41 -0500 |
commit | 1fb359f4831f23653c5517f6ef0d1dc0b607db4d (patch) | |
tree | 04cd524cb0459d713b2f7ce91346670208a9f93e /day3.exs | |
parent | 7d9b68597bcd46e21e0e91aecb6d6620aa3e79e4 (diff) |
day3
Diffstat (limited to 'day3.exs')
-rw-r--r-- | day3.exs | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/day3.exs b/day3.exs new file mode 100644 index 0000000..4e66de4 --- /dev/null +++ b/day3.exs @@ -0,0 +1,40 @@ +defmodule Day3 do + def input() do + File.stream!("input/day3.in") + |> Stream.map(fn x -> + Regex.scan(~r/\d+/, x) + |> Enum.map(&String.to_integer(hd(&1))) + end) + end + + def plots(), do: Enum.reduce(input(), %{}, &stake_claim/2) + + def part1() do + plots() + |> Enum.count(fn {_k, {v, _}} -> v > 1 end) + end + + def part2() do + hd(Enum.find(input(), &intact?(&1, plots()))) + end + + def stake_claim([id, x, y, w, h], claims) do + coords = for wide <- 0..(w - 1), high <- 0..(h - 1), do: {wide, high} + + Enum.reduce(coords, claims, fn {wide, high}, acc -> + Map.update(acc, {x + wide, y + high}, {1, [id]}, fn {count, claims} -> + {count + 1, [id | claims]} + end) + end) + end + + def intact?([id, _x, _y, _w, _h], plot) do + Enum.all?(plot, fn {_coord, {_count, claims}} -> + not Enum.member?(claims, id) || claims == [id] + end) + end +end + +IO.puts(Day3.part1()) +IO.puts(Day3.part2()) + |