Day 3
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
Section
Day 3: Mull It Over
“Our computers are having issues, so I have no idea if we have any Chief Historians in stock! You’re welcome to check the warehouse, though,” says the mildly flustered shopkeeper at the North Pole Toboggan Rental Shop. The Historians head out to take a look.
The shopkeeper turns to you. “Any chance you can see why our computers are having issues again?”
The computer appears to be trying to run a program, but its memory (your puzzle input) is corrupted. All of the instructions have been jumbled up!
It seems like the goal of the program is just to multiply some numbers. It does that with instructions like mul(X,Y), where X and Y are each 1-3 digit numbers. For instance, mul(44,46) multiplies 44 by 46 to get a result of 2024. Similarly, mul(123,4) would multiply 123 by 4.
However, because the program’s memory has been corrupted, there are also many invalid characters that should be ignored, even if they look like part of a mul instruction. Sequences like mul(4*
, mul(6,9!
, ?(12,34)
, or mul ( 2 , 4 )
do nothing.
For example, consider the following section of corrupted memory:
xmul(2,4)
%&mul[3,7]!@^donotmul(5,5)
+mul(32,64]then(mul(11,8)``mul(8,5)
)
Only the four highlighted sections are real mul instructions. Adding up the result of each instruction produces 161 (2*4 + 5*5 + 11*8 + 8*5)
.
Scan the corrupted memory for uncorrupted mul instructions. What do you get if you add up all of the results of the multiplications?
practice_input = ["xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5)"]
puzzle_input = Utilities.read_data(3) |> Utilities.to_list()
# puzzle_input = practice_input
defmodule DayThree do
def process_data(puzzle_input) do
Enum.at(puzzle_input, 0)
end
@mul_regex ~r/mul\(\d+,\d+\)/
@do_dont_regex ~r/mul\(\d+,\d+\)|do\(\)|don't\(\)/
@digit_regex ~r/\((?\d+),(?\d+)\)/
def part_one(puzzle_input) do
puzzle_input = puzzle_input |> process_data()
multiplications = Regex.scan(@mul_regex, puzzle_input)
Enum.reduce(multiplications, 0, fn multiplication, total ->
[string_multiplication] = multiplication
digits = Regex.named_captures(@digit_regex, string_multiplication)
result = String.to_integer(digits["first"]) * String.to_integer(digits["second"])
total + result
end)
end
def part_two(puzzle_input) do
puzzle_input = puzzle_input |> process_data()
split_input = Regex.scan(@do_dont_regex, puzzle_input)
Enum.reduce(split_input, {true, 0}, fn split, {flag, total} ->
[command] = split
cond do
command == "don't()" ->
{false, total}
command == "do()" ->
{true, total}
true ->
if flag do
digits = Regex.named_captures(@digit_regex, command)
result = String.to_integer(digits["first"]) * String.to_integer(digits["second"])
{flag, total + result}
else
{flag, total}
end
end
end)
end
end
cond do
2 + 2 == 5 ->
"This is never true"
2 * 2 == 3 ->
"Nor this"
true ->
"This is always true (equivalent to else)"
end
DayThree.part_two(puzzle_input)