Day 3

Part 1

  • Read in input file containing list of the contents of rucksacks
  • Each line contains items in 2 compartments equally divided
  • There is one component common to 2 compartments
  • This common component determines the priority (a-z 1-26, A-Z 27-52)
  • Get the sum of all components
with open("day3/input.txt") as f:
    input = [o.strip() for o in f.read().split("\n")]

pprint(input[:5])
['shzsFcPssFhjFssBzdpRcNHNZrpdJdJVJZ',
 'fwvMCntfCCbSbSbtDgDNrDtDtJHZVH',
 'GbCwwbwwnGrLhBzjFFFsWPhL',
 'PpCqRsqqmmtCwMJC',
 'LHFrLLHDSNHlfWNhDzmjzzJlJzPJMvPJjQ']
  • Create a function that finds the common item in the two compartments for each rucksack

common_item

 common_item (sample:str)

Find the common item in the 2 compartments

Type Details
sample str a sample rucksack containing items from 2 compartments
Returns str
samples = """vJrwpWtwJgWrhcsFMMfFFhFp
jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL
PmmdzqPrVvPwwTWBwg
wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn
ttgJtRGJQctTZtZT
CrZsJsPPZsGzwwsLwLmpwMDw""".split("\n")
print(common_item(samples[0]))
print(common_item(samples[1]))
print(common_item(samples[2]))
print(common_item(samples[3]))
print(common_item(samples[4]))
print(common_item(samples[5]))
p
L
P
v
t
s
  • Create a function that computes the priority for an item

priority

 priority (item:str)

Return the priority of an item

Type Details
item str return the priority of an item (single char)
Returns int
print(priority('a'))
print(priority('p'))
print(priority('A'))
print(priority('Z'))
1
16
27
52
  • Create a function that sums all the priorities of the common element for all the rucksacks

sum_priorities

 sum_priorities (inputs:list[str])

Return the sum of priorities of the common item for all the rucksacks

Type Details
inputs list a list of rucksacks, each containing 2 compartments
Returns int
print(sum_priorities(samples))
157
  • Finally the total score

Click on the Answer tab to view the answer

total_priorities = sum_priorities(input)
print(f'the correct answer for part 1 is {total_priorities}')
the correct answer for part 1 is 8185

Part 2

  • Group the rucksacks into 3 each

  • Create a generator that returns the inputs as groups of 3:


generate_group

 generate_group (inputs:list[str], sz:int=3)

Generate a subsetted list of rucksacks of size sz

Type Default Details
inputs list a list of rucksacks
sz int 3 group size
Returns typing.Generator[list[str], NoneType, NoneType]
groups = list(generate_group(samples))
print(groups[0])
print(groups[1])
['vJrwpWtwJgWrhcsFMMfFFhFp', 'jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL', 'PmmdzqPrVvPwwTWBwg']
['wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn', 'ttgJtRGJQctTZtZT', 'CrZsJsPPZsGzwwsLwLmpwMDw']
  • Create a function that finds the badge (i.e. the common element) for a group of rucksacks

find_group_badge

 find_group_badge (group:list[str])

Return the common item for the group of rucksacks

Type Details
group list group of rucksacks
Returns str
print(find_group_badge(groups[0]))
print(find_group_badge(groups[1]))
r
Z
  • Create a function that sums the priorities of the badges of each group in the list of rucksacks

sum_badge_priorities

 sum_badge_priorities (inputs:list[str], sz:int=3)

Return the sum of the priorities of the group badges for all inputs

Type Default Details
inputs list list of rucksacks
sz int 3 group size
Returns int
print(sum_badge_priorities(samples))
70

Click on the Answer tab to view the answer

total_badge_priorities = sum_badge_priorities(input)
print(f'the correct answer for part 2 is {total_badge_priorities}')
the correct answer for part 2 is 2817