Commit 08c1e509 authored by Konrad Borowski's avatar Konrad Borowski
Browse files

Add Day 19 solution

parent b7ab3522
Pipeline #104268805 passed with stage
in 3 minutes and 17 seconds
109,424,203,1,21102,1,11,0,1105,1,282,21101,18,0,0,1106,0,259,2101,0,1,221,203,1,21102,1,31,0,1106,0,282,21101,0,38,0,1106,0,259,21002,23,1,2,22102,1,1,3,21101,0,1,1,21102,57,1,0,1106,0,303,2102,1,1,222,21002,221,1,3,21002,221,1,2,21101,0,259,1,21101,0,80,0,1105,1,225,21101,123,0,2,21101,91,0,0,1105,1,303,1201,1,0,223,20101,0,222,4,21101,259,0,3,21102,225,1,2,21101,0,225,1,21102,118,1,0,1105,1,225,21001,222,0,3,21102,58,1,2,21101,133,0,0,1105,1,303,21202,1,-1,1,22001,223,1,1,21102,1,148,0,1106,0,259,1201,1,0,223,20101,0,221,4,21002,222,1,3,21101,20,0,2,1001,132,-2,224,1002,224,2,224,1001,224,3,224,1002,132,-1,132,1,224,132,224,21001,224,1,1,21101,195,0,0,105,1,109,20207,1,223,2,20102,1,23,1,21101,-1,0,3,21102,214,1,0,1105,1,303,22101,1,1,1,204,1,99,0,0,0,0,109,5,2101,0,-4,249,22102,1,-3,1,22102,1,-2,2,22101,0,-1,3,21101,250,0,0,1105,1,225,21202,1,1,-4,109,-5,2105,1,0,109,3,22107,0,-2,-1,21202,-1,2,-1,21201,-1,-1,-1,22202,-1,-2,-2,109,-3,2106,0,0,109,3,21207,-2,0,-1,1206,-1,294,104,0,99,21201,-2,0,-2,109,-3,2106,0,0,109,5,22207,-3,-4,-1,1206,-1,346,22201,-4,-3,-4,21202,-3,-1,-1,22201,-4,-1,2,21202,2,-1,-1,22201,-4,-1,1,22102,1,-2,3,21102,1,343,0,1105,1,303,1105,1,415,22207,-2,-3,-1,1206,-1,387,22201,-3,-2,-3,21202,-2,-1,-1,22201,-3,-1,3,21202,3,-1,-1,22201,-3,-1,2,21201,-4,0,1,21102,1,384,0,1106,0,303,1105,1,415,21202,-4,-1,-4,22201,-4,-3,-4,22202,-3,-2,-2,22202,-2,-4,-4,22202,-3,-2,-3,21202,-4,-1,-2,22201,-3,-2,1,21201,1,0,-4,109,-5,2105,1,0
defmodule AdventOfCode2019.Day19 do
@moduledoc """
Day 19 solutions
"""
@doc """
Solves the first riddle of day 19.
## Examples
iex> File.read!("inputs/day19.txt") |> AdventOfCode2019.Day19.part1()
201
"""
def part1(input) do
continue = start(input)
for(x <- 0..49, y <- 0..49, do: at_pos({x, y}, continue)) |> Enum.count(& &1)
end
@doc """
Solves the second riddle of day 19.
## Examples
iex> File.read!("inputs/day19.txt") |> AdventOfCode2019.Day19.part2()
6610984
"""
def part2(input) do
continue = start(input)
{x, y} =
for(x <- 0..49, y <- 0..49, do: {x, y})
|> Enum.filter(&at_pos(&1, continue))
|> Enum.max()
|> search(continue)
x * 10000 + y
end
defp start(input) do
{{:input, continue}, []} =
AdventOfCode2019.IntCode.parse(input) |> AdventOfCode2019.IntCode.run()
continue
end
defp search({x, y}, continue) do
cond do
x >= 99 && at_pos({x - 99, y + 99}, continue) -> {x - 99, y}
at_pos({x + 1, y}, continue) -> search({x + 1, y}, continue)
true -> search({x, y + 1}, continue)
end
end
defp at_pos({x, y}, continue) do
{{:input, continue}, []} = continue.(x)
{:end, [value]} = continue.(y)
case value do
0 -> false
1 -> true
end
end
end
defmodule AdventOfCode2019.Day19Test do
use ExUnit.Case
doctest AdventOfCode2019.Day19
end
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment