Skip to content

Instantly share code, notes, and snippets.

@Wowfunhappy
Created January 23, 2024 22:47
Show Gist options
  • Save Wowfunhappy/975ca3e00681ff694b2bb438493eb46f to your computer and use it in GitHub Desktop.
Save Wowfunhappy/975ca3e00681ff694b2bb438493eb46f to your computer and use it in GitHub Desktop.
Advent of Code 2022 Day 3
input = open("input.txt")
rucksacks = input.readlines()
commonItems = []
def findCommonItem(compartmentA, compartmentB):
for i in range(0, len(compartmentA)):
for j in range(0, len(compartmentB)):
if (compartmentA[i] == compartmentB[j]):
return compartmentA[i]
for rucksack in rucksacks:
rucksackSize = int(len(rucksack))
compartmentSize = int(rucksackSize / 2)
compartmentA = rucksack[0:compartmentSize]
compartmentB = rucksack[compartmentSize:rucksackSize]
commonItems.append(findCommonItem(compartmentA, compartmentB))
priorities = {
"a": 1,
"b": 2,
"c": 3,
"d": 4,
"e": 5,
"f": 6,
"g": 7,
"h": 8,
"i": 9,
"j": 10,
"k": 11,
"l": 12,
"m": 13,
"n": 14,
"o": 15,
"p": 16,
"q": 17,
"r": 18,
"s": 19,
"t": 20,
"u": 21,
"v": 22,
"w": 23,
"x": 24,
"y": 25,
"z": 26,
"A": 27,
"B": 28,
"C": 29,
"D": 30,
"E": 31,
"F": 32,
"G": 33,
"H": 34,
"I": 35,
"J": 36,
"K": 37,
"L": 38,
"M": 39,
"N": 40,
"O": 41,
"P": 42,
"Q": 43,
"R": 44,
"S": 45,
"T": 46,
"U": 47,
"V": 48,
"W": 49,
"X": 50,
"Y": 51,
"Z": 52
}
prioritiesSum = 0
for commonItem in commonItems:
prioritiesSum += priorities[commonItem]
print(prioritiesSum)
input = open("input.txt")
rucksacks = input.readlines()
groups = []
i = 0
while i < len(rucksacks):
group = []
j = 0
while j < 3:
group.append(rucksacks[i])
i += 1
j += 1
groups.append(group)
def findCommonItem(groupA, groupB, groupC):
for i in range(0, len(groupA)):
for j in range(0, len(groupB)):
if (groupA[i] == groupB[j]):
for k in range(0, len(groupC)):
if groupA[i] == groupC[k]:
return groupA[i]
badges = []
for group in groups:
badges.append(findCommonItem(group[0], group[1], group[2]))
priorities = {
"a": 1,
"b": 2,
"c": 3,
"d": 4,
"e": 5,
"f": 6,
"g": 7,
"h": 8,
"i": 9,
"j": 10,
"k": 11,
"l": 12,
"m": 13,
"n": 14,
"o": 15,
"p": 16,
"q": 17,
"r": 18,
"s": 19,
"t": 20,
"u": 21,
"v": 22,
"w": 23,
"x": 24,
"y": 25,
"z": 26,
"A": 27,
"B": 28,
"C": 29,
"D": 30,
"E": 31,
"F": 32,
"G": 33,
"H": 34,
"I": 35,
"J": 36,
"K": 37,
"L": 38,
"M": 39,
"N": 40,
"O": 41,
"P": 42,
"Q": 43,
"R": 44,
"S": 45,
"T": 46,
"U": 47,
"V": 48,
"W": 49,
"X": 50,
"Y": 51,
"Z": 52
}
prioritiesSum = 0
for badge in badges:
prioritiesSum += priorities[badge]
print(prioritiesSum)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment