Day Two
Section
Mix.install([
{:kino, "~> 0.11.3"}
])
defmodule Utilities do
@moduledoc """
Provides utility functions to be used across days
"""
def read_data(day) do
day_as_string =
day
|> Integer.to_string()
|> String.pad_leading(2, "0")
Path.join(__DIR__, "../data/day_#{day_as_string}_input.txt")
|> Path.expand()
|> File.read!()
end
def to_list(big_string) do
String.split(big_string, "\n")
end
end
Day 2: Red-Nosed Reports
Fortunately, the first location The Historians want to search isn’t a long walk from the Chief Historian’s office.
While the Red-Nosed Reindeer nuclear fusion/fission plant appears to contain no sign of the Chief Historian, the engineers there run up to you as soon as they see you. Apparently, they still talk about the time Rudolph was saved through molecular synthesis from a single electron.
They’re quick to add that - since you’re already here - they’d really appreciate your help analyzing some unusual data from the Red-Nosed reactor. You turn to check if The Historians are waiting for you, but they seem to have already divided into groups that are currently searching every corner of the facility. You offer to help with the unusual data.
The unusual data (your puzzle input) consists of many reports, one report per line. Each report is a list of numbers called levels that are separated by spaces. For example:
7 6 4 2 1
1 2 7 8 9
9 7 6 2 1
1 3 2 4 5
8 6 4 4 1
1 3 6 7 9
This example data contains six reports each containing five levels.
The engineers are trying to figure out which reports are safe. The Red-Nosed reactor safety systems can only tolerate levels that are either gradually increasing or gradually decreasing. So, a report only counts as safe if both of the following are true:
- The levels are either all increasing or all decreasing.
- Any two adjacent levels differ by at least one and at most three.
In the example above, the reports can be found safe or unsafe by checking those rules:
7 6 4 2 1: Safe because the levels are all decreasing by 1 or 2.
1 2 7 8 9: Unsafe because 2 7 is an increase of 5.
9 7 6 2 1: Unsafe because 6 2 is a decrease of 4.
1 3 2 4 5: Unsafe because 1 3 is increasing but 3 2 is decreasing.
8 6 4 4 1: Unsafe because 4 4 is neither an increase or a decrease.
1 3 6 7 9: Safe because the levels are all increasing by 1, 2, or 3.
So, in this example, 2 reports are safe.
Analyze the unusual data from the engineers. How many reports are safe?
practice_input = [
"7 6 4 2 1",
"1 2 7 8 9",
"9 7 6 2 1",
"1 3 2 4 5",
"8 6 4 4 1",
"1 3 6 7 9"
]
puzzle_input = Utilities.read_data(2) |> Utilities.to_list()
# puzzle_input = practice_input
defmodule DayTwo do
def process_data(puzzle_input) do
puzzle_input
|> Enum.map(fn
"" ->
[]
report ->
report
|> String.split()
|> Enum.map(&String.to_integer(&1))
end)
end
def part_one(puzzle_input) do
puzzle_input
|> process_data()
|> Enum.reduce(0, fn report, safe_total ->
safe?(report)
if safe?(report), do: safe_total + 1, else: safe_total
end)
end
def safe?(report), do: differ_by_test(report) && ordered?(report)
def differ_by_test([]), do: false
def differ_by_test(report) do
report_with_index = Enum.with_index(report)
Enum.reduce_while(report_with_index, true, fn {_level, item_index}, test_result ->
item_before_index = if item_index - 1 < 0, do: 10_000_000, else: item_index - 1
item_before = Enum.at(report_with_index, item_before_index)
item = Enum.at(report_with_index, item_index)
test = in_bounds?(item_before, item)
if test, do: {:cont, test_result}, else: {:halt, test}
end)
end
def ordered?(report) do
asc? = report == Enum.sort(report)
desc? = report == Enum.sort(report, :desc)
asc? || desc?
end
def in_bounds?(nil, _item) do
true
end
def in_bounds?({item_before, _before_index}, {item, _item_index}) do
difference_between_item_and_before = abs(item - item_before)
difference_between_item_and_before >= 1 && difference_between_item_and_before <= 3
end
def part_two(puzzle_input) do
puzzle_input
|> process_data()
|> Enum.reduce(0, fn report, safe_total ->
if safe?(report) do
safe_total + 1
else
dampener? =
Enum.reduce_while(report, false, fn level, _acc ->
if safe?(report -- [level]) || safe?(Enum.reverse(report) -- [level]) do
{:halt, true}
else
{:cont, false}
end
end)
if dampener?, do: safe_total + 1, else: safe_total
end
end)
end
end
DayTwo.part_two(puzzle_input)
### it's totally going to be look ahead and look behind on the row too
### function that, given an array, look at the before and the after
### item from before, item, and item after in arrray