Add day 1-5

This commit is contained in:
2024-12-18 23:45:11 -07:00
commit ebd8d45970
61 changed files with 932 additions and 0 deletions
View File
View File
+46
View File
@@ -0,0 +1,46 @@
#!/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
for print_order in print_orders:
bad = False
for i, x in enumerate(print_order):
for v in rules[x]:
if v in print_order:
if print_order.index(v) <= i:
bad = True
if bad:
print(f"{print_order} is bad")
continue
print(f"{print_order} is good")
count = count + int(print_order[len(print_order) // 2])
print(count)
+28
View 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
+14
View File
@@ -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"
View File
View File
View File
+69
View File
@@ -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)
+28
View 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
+14
View File
@@ -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"
View File