-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday-13.py
137 lines (96 loc) · 2.53 KB
/
day-13.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# import all the utils I might use
from __future__ import annotations
from aocd import get_data
from aocd import submit
import re
from dataclasses import dataclass, field
import typing as T
from collections import defaultdict
from pprint import pprint
aoc_data = get_data(day=13, year=2022)
# problem part is 1 or 2
# problem_part = 2
# test
test_data = \
"""[1,1,3,1,1]
[1,1,5,1,1]
[[1],[2,3,4]]
[[1],4]
[9]
[[8,7,6]]
[[4,4],4,4]
[[4,4],4,4,4]
[7,7,7,7]
[7,7,7]
[]
[3]
[[[]]]
[[]]
[1,[2,[3,[4,[5,6,7]]]],8,9]
[1,[2,[3,[4,[5,6,0]]]],8,9]"""
use_real_data = False
# un-comment below when ready to use real data
use_real_data = True
if use_real_data:
print("Using AOC data")
data = aoc_data
else:
print("Using test data")
data = test_data
lines = data.split('\n')
# --------------- part 1 ------------
index_of_pairs = 1
pairs_in_right_order = []
def in_order(left, right):
def compare(l, r):
if isinstance(l, int) and isinstance(r, int):
if l < r:
return True
elif l == r:
return
elif l > r:
return False
else:
# coerce to lists, if necessary
l = [l] if not isinstance(l, list) else l
r = [r] if not isinstance(r, list) else r
# iterate and compare
for i in range(len(l)):
if i > len(r) - 1:
return False
res = compare(l[i], r[i])
if res is not None:
return res
if len(l) < len(r):
return True
return compare(left, right)
for i in range(0, len(lines), 3):
left = eval(lines[i])
right = eval(lines[i+1])
if in_order(left, right):
pairs_in_right_order.append(index_of_pairs)
print('found a pair')
index_of_pairs += 1
print(pairs_in_right_order)
ans = sum(pairs_in_right_order)
print(ans)
# --------------- part 2 ------------
packets = [[[2]], [[6]]]
for i in range(0, len(lines), 3):
# print(index_of_packets, lines[i], lines[i+1])
left = eval(lines[i])
right = eval(lines[i+1])
packets.append(left)
packets.append(right)
from functools import cmp_to_key
def comp_func(left, right):
return -1 if in_order(left, right) else 1
sorted_packets = sorted(packets, key=cmp_to_key(comp_func))
indices = []
two = [[2]]
six = [[6]]
for i in range(0, len(sorted_packets)):
if sorted_packets[i] == two or sorted_packets[i] == six:
indices.append(i + 1)
ans = indices[0] * indices[1]
print(ans)