Day 10: The Stars Align - Advent of Code 2018
I did the Advent of Code 2018 day 10 challenge in Elixir! Parts one and two are as follows:
defmodule Day10 do
def part1(input) do
{grid, _times} =
input
|> read_input()
|> parse_input()
|> iter()
draw(grid)
end
def part2(input) do
{_grid, times} =
input
|> read_input()
|> parse_input()
|> iter()
times
end
defp read_input(input) do
input
|> File.read!()
|> String.split("\n", trim: true)
end
defp parse_input(input) do
Enum.map(input, fn line ->
[x, y, vx, vy] =
Regex.scan(~r/(-?\d+)/, line, capture: :all_but_first)
|> List.flatten()
|> Enum.map(&String.to_integer/1)
{{x, y}, {vx, vy}}
end)
end
defp iter(grid) do
distance =
grid
|> layout()
|> distance()
iter(grid, distance, 0)
end
defp iter(grid, previous_distance, times) do
new_grid =
Enum.map(grid, fn {{x, y}, {vx, vy} = v} ->
{{x + vx, y + vy}, v}
end)
distance =
new_grid
|> layout()
|> distance()
if distance <= previous_distance do
iter(new_grid, distance, times + 1)
else
{grid, times}
end
end
defp layout(grid) do
Enum.reduce(grid, {0, 0, 0, 0}, fn {{x, _}, _}, {min_x, max_x, min_y, max_y} ->
{
Enum.min([min_x, x]),
Enum.max([max_x, x]),
Enum.min([min_y, x]),
Enum.max([max_y, x])
}
end)
end
defp distance({min_x, max_x, min_y, max_y}) do
abs(max_x - min_x) + abs(max_y - min_y)
end
defp draw(grid) do
grid =
grid
|> Enum.map(&{elem(&1, 0), 0})
|> Enum.into(%{})
{min_x, max_x, min_y, max_y} = layout(grid)
for y <- min_y..max_y do
for x <- min_x..max_x do
if Map.get(grid, {x, y}) do
IO.write("#")
else
IO.write(".")
end
end
IO.write("\n")
end
grid
end
end
# r Day10; :aoc2018 |> :code.priv_dir() |> Path.join("day10.txt") |> Day10.part1()
# r Day10; :aoc2018 |> :code.priv_dir() |> Path.join("day10.txt") |> Day10.part2()
![Simon Escobar Benitez](/uploads/media/speakers/0001/01/thumb_722_speakers_small.png)
ARTICLES: 8
Day 11: Chronal Charge - Advent of Code 2018
![Day 11: Chronal Charge - Advent of Code 2018](/uploads/media/articles/0001/01/63e322b220891c7f81b7a2468c6914ceb39a7f3f.png)
I did the Advent of Code 2018 day 11 challenge in Elixir! Parts one and two are as follows:
READ MOREDay 7: The Sum of Its Parts - Advent of Code 2018
![Day 7: The Sum of Its Parts - Advent of Code 2018](/uploads/media/articles/0001/01/a2f632e45209197c43636b4da1887ab38e2d8f76.png)
I did the Advent of Code 2018 day 7 challenge in Elixir! Parts one and two are as follows:
READ MOREDay 6: Chronal Coordinates - Advent of Code 2018
![Day 6: Chronal Coordinates - Advent of Code 2018](/uploads/media/articles/0001/01/978c395f39a6ce30d9db84a9b71c068f2f24ca62.png)
I did the Advent of Code 2018 day 6 challenge in Elixir! Parts one and two are as follows:
READ MOREDay 5: Alchemical Reduction - Advent of Code 2018
![Day 5: Alchemical Reduction - Advent of Code 2018](/uploads/media/articles/0001/01/4609e59fb2d556c4657bd52aea2a991dcd59a1f1.png)
I did the Advent of Code 2018 day 5 challenge in Elixir! Parts one and two are as follows:
READ MOREDay 4: Repose Record - Advent of Code 2018
![Day 4: Repose Record - Advent of Code 2018](/uploads/media/articles/0001/01/3996c8b8f765c5d1a0485e3bc309863d97427109.png)
I did the Advent of Code 2018 day 4 challenge in Elixir! Parts one and two are as follows:
READ MOREDay 3: No matter how you slice it - Advent of Code 2018
![Day 3: No matter how you slice it - Advent of Code 2018](/uploads/media/articles/0001/01/c4cdaaaaa6fbfc6d3be78ddcbe2f05bf8ccea64f.png)
I did the Advent of Code 2018 day 3 challenge in Elixir! Parts one and two are as follows:
READ MOREDay 2: Inventory Management System - Advent of Code 2018
![Day 2: Inventory Management System - Advent of Code 2018](/uploads/media/articles/0001/01/10a67edefbec721abd6940f0b4990adfc8683a1d.png)
I did the Advent of Code 2018 day 2 challenge in Elixir! Parts one and two are as follows:
READ MOREDay 1: Chronal Calibration - Advent of Code 2018
![Day 1: Chronal Calibration - Advent of Code 2018](/uploads/media/articles/0001/01/007cb411da7da99ec16de318f4062c00930db5cd.png)
Advent of Code 2018 - Day 1 solution in Elixir! #AdventOfBEAM
READ MORE