Skip to content

Instantly share code, notes, and snippets.

@rtt
Created December 4, 2022 10:59
Show Gist options
  • Save rtt/76e53e823d3f571f611bd3980646bfc7 to your computer and use it in GitHub Desktop.
Save rtt/76e53e823d3f571f611bd3980646bfc7 to your computer and use it in GitHub Desktop.
from string import ascii_letters
from itertools import zip_longest
def chunk(things, num):
things = [iter(things)] * num
return zip_longest(fillvalue=None, *things)
def part1(inp):
items = []
vals = {v: k for k, v in enumerate(ascii_letters, start=1)}
for line in inp:
a, b = set(line[:int(len(line)/2)]), set(line[int(len(line)/2):])
if a & b:
items.append(vals[(a & b).pop()])
return sum(items)
def part2(inp):
items = []
vals = {v: k for k, v in enumerate(ascii_letters, start=1)}
chunks = chunk(inp, 3)
commons = []
for c in chunks:
a, b, c = set(c[0]), set(c[1]), set(c[2])
if a & b & c:
commons.append(vals[(a & b & c).pop()])
return sum(commons)
if __name__ == '__main__':
with open('./input.txt') as f:
inp = f.read().strip().split('\n')
print(part1(inp))
print(part2(inp))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment