-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
1,066 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
defmodule Advent2018.Day23.Nanobot do | ||
defstruct ~w(x y z radius)a | ||
end | ||
|
||
defmodule Advent2018.Day23 do | ||
alias Advent2018.Day23.Nanobot | ||
|
||
def part_a(input) do | ||
nanobots = input |> parse | ||
strongest = Enum.max_by(nanobots, fn each -> each.radius end) | ||
Enum.count(nanobots, fn each -> in_range?(strongest, each) end) | ||
end | ||
|
||
defp in_range?(%Nanobot{radius: radius} = strongest, nanobot) do | ||
distance(strongest, nanobot) <= radius | ||
end | ||
|
||
defp distance(%Nanobot{x: x1, y: y1, z: z1}, %Nanobot{x: x2, y: y2, z: z2}) do | ||
abs(x1 - x2) + abs(y1 - y2) + abs(z1 - z2) | ||
end | ||
|
||
defp parse(input) do | ||
input | ||
|> String.split("\n", trim: true) | ||
|> Enum.map(&nanobot/1) | ||
end | ||
|
||
defp nanobot(string) do | ||
map = | ||
Regex.named_captures(nanobot_regex(), string) | ||
|> Enum.map(fn {key, value} -> {String.to_atom(key), String.to_integer(value)} end) | ||
|
||
struct!(Nanobot, map) | ||
end | ||
|
||
defp nanobot_regex do | ||
~r/pos=<(?<x>-?\d*),(?<y>-?\d*),(?<z>-?\d*)>, r=(?<radius>-?\d*)/ | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
defmodule Advent2018.Day23Test do | ||
use ExUnit.Case, async: true | ||
|
||
alias Advent2018.Day23 | ||
|
||
test "part_a" do | ||
input = """ | ||
pos=<0,0,0>, r=4 | ||
pos=<1,0,0>, r=1 | ||
pos=<4,0,0>, r=3 | ||
pos=<0,2,0>, r=1 | ||
pos=<0,5,0>, r=3 | ||
pos=<0,0,3>, r=1 | ||
pos=<1,1,1>, r=1 | ||
pos=<1,1,2>, r=1 | ||
pos=<1,3,1>, r=1 | ||
""" | ||
|
||
assert Day23.part_a(input) == 7 | ||
end | ||
|
||
@tag :real | ||
test "part_a real" do | ||
input = File.read!("test/lib/advent_2018/input/day_23.txt") | ||
assert Day23.part_a(input) == 172 | ||
end | ||
end |
Oops, something went wrong.