Advent of Code 2015 Day 6 Part 1
Mix.install([
{:kino_aoc, "~> 0.1"}
])
Get Inputs
{:ok, puzzle_input} =
KinoAOC.download_puzzle("2015", "6", System.fetch_env!("LB_SESSION"))
My answer
Regex.named_captures(~r/(?.+) (?\d+),(?\d+) through (?\d+),(?\d+)/, "turn on 0,0 through 999,999")
puzzle_input
|> String.split("\n")
|> Enum.reduce(%{},fn instruction, map ->
%{"op" => op, "sx" => sx, "sy" => sy, "ex" => ex, "ey" => ey} =
Regex.named_captures(
~r/(?.+) (?\d+),(?\d+) through (?\d+),(?\d+)/,
instruction
)
[sx, sy, ex, ey] = Enum.map([sx, sy, ex, ey], &String.to_integer(&1))
Enum.reduce(sx..ex, map, fn x, x_map ->
Enum.reduce(sy..ey, x_map, fn y, y_map ->
lit = Map.get(y_map, {x, y}, false)
new =
case op do
"turn on" -> true
"turn off" -> false
_ -> !lit
end
Map.put(y_map, {x, y}, new)
end)
end)
end)
|> Enum.count(fn {_, lit} -> lit end)