-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart1.py
70 lines (52 loc) · 1.53 KB
/
part1.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
from typing import List
import json
VALID = 1
NOT_VALID = 0
PASS = -1
def is_valid(left, right) -> int:
if isinstance(left, int) and isinstance(right, int):
if left < right:
return VALID
elif right < left:
return NOT_VALID
return PASS
elif isinstance(left, list) and isinstance(right, list):
for left_value, right_value in zip(left, right):
valid = is_valid(left_value, right_value)
if valid == VALID:
return VALID
elif valid == NOT_VALID:
return NOT_VALID
if len(left) < len(right):
return VALID
elif len(right) < len(left):
return NOT_VALID
return PASS
elif isinstance(left, int):
left = [left]
return is_valid(left, right)
elif isinstance(right, int):
right = [right]
return is_valid(left, right)
return NOT_VALID
def solution(inp: List[str]) -> int:
sum = 0
pair = 1
for i in range(0, len(inp), 3):
left = json.loads(inp[i])
right = json.loads(inp[i+1])
valid = is_valid(left, right)
if valid == PASS:
pair += 1
continue
if valid == VALID:
sum += pair
pair += 1
return sum
def result(inp: List[str]) -> int:
return solution(inp)
def test(examples: List[List[str]]) -> None:
example = 0
exp = 13
res = result(examples[example])
assert res == exp, f"example {example}: result was {res}, expected {exp}"