05
This commit is contained in:
85
05/05.py
Normal file
85
05/05.py
Normal file
@@ -0,0 +1,85 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
file = "./input.txt"
|
||||||
|
from copy import deepcopy
|
||||||
|
|
||||||
|
def sort_by_rules(update:list[int]) -> list[int]:
|
||||||
|
temp = (deepcopy(update))
|
||||||
|
is_not_sorted = True
|
||||||
|
while is_not_sorted:
|
||||||
|
is_not_sorted = False
|
||||||
|
for rule in rules:
|
||||||
|
if rule[0] not in temp or rule[1] not in temp:
|
||||||
|
continue
|
||||||
|
elif temp.index(rule[0]) > temp.index(rule[1]):
|
||||||
|
temp.pop(temp.index(rule[0]))
|
||||||
|
temp.insert(temp.index(rule[1]), rule[0])
|
||||||
|
is_not_sorted = True
|
||||||
|
return temp
|
||||||
|
|
||||||
|
def order_updates():
|
||||||
|
for update in updates_not_ok:
|
||||||
|
updates_not_ok_ordered.append(sort_by_rules(update))
|
||||||
|
|
||||||
|
def rules_ok(update:list[int]) -> bool:
|
||||||
|
for rule in rules:
|
||||||
|
if rule[0] not in update or rule[1] not in update:
|
||||||
|
continue
|
||||||
|
elif update.index(rule[0]) > update.index(rule[1]):
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
def check_updates():
|
||||||
|
for update in updates:
|
||||||
|
if rules_ok(update):
|
||||||
|
updates_ok.append(sort_by_rules(update))
|
||||||
|
else:
|
||||||
|
updates_not_ok.append(update)
|
||||||
|
|
||||||
|
|
||||||
|
def sum_middle_page_numbers_of_updates(update_list:list[list[int]]) -> int:
|
||||||
|
result = 0
|
||||||
|
for u in update_list:
|
||||||
|
result += u[len(u)//2]
|
||||||
|
return result
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
solution = 0
|
||||||
|
rules = []
|
||||||
|
updates = []
|
||||||
|
updates_ok = []
|
||||||
|
updates_not_ok = []
|
||||||
|
updates_not_ok_ordered = []
|
||||||
|
input_file = open(file, "r")
|
||||||
|
for line in input_file:
|
||||||
|
line = line.strip()
|
||||||
|
if line == "":
|
||||||
|
continue
|
||||||
|
elif "|" in line: # create a dictionary for rules
|
||||||
|
a, b = line.split("|")
|
||||||
|
rules.append([int(a), int(b)])
|
||||||
|
elif "," in line:
|
||||||
|
tmp = []
|
||||||
|
line = line.split(",")
|
||||||
|
for z in line:
|
||||||
|
tmp.append(int(z))
|
||||||
|
updates.append(tmp)
|
||||||
|
input_file.close()
|
||||||
|
|
||||||
|
check_updates()
|
||||||
|
order_updates()
|
||||||
|
|
||||||
|
print("Rules:")
|
||||||
|
print(rules)
|
||||||
|
print("Updates:")
|
||||||
|
print(updates)
|
||||||
|
print("\nPART 1")
|
||||||
|
print("OK-Updates")
|
||||||
|
print(updates_ok)
|
||||||
|
print(f'Solution-P1:{sum_middle_page_numbers_of_updates(updates_ok)}')
|
||||||
|
print("\nPART 2")
|
||||||
|
print("Not-OK-Updates")
|
||||||
|
print(updates_not_ok)
|
||||||
|
print("Not-OK-Updates Ordered")
|
||||||
|
print(updates_not_ok_ordered)
|
||||||
|
print(f'Solution-P2:{sum_middle_page_numbers_of_updates(updates_not_ok_ordered)}')
|
||||||
28
05/ex.txt
Normal file
28
05/ex.txt
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
47|53
|
||||||
|
97|13
|
||||||
|
97|61
|
||||||
|
97|47
|
||||||
|
75|29
|
||||||
|
61|13
|
||||||
|
75|53
|
||||||
|
29|13
|
||||||
|
97|29
|
||||||
|
53|29
|
||||||
|
61|53
|
||||||
|
97|53
|
||||||
|
61|29
|
||||||
|
47|13
|
||||||
|
75|47
|
||||||
|
97|75
|
||||||
|
47|61
|
||||||
|
75|61
|
||||||
|
47|29
|
||||||
|
75|13
|
||||||
|
53|13
|
||||||
|
|
||||||
|
75,47,61,53,29
|
||||||
|
97,61,53,29,13
|
||||||
|
75,29,13
|
||||||
|
75,97,47,61,53
|
||||||
|
61,13,29
|
||||||
|
97,13,75,29,47
|
||||||
1380
05/input.txt
Normal file
1380
05/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user