Advent of Code 2022 day 3
There is definitely a saner way to do part 2 using itertools, but the
threes hack also worked.
with open("3.txt") as f:
lines = f.read().splitlines()
def value(x):
if x.islower():
return ord(x) - 96
return ord(x) - 38
def in_line(l):
return value(
set(l[:int(len(l)/2)]).intersection(set(l[int(len(l)/2):])).pop()
)
def in_lines(l1, l2, l3):
return value(set(l1).intersection(set(l2), set(l3)).pop())
def threes(lines):
l = []
count = 0
for line in lines:
if count == 3:
yield l
l = []
count = 0
l.append(line)
count += 1
yield l
print(sum([in_line(line) for line in lines if line]))
print(sum([in_lines(l1, l2, l3) for l1, l2, l3 in threes(lines)]))