Last active
December 13, 2022 13:21
-
-
Save maksverver/a0df3674aa2239691bacc3ab7e9e7ac1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import json | |
def ParsePart(part): | |
a, b = part.splitlines() | |
return json.loads(a), json.loads(b) | |
pairs = list(map(ParsePart, sys.stdin.read().split('\n\n'))) | |
def Compare(a, b): | |
if isinstance(a, int) and isinstance(b, int): | |
return a - b | |
if isinstance(a, int): | |
return Compare([a], b) | |
if isinstance(b, int): | |
return Compare(a, [b]) | |
assert isinstance(a, list) and isinstance(b, list) | |
for x, y in zip(a, b): | |
if c := Compare(x, y): | |
return c | |
return len(a) - len(b) | |
def SolvePart1(): | |
return sum(i for i, (a, b) in enumerate(pairs, 1) if Compare(a, b) < 0) | |
def SolvePart2(): | |
marker1 = [[2]] | |
marker2 = [[6]] | |
lists = [list for pair in pairs for list in pair] | |
i1 = sum(Compare(list, marker1) < 0 for list in lists) | |
i2 = sum(Compare(list, marker2) < 0 for list in lists) | |
return (i1 + 1) * (i2 + 2) | |
print(SolvePart1()) | |
print(SolvePart2()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment