day13 - now it's a correctly behaving comparator
This commit is contained in:
parent
41596e279c
commit
d916790cb1
28
day13.py
28
day13.py
@ -7,22 +7,22 @@ from typing import Any
|
|||||||
|
|
||||||
def packet_compare(left: list, right: list) -> int:
|
def packet_compare(left: list, right: list) -> int:
|
||||||
for l_value, r_value in zip(left, right):
|
for l_value, r_value in zip(left, right):
|
||||||
if isinstance(l_value, list) and isinstance(r_value, int):
|
if l_value == r_value:
|
||||||
r_value = [r_value]
|
continue
|
||||||
elif isinstance(l_value, int) and isinstance(r_value, list):
|
|
||||||
l_value = [l_value]
|
|
||||||
|
|
||||||
if r_value != l_value:
|
match l_value, r_value:
|
||||||
if isinstance(l_value, int):
|
case int(), int():
|
||||||
return compare(l_value, r_value)
|
return compare(l_value, r_value)
|
||||||
else:
|
case int(), list():
|
||||||
return packet_compare(l_value, r_value)
|
l_value = [l_value]
|
||||||
|
case list(), int():
|
||||||
|
r_value = [r_value]
|
||||||
|
|
||||||
|
if c := packet_compare(l_value, r_value):
|
||||||
|
return c
|
||||||
|
|
||||||
if len(left) != len(right):
|
|
||||||
return compare(len(left), len(right))
|
return compare(len(left), len(right))
|
||||||
|
|
||||||
return -1
|
|
||||||
|
|
||||||
|
|
||||||
class Day(AOCDay):
|
class Day(AOCDay):
|
||||||
inputs = [
|
inputs = [
|
||||||
@ -45,7 +45,7 @@ class Day(AOCDay):
|
|||||||
packets = self.parse_input()
|
packets = self.parse_input()
|
||||||
index_sum = 0
|
index_sum = 0
|
||||||
for x in range(len(packets) // 2):
|
for x in range(len(packets) // 2):
|
||||||
if packet_compare(packets[x * 2], packets[x * 2 + 1]) == -1:
|
if packet_compare(packets[x * 2], packets[x * 2 + 1]) < 1:
|
||||||
index_sum += x + 1
|
index_sum += x + 1
|
||||||
|
|
||||||
return index_sum
|
return index_sum
|
||||||
@ -53,9 +53,9 @@ class Day(AOCDay):
|
|||||||
def part2(self) -> Any:
|
def part2(self) -> Any:
|
||||||
packets = self.parse_input()
|
packets = self.parse_input()
|
||||||
packets.extend([[[2]], [[6]]])
|
packets.extend([[[2]], [[6]]])
|
||||||
sorted_packets = sorted(packets, key=cmp_to_key(packet_compare))
|
packets.sort(key=cmp_to_key(packet_compare))
|
||||||
|
|
||||||
return (sorted_packets.index([[2]]) + 1) * (sorted_packets.index([[6]]) + 1)
|
return (packets.index([[2]]) + 1) * (packets.index([[6]]) + 1)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user