Advent of Code: Day 03
Mix.install([
{:kino, "~> 0.7.0"}
])
Day 03: Rucksack Reorganization
Each rucksack has two large compartments
All items go into exactly one of two compartments
The Elves have made a list of all of the items currently in each rucksack (your puzzle input), but they need your help finding the errors. Every item type is identified by a single lowercase or uppercase letter (that is, a
and A
refer to different types of items)
defmodule Helpers do
def read_file_contents(filename) do
file_path = "/Users/charlie/github.com/charlieroth/advent-of-code/2022/#{filename}"
case File.read(file_path) do
{:ok, contents} -> contents
{:error, _} -> raise("Failed to read file contents")
end
end
def find_common_character(group) when length(group) === 2 do
str2_split = String.split(Enum.at(group, 1), "", trim: true)
String.split(Enum.at(group, 0), "", trim: true)
|> Enum.map(fn c -> {c, Enum.member?(str2_split, c)} end)
|> Enum.filter(fn x -> elem(x, 1) === true end)
|> hd()
|> elem(0)
end
def find_common_character(group) when length(group) === 3 do
str3_split = String.split(Enum.at(group, 2), "", trim: true)
str2_split = String.split(Enum.at(group, 1), "", trim: true)
String.split(Enum.at(group, 0), "", trim: true)
|> Enum.map(fn c -> {c, Enum.member?(str2_split, c) && Enum.member?(str3_split, c)} end)
|> Enum.filter(fn x -> elem(x, 1) === true end)
|> hd()
|> elem(0)
end
def get_priority(letter) do
lower = [
"a",
"b",
"c",
"d",
"e",
"f",
"g",
"h",
"i",
"j",
"k",
"l",
"m",
"n",
"o",
"p",
"q",
"r",
"s",
"t",
"u",
"v",
"w",
"x",
"y",
"z"
]
upper = [
"A",
"B",
"C",
"D",
"E",
"F",
"G",
"H",
"I",
"J",
"K",
"L",
"M",
"N",
"O",
"P",
"Q",
"R",
"S",
"T",
"U",
"V",
"W",
"X",
"Y",
"Z"
]
is_lower = Enum.member?(lower, letter)
is_upper = Enum.member?(upper, letter)
cond do
is_lower ->
Enum.find_index(lower, &(&1 === letter)) + 1
is_upper ->
Enum.find_index(upper, &(&1 === letter)) + 26 + 1
end
end
end
Part 01
For example, suppose you have the following list of contents from six rucksacks:
vJrwpWtwJgWrhcsFMMfFFhFp
jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL
PmmdzqPrVvPwwTWBwg
wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn
ttgJtRGJQctTZtZT
CrZsJsPPZsGzwwsLwLmpwMDw
-
The first rucksack contains the items
vJrwpWtwJgWrhcsFMMfFFhFp
, which means its first compartment contains the itemsvJrwpWtwJgWr
, while the second compartment contains the itemshcsFMMfFFhFp
. The only item type that appears in both compartments is lowercasep
. -
The second rucksack’s compartments contain
jqHRNqRjqzjGDLGL
andrsFMfFZSrLrFZsSL
. The only item type that appears in both compartments is uppercaseL
. -
The third rucksack’s compartments contain
PmmdzqPrV
andvPwwTWBwg
; the only common item type is uppercaseP
. -
The fourth rucksack’s compartments only share item type
v
. -
The fifth rucksack’s compartments only share item type
t
. -
The sixth rucksack’s compartments only share item type
s
.
To help prioritize item rearrangement, every item type can be converted to a priority:
Lowercase item types a
through z
have priorities 1
through 26
.
Uppercase item types A
through Z
have priorities 27
through 52
.
In the above example, the priority of the item type that appears in both compartments of each rucksack is 16
(p
), 38
(L
), 42
(P
), 22
(v
), 20
(t
), and 19
(s
); the sum of these is 157
.
# ~S"""
# vJrwpWtwJgWrhcsFMMfFFhFp
# jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL
# PmmdzqPrVvPwwTWBwg
# wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn
# ttgJtRGJQctTZtZT
# CrZsJsPPZsGzwwsLwLmpwMDw
# """
Helpers.read_file_contents("inputs/Day03.txt")
|> String.split("\n", trim: true)
|> Enum.map(&String.split_at(&1, trunc(String.length(&1) / 2)))
|> Enum.map(&[elem(&1, 0), elem(&1, 1)])
|> Enum.map(&Helpers.find_common_character/1)
|> Enum.map(&Helpers.get_priority/1)
|> Enum.sum()
Part 02
For safety, the Elves are divided into groups of three. Every Elf carries a badge that identifies their group. For efficiency, within each group of three Elves, the badge is the only item type carried by all three Elves. That is, if a group’s badge is item type B, then all three Elves will have item type B somewhere in their rucksack, and at most two of the Elves will be carrying any other item type.
Additionally, nobody wrote down which item type corresponds to each group’s badges. The only way to tell which item type is the right one is by finding the one item type that is common between all three Elves in each group.
Every set of three lines in your list corresponds to a single group, but each group can have a different badge item type. So, in the above example, the first group’s rucksacks are the first three lines:
vJrwpWtwJgWrhcsFMMfFFhFp
jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL
PmmdzqPrVvPwwTWBwg
And the second group’s rucksacks are the next three lines:
wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn
ttgJtRGJQctTZtZT
CrZsJsPPZsGzwwsLwLmpwMDw
In the first group, the only item type that appears in all three rucksacks is lowercase r
; this must be their badges. In the second group, their badge item type must be Z
.
Priorities for these items must still be found to organize the sticker attachment efforts: here, they are 18
(r
) for the first group and 52
(Z
) for the second group. The sum of these is 70
.
Find the item type that corresponds to the badges of each three-Elf group. What is the sum of the priorities of those item types?
# ~S"""
# vJrwpWtwJgWrhcsFMMfFFhFp
# jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL
# PmmdzqPrVvPwwTWBwg
# wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn
# ttgJtRGJQctTZtZT
# CrZsJsPPZsGzwwsLwLmpwMDw
# """
Helpers.read_file_contents("inputs/Day03.txt")
|> String.split("\n", trim: true)
|> Enum.chunk_every(3)
|> Enum.map(&Helpers.find_common_character/1)
|> Enum.map(&Helpers.get_priority/1)
|> Enum.sum()