Add day 1-5
This commit is contained in:
Executable
+69
@@ -0,0 +1,69 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import collections
|
||||
|
||||
filename = "example.txt"
|
||||
filename = "input.txt"
|
||||
|
||||
with open(filename) as f:
|
||||
input = f.readlines()
|
||||
input = [line.strip() for line in input]
|
||||
|
||||
rs = [
|
||||
line.split("|") for line in input if "|" in line
|
||||
]
|
||||
|
||||
rules = collections.defaultdict(list)
|
||||
|
||||
for r in rs:
|
||||
rules[r[0]].append(r[1])
|
||||
|
||||
print(rules)
|
||||
|
||||
|
||||
print_orders = [
|
||||
line.split(",") for line in input if "|" not in line and len(line) > 0
|
||||
]
|
||||
|
||||
count = 0
|
||||
|
||||
def validate_rule(order):
|
||||
for i, x in enumerate(print_order):
|
||||
for v in rules[x]:
|
||||
if v in print_order:
|
||||
if print_order.index(v) <= i:
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def fix_rule(order):
|
||||
to_fix = []
|
||||
for i, x in enumerate(print_order):
|
||||
for v in rules[x]:
|
||||
if v in print_order:
|
||||
if print_order.index(v) <= i:
|
||||
order.insert(print_order.index(v), x)
|
||||
order.pop(i + 1)
|
||||
return order
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
for print_order in print_orders:
|
||||
if not validate_rule(print_order):
|
||||
print(f"{print_order} is bad")
|
||||
#count = count + fix_rule(print_order)
|
||||
while True:
|
||||
new = fix_rule(print_order)
|
||||
if validate_rule(new):
|
||||
print_order = new
|
||||
break
|
||||
else:
|
||||
continue
|
||||
print(f"{print_order} is good")
|
||||
|
||||
count = count + int(print_order[len(print_order) // 2])
|
||||
|
||||
print(count)
|
||||
|
||||
@@ -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
|
||||
@@ -0,0 +1,14 @@
|
||||
[tool.poetry]
|
||||
name = "aoc"
|
||||
version = "0.1.0"
|
||||
description = ""
|
||||
authors = ["restitux <restitux@ohea.xyz>"]
|
||||
readme = "README.md"
|
||||
|
||||
[tool.poetry.dependencies]
|
||||
python = "^3.12"
|
||||
|
||||
|
||||
[build-system]
|
||||
requires = ["poetry-core"]
|
||||
build-backend = "poetry.core.masonry.api"
|
||||
Reference in New Issue
Block a user