Skip to content

Instantly share code, notes, and snippets.

@vjeranc
Last active December 15, 2023 06:13
Show Gist options
  • Save vjeranc/4e55dd28fb8e618ab416274fc4d86e4e to your computer and use it in GitHub Desktop.
Save vjeranc/4e55dd28fb8e618ab416274fc4d86e4e to your computer and use it in GitHub Desktop.
def chsh(s):
hsh = 0
for c in s:
hsh += ord(c)
hsh *= 17
hsh %= 256
return hsh
hshsum = 0
bx = [dict() for _ in range(256)]
for s in input().split(','):
hshsum += chsh(s) # part 1
match s.split('='):
case [l, f]:
bx[chsh(l)][l] = int(f)
case [l]:
bx[chsh(l[:-1])].pop(l[:-1], 0)
print(hshsum)
print(sum(i*j*b[h] for i, b in enumerate(bx, 1) for j, h in enumerate(b, 1)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment