Skip to content

Instantly share code, notes, and snippets.

@j9ac9k
Created December 13, 2022 18:08
Show Gist options
  • Save j9ac9k/6735a21530d84d6964e736b9dba7f75c to your computer and use it in GitHub Desktop.
Save j9ac9k/6735a21530d84d6964e736b9dba7f75c to your computer and use it in GitHub Desktop.
Aoc 2022 Day 13 Solution
import itertools
import functools
def compare(a, b):
if isinstance(a, int) and isinstance(b, int):
if a < b:
return True
elif a > b:
return False
else:
# they're equal
return None
elif isinstance(a, list) and isinstance(b, list):
for c, d in zip(a, b):
output = compare(c, d)
if output is None:
continue
elif output is True:
return True
elif output is False:
return False
if len(a) == len(b):
return None
elif len(a) > len(b):
return False
else:
return True
elif isinstance(a, int) and isinstance(b, list):
return compare([a], b)
elif isinstance(a, list) and isinstance(b, int):
return compare(a, [b])
else:
raise RuntimeError
def solve_p2(input_file):
with open(input_file) as aoc_input:
codes = [[[2]], [[6]]]
for is_newline, pair in itertools.groupby((line.strip() for line in aoc_input), lambda line: line == ""):
if is_newline:
continue
else:
codes.extend([eval(line) for line in pair])
results = sorted(codes, key=functools.cmp_to_key(lambda a, b: 1 if compare(a, b) else -1 if compare(b, a) else 0), reverse=True)
first = results.index(codes[0]) + 1
second = results.index(codes[1]) + 1
print(first * second)
def solve(input_file):
index = 1
is_correct = set()
with open(input_file) as aoc_input:
for is_newline, pair in itertools.groupby((line.strip() for line in aoc_input), lambda line: line == ""):
if is_newline:
index += 1
else:
left, right = pair
output = compare(eval(left), eval(right))
if output is False:
continue
elif output is True:
is_correct.add(index)
continue
else:
breakpoint()
print(sum(is_correct))
breakpoint()
def main():
solve("day13.txt")
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment