Give it up for day 3 of (minimal :D) smooth brain shenanigans! https://pbs.twimg.com/media/E1bxikWWEAEGYHU.png

master
Ben Maurer 2022-12-03 10:30:55 -06:00
parent 3491af01a1
commit 041fbb83be
1 changed files with 37 additions and 0 deletions

37
day_3.py 100644
View File

@ -0,0 +1,37 @@
import aocd
from dotenv import load_dotenv
load_dotenv()
puzzle_input = aocd.get_data(day=3, year=2022)
rucksacks = puzzle_input.split('\n')
total_priority = 0
badges_priority = 0
for r, rucksack in enumerate(rucksacks):
# Part 1 =================================================
half_1 = set(rucksack[:len(rucksack)//2])
half_2 = set(rucksack[len(rucksack)//2:])
(in_both,) = half_1.intersection(half_2)
if in_both.islower():
total_priority = total_priority + ord(in_both) - 96
else:
total_priority = total_priority + ord(in_both) - 38
# Part 2 =================================================
if (r+1)%3 == 0:
rucksack_0prev = set(rucksack)
rucksack_1prev = set(rucksacks[r-1])
rucksack_2prev = set(rucksacks[r-2])
(in_badge,) = rucksack_0prev & rucksack_1prev & rucksack_2prev
print(in_badge)
if in_badge.islower():
badges_priority = badges_priority + ord(in_badge) - 96
else:
badges_priority = badges_priority + ord(in_badge) - 38
print(f'These elves are carrying a total of {total_priority:,} (prioritized?) things. That\'s a lotta stuff.')
print(f'They\'re also v important with a total badge priority of {badges_priority:,}.')