2024 - Day 5 part 1

This commit is contained in:
kleph 2024-12-06 04:03:17 +01:00
parent d1e7ff9785
commit 14413dce5b
3 changed files with 1442 additions and 0 deletions

48
2024/5/5.py Normal file
View file

@ -0,0 +1,48 @@
#!/usr/bin/env python
# 2024 - Advent Of Code 5
from math import floor
# file = 'input_example.txt'
file = 'input.txt'
updates = []
rules = {}
update = 0
with open(file, encoding="utf-8") as f:
for line in f.read().splitlines():
if line == '':
update = 1
continue
if update == 0:
before, after = line.split('|')
if int(before) in rules:
l = rules[int(before)]
l.append(int(after))
rules[int(before)] = l
else:
rules[int(before)] = [int(after)]
else:
updates.append([int(x) for x in line.split(',')])
#print(f'{rules=}')
#print(f'{updates=}')
good_updates = []
for update in updates:
broken_rule = False
for num, page in enumerate(update):
if page in rules:
for p in rules[page]:
if p in update[0:num]:
broken_rule = True
break
if not broken_rule:
good_updates.append(update)
accum = 0
print(f'# good_updates {len(good_updates)}')
for u in good_updates:
accum += u[floor(len(u)/2)]
print(f'{accum=}')

1366
2024/5/input.txt Normal file

File diff suppressed because it is too large Load diff

28
2024/5/input_example.txt Normal file
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