Skip to content

Commit

Permalink
Day 23A
Browse files Browse the repository at this point in the history
  • Loading branch information
duff committed Jan 14, 2019
1 parent 6ced036 commit 5ab4c7b
Show file tree
Hide file tree
Showing 3 changed files with 1,066 additions and 0 deletions.
39 changes: 39 additions & 0 deletions lib/advent_2018/day_23.ex
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
27 changes: 27 additions & 0 deletions test/lib/advent_2018/day_23_test.exs
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
Loading

0 comments on commit 5ab4c7b

Please sign in to comment.