Day 6 - Advent of Code 2015
Mix.install([
:kino,
{:nx, "~> 0.4"}
])
:ok
Links
Prompt
— Day 6: Probably a Fire Hazard —
Because your neighbors keep defeating you in the holiday house decorating contest year after year, you’ve decided to deploy one million lights in a 1000x1000
grid.
Furthermore, because you’ve been especially nice this year, Santa has mailed you instructions on how to display the ideal lighting configuration.
Lights in your grid are numbered from 0 to 999 in each direction; the lights at each corner are at 0,0
, 0,999
, 999,999
, and 999,0
. The instructions include whether to turn on
, turn off
, or toggle
various inclusive ranges given as coordinate pairs. Each coordinate pair represents opposite corners of a rectangle, inclusive; a coordinate pair like 0,0 through 2,2
therefore refers to 9 lights in a 3x3 square. The lights all start turned off.
To defeat your neighbors this year, all you have to do is set up your lights by doing the instructions Santa sent you in order.
For example:
-
turn on 0,0 through 999,999
would turn on (or leave on) every light. -
toggle 0,0 through 999,0
would toggle the first line of 1000 lights, turning off the ones that were on, and turning on the ones that were off. -
turn off 499,499 through 500,500
would turn off (or leave off) the middle four lights.
After following the instructions, how many lights are lit?
To begin, get your puzzle input.
— Part Two —
Part Two prompt
QUESTION TWO?
Although it hasn’t changed, you can still get your puzzle input.
Input
input = Kino.Input.textarea("Please paste your input file:")
input = input |> Kino.Input.read()
""
Solution
https://dockyard.com/blog/2022/07/12/elixir-versus-python-for-data-science https://dockyard.com/blog/2021/04/08/up-and-running-nx https://hexdocs.pm/nx/Nx.html#pad/3
t_new = Nx.broadcast(0, {1000, 1000})
# t1 = Nx.pad(
# Nx.broadcast(1, {400, 400}),
# 0,
# [
# {200, 400, 0},
# {200, 400, 0}
# ]
# )
# Nx.bitwise_or(t_new, t1)
#Nx.Tensor<
s64[1000][1000]
[
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...],
...
]
>
defmodule Day06 do
defdelegate parse(input), to: __MODULE__.Input
def part1(input) do
input
|> parse()
|> Enum.map(& &1)
end
def part2(input) do
input
|> parse()
|> Enum.map(& &1)
end
defmodule Input do
def parse(input) when is_binary(input) do
input
|> String.split("\n")
|> parse()
end
def parse(input) when is_list(input) do
Stream.map(input, &parse_line/1)
end
def parse_line(line) do
line
# |> String.split(["-", ","])
# |> Enum.map(&String.to_integer/1)
end
end
end
{:module, Day06, <<70, 79, 82, 49, 0, 0, 8, ...>>,
{:module, Day06.Input, <<70, 79, 82, ...>>, {:parse_line, 1}}}
After following the instructions, how many lights are lit?
Your puzzle answer was answer one
.
Day06.part1(input)
[""]
QUESTION TWO?
Your puzzle answer was answer two
.
Day06.part2(input)
[""]
Both parts of this puzzle are complete! They provide two gold stars: **
At this point, you should return to your Advent calendar and try another puzzle.
If you still want to see it, you can get your puzzle input.
Benchmarking
defmodule Benchmark do
def fun1(input) do
Day06.part1(input)
end
def fun2(input) do
Day06.part2(input)
end
end
{:module, Benchmark, <<70, 79, 82, 49, 0, 0, 6, ...>>, {:fun2, 1}}
# https://github.com/bencheeorg/benchee
input = Day06.Input.parse(input) |> Enum.to_list()
Benchee.run(
%{
"fun1" => fn _ -> Benchmark.fun1(input) end,
"fun2" => fn _ -> Benchmark.fun2(input) end
},
inputs: %{
"Part 1" => 1
},
memory_time: 2,
reduction_time: 2
)
nil
nil
Tests
ExUnit.start(auto_run: false)
defmodule Day06Test do
use ExUnit.Case, async: false
setup_all do
[
input: ""
]
end
describe "part1/1" do
test "returns expected value", %{input: input} do
assert Day06.part1(input) == [""]
end
end
describe "part2/1" do
test "returns expected value", %{input: input} do
assert Day06.part2(input) == [""]
end
end
end
ExUnit.run()
..
Finished in 0.00 seconds (0.00s async, 0.00s sync)
2 tests, 0 failures
Randomized with seed 162351
%{excluded: 0, failures: 0, skipped: 0, total: 2}