-
Notifications
You must be signed in to change notification settings - Fork 0
/
day5.py
55 lines (48 loc) · 1.54 KB
/
day5.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
#!/usr/bin/env python3
import numpy as np
import regex as re
from collections import defaultdict
f = open("ressources/day5.txt", "r") #Open File
lines = f.readlines() #Separate in lines
#Read Data
rules = defaultdict(set)
prints = []
for line in lines:
line = line.replace("\n","")
if "|" in line:
numbers = line.split("|")
rules[int(numbers[0])].add(int(numbers[1]))
if "," in line:
prints.append([int(x) for x in line.split(",")])
#Q1
def checkIfCorrect(printing, rules):
printing = printing.copy()
doAdd = True
while len(printing) > 0:
elem = printing.pop(0)
if any([elem in rules[element] for element in printing]):
doAdd = False
break
return doAdd
total = 0
#Identify correct prints
for printing in prints:
middle = printing[len(printing)//2]
doAdd = checkIfCorrect(printing, rules)
total += middle*int(doAdd)
print("Total of middle correct prints is: {}".format(total))
#Q2
total = 0
for printing in prints:
if not checkIfCorrect(printing, rules):
while not checkIfCorrect(printing, rules):
elem = printing.pop()
indexes = [printing.index(rule) if rule in printing else 1000 for rule in rules[elem]]
minIndex = min(indexes)
if minIndex < len(printing):
printing.insert(minIndex,elem)
else:
printing.insert(0, elem)
middle = printing[len(printing)//2]
total += middle
print("Total of middle corrected prints is: {}".format(total))