day5 part1:

This commit is contained in:
Tyrel Souza 2024-12-05 01:15:48 -05:00
parent be3fdad22b
commit 30de9566c5
3 changed files with 1450 additions and 0 deletions

1367
2024/full/day05.txt Normal file

File diff suppressed because it is too large Load Diff

55
2024/python/day05.py Normal file
View File

@ -0,0 +1,55 @@
import matrix
import shared
def read(rows):
second = False
rules = []
updates = []
for row in rows:
if second:
updates.append(list(map(int, row.split(","))))
elif row == "":
second = True
else:
rules.append(list(map(int, row.split("|"))))
return rules, updates
# @shared.profile
def part1(rows):
rules, updates = read(rows)
bad = []
for idx, update in enumerate(updates):
for first, second in rules:
if first in update and second in update:
first_idx = update.index(first)
second_idx = update.index(second)
if first_idx > second_idx:
bad.append(idx)
break
total = 0
for idx, update in enumerate(updates):
if idx in bad:
continue
total += update[len(update) // 2]
print(total)
# @shared.profile
def part2(rows):
pass
def main():
rows = [row for row in shared.load_rows(5)]
with shared.elapsed_timer() as elapsed:
part1(rows)
print("🕒", elapsed())
rows = [row for row in shared.load_rows(5, True)]
with shared.elapsed_timer() as elapsed:
part2(rows)
print("🕒", elapsed())
if __name__ == "__main__":
main()

28
2024/samples/day05.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