Advent of Code 2022 Day 13 Solve

preview_player
Показать описание
Recording of me solving day 13 of Advent of Code 2022 in Python. Finished rank #33 and #13 for each part.

Finish times:
Part 1: 5:49
Part 2: 7:55 (+2:06)

Relatively straightforward implementation today, part 1 was pretty much "here are some rules for comparing elements", and part 2 was "sort all the elements with these comparison rules". I stumbled a bit on part 1 because I couldn't make up my mind with how to structure my code, since I was worried I'd have to modify it in part 2 and didn't want to write _completely_ awful code. For part 2 I just wrote a bubble sort from scratch because I thought it'd be the fastest to write.

0:00 - Part 1
5:54 - Part 2
8:00 - Commentary
9:43 - Explanation
Рекомендации по теме
Комментарии
Автор

def compare_nums(a, b):
return -1 if a < b else 0 if a == b else 1


def compare(a, b):
if type(a) == int and type(b) == int:
return compare_nums(a, b)

if type(a) == int and type(b) == list:
return compare([a], b)

if type(a) == list and type(b) == int:
return compare(a, [b])

for aa, bb in zip(a, b):
r = compare(aa, bb)
if r != 0:
return r

return compare_nums(len(a), len(b))

rastislavsvoboda
Автор

Neil, thank you so much for all your AoC videos! I really enjoy watching in the evening after I have worked through the problems myself. I appreciate how you explain what you are thinking while coding, so it is (somewhat) possible to follow your train of thought. A quick tip regarding your remarks around 14:30: You should take a look at functools.cmp_to_key() which lets you do this:

einarids