-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday13.py
68 lines (54 loc) · 1.85 KB
/
day13.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
#!/usr/bin/env python
import numpy as np
import json
def compare(left, right):
if type(left) is int and type(right) is int:
return left - right
elif type(left) is not int and type(right) is int:
return compare(left, [right])
elif type(left) is int and type(right) is not int:
return compare([left], right)
for leftElement, rightElement in zip(left, right):
difference = compare(leftElement, rightElement)
if difference != 0:
return difference
return len(left) - len(right)
f = open("ressources/day13.txt", "r")
lines = f.readlines()
inRightOrder = []
for i in range(0, len(lines), 3):
firstLine = json.loads(lines[i])
secondLine = json.loads(lines[i+1])
result = compare(firstLine, secondLine)
if result <= 0:
inRightOrder.append(i//3+1)
print("Number of correct packets is: {}".format(sum(inRightOrder)))
listOfPackets = []
for i in range(0, len(lines), 3):
firstLine = json.loads(lines[i])
secondLine = json.loads(lines[i+1])
listOfPackets.append(firstLine)
listOfPackets.append(secondLine)
organizedPackets = [listOfPackets.pop()]
while len(listOfPackets)> 0:
elementToOrganize = listOfPackets.pop()
idx = 0
isEnd = False
while compare(organizedPackets[idx], elementToOrganize) < 0:
idx += 1
if idx >= len(organizedPackets):
isEnd = True
break
if isEnd:
organizedPackets.append(elementToOrganize)
else:
organizedPackets.insert(idx, elementToOrganize)
idx2 = 0
while compare(organizedPackets[idx2], [[2]]) < 0:
idx2 += 1
organizedPackets.insert(idx2, elementToOrganize)
idx6 = 0
while compare(organizedPackets[idx6], [[6]]) < 0:
idx6 += 1
organizedPackets.insert(idx2, elementToOrganize)
print("Signal emergency is: {}".format((idx2+1)*(idx6+1)))