Day 3 – Sorting the Food Into Sacks

The Problem Part 1:

One Elf has the important job of loading all of the rucksacks with supplies for the jungle journey. Unfortunately, that Elf didn’t quite follow the packing instructions, and so a few items now need to be rearranged.

Each rucksack has two large compartments. All items of a given type are meant to go into exactly one of the two compartments. The Elf that did the packing failed to follow this rule for exactly one item type per rucksack.

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).

The list of items for each rucksack is given as characters all on a single line. A given rucksack always has the same number of items in each of its two compartments, so the first half of the characters represent items in the first compartment, while the second half of the characters represent items in the second compartment.

Part 2 Problem:

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.

The problem is that someone forgot to put this year’s updated authenticity sticker on the badges. All of the badges need to be pulled out of the rucksacks so the new authenticity stickers can be attached.

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.

So, the actual input on a lot of these puzzles is a numerical value, for this problem, it wanted the totals for the Food ID and Badge IDs, based on 1-26 and 27-52, depending on the value.  The simple way of summing these is the list at the top.  Each value is just the index of that list.

I also have this really messy line for ” contents = [[each[:int(len(each)/2)],each[int(len(each)/2):]] for each in data.split(‘\n’)]” in there, to break up each line into a list of lists.  Split can’t be used because each line is just a long list of jumbled letters.  The next line uses a function that I came across looking for an easy way to group everyone up into blocks of 3 for the second part.  I found the “zip” function, which is pretty cool and I’ve used it later in another puzzle as well.

The original Part 1 version of this code just ran through the individual contents variable.  But when I added in Part 2, with the 3 Elf Groups, I modified it to run off of groups of 3, while still solving both problems at once.  The actual finding of the items and badges was not hard, it’s just checking to see if values of one string are in another string using a loop.  A List Comprehension would work too, but for simple loops like this I find them more readable.

with open("Day03Input.txt") as file:
    data = file.read()

letters = [
    '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', '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']

contents = [[each[:int(len(each)/2)],each[int(len(each)/2):]] for each in data.split('\n')]

groups = zip(*[iter(contents)]*3)
def find_item(elf):
    for i in elf[0]:
        if i in elf[1]:
            return letters.index(i) + 1

def find_badge(group):
    elves = [group[0][0]+group[0][1],group[1][0]+group[1][1],group[2][0]+group[2][1]]
    for i in elves[0]:
        if i in elves[1] and i in elves[2]:
            return letters.index(i)+1

priority_sum = 0
badge_sum = 0

for each_group in groups:
    for elf in each_group:
        priority_sum += find_item(elf)
    badge_sum += find_badge(each_group)

print(priority_sum)
# 7526 Too Low
# 7826
print(badge_sum)
# 2577