Below is my attempt at Advent of Code 2022 - Day 3 - Part 1. That link will bring you to the problem description.
I'd like to get some feedback on this. I'm new to Python, started working with it for this years challenge in order to learn. I'm quite certain there are better ways to solve this particular problem. I'd appreciate some peer review on alternative solutions to improve my technique.
Problem Text
--- Day 3: Rucksack Reorganization ---
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.
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 items vJrwpWtwJgWr, while the second compartment contains the items hcsFMMfFFhFp. The only item type that appears in both compartments is lowercase p.
- The second rucksack's compartments contain jqHRNqRjqzjGDLGL and rsFMfFZSrLrFZsSL. The only item type that appears in both compartments is uppercase L.
- The third rucksack's compartments contain PmmdzqPrV and vPwwTWBwg; the only common item type is uppercase P.
- 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.
Find the item type that appears in both compartments of each rucksack. What is the sum of the priorities of those item types?
from pathlib import Path
from string import ascii_letters as allAsciiLetters
def buildAsciiDictionary():
dictionary = {}
counter = 1
for i in allAsciiLetters:
dictionary[i] = counter
counter += 1
return dictionary
def readInputsFromFile(filePath):
inputs = []
with open(filePath) as file:
while True:
line = file.readline().strip()
if not line:
break
else:
inputs.append(line)
return inputs
# Attempt 2
def evaluateInputs(inputs, asciiDictionary):
sum = 0
duplicateToken = ''
duplicateFound = False
for input in inputs:
# split the string into two equal halves
firstHalf, secondHalf = input[:len(input)//2], input[len(input)//2:]
# compare each character in the first half, to every character in the second half
for character1 in firstHalf:
if duplicateFound:
break
for character2 in secondHalf:
if character1 == character2:
duplicateToken = character1
duplicateFound = True
sum += asciiDictionary[duplicateToken]
break
duplicateFound = False
return sum
def main():
print("Welcome to day 3 of AoC")
pathToFile = Path(__file__).with_name('day3-1_input.txt')
inputs = readInputsFromFile(pathToFile)
asciiDictionary = buildAsciiDictionary()
sum = evaluateInputs(inputs, asciiDictionary)
print("The sum of duplicated items is: " + str(sum))
main()