Skip to content

Instantly share code, notes, and snippets.

@kiliankoe
Created December 3, 2022 12:30
Show Gist options
  • Save kiliankoe/1913abca17fc45fddcc7cdc3a50d8c3a to your computer and use it in GitHub Desktop.
Save kiliankoe/1913abca17fc45fddcc7cdc3a50d8c3a to your computer and use it in GitHub Desktop.
Advent of Code 2022 Day 3 as solved by OpenAI's ChatGPT
# Read the input from the file
with open("input.txt") as f:
rucksacks = f.readlines()
# Initialize the sum of priorities to 0
sum_of_priorities = 0
# Loop over each rucksack
for rucksack in rucksacks:
# Split the rucksack into two compartments
comp1 = rucksack[:len(rucksack) // 2]
comp2 = rucksack[len(rucksack) // 2:]
# Create sets for each compartment
set1 = set(comp1)
set2 = set(comp2)
# Find the common items in the two compartments
common_items = set1.intersection(set2)
# Loop over the common items
for item in common_items:
# Convert the item to its priority
if item.islower():
# Lowercase items have priorities 1 through 26
priority = ord(item) - ord('a') + 1
else:
# Uppercase items have priorities 27 through 52
priority = ord(item) - ord('A') + 27
# Add the priority to the sum
sum_of_priorities += priority
# Print the sum of priorities
print(sum_of_priorities)
# Read the input from the file
with open("input.txt") as f:
rucksacks = f.readlines()
# Initialize the sum of priorities to 0
sum_of_priorities = 0
# Loop over the rucksacks in groups of three
for i in range(0, len(rucksacks), 3):
# Create sets for each rucksack
set1 = set(rucksacks[i].strip())
set2 = set(rucksacks[i + 1].strip())
set3 = set(rucksacks[i + 2].strip())
# Find the common items in the three rucksacks
common_items = set1.intersection(set2, set3)
# Loop over the common items
for item in common_items:
# Convert the item to its priority
if item.islower():
# Lowercase items have priorities 1 through 26
priority = ord(item) - ord('a') + 1
else:
# Uppercase items have priorities 27 through 52
priority = ord(item) - ord('A') + 27
# Add the priority to the sum
sum_of_priorities += priority
# Print the sum of priorities
print(sum_of_priorities)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment