Skip to content

Commit 8921b40

Browse files
committed
2024-5 solutions
1 parent 0774b10 commit 8921b40

File tree

2 files changed

+148
-0
lines changed

2 files changed

+148
-0
lines changed

2024/day5/main1.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
def match_rules(update, rules):
2+
for start, end in rules:
3+
if start in update and end in update:
4+
if update.index(start) > update.index(end):
5+
return False
6+
return True
7+
8+
9+
def solution(lines: list[str]):
10+
ret = 0
11+
section = 0
12+
rules = []
13+
updates = []
14+
for line in lines:
15+
if line == '':
16+
section += 1
17+
continue
18+
if section == 0:
19+
rules.append(tuple(line.split('|')))
20+
elif section == 1:
21+
updates.append(line.split(','))
22+
23+
print(rules)
24+
print(updates)
25+
for update in updates:
26+
if match_rules(update, rules):
27+
ret += int(update[(len(update) - 1) // 2])
28+
...
29+
return ret
30+
31+
32+
data = '''47|53
33+
97|13
34+
97|61
35+
97|47
36+
75|29
37+
61|13
38+
75|53
39+
29|13
40+
97|29
41+
53|29
42+
61|53
43+
97|53
44+
61|29
45+
47|13
46+
75|47
47+
97|75
48+
47|61
49+
75|61
50+
47|29
51+
75|13
52+
53|13
53+
54+
75,47,61,53,29
55+
97,61,53,29,13
56+
75,29,13
57+
75,97,47,61,53
58+
61,13,29
59+
97,13,75,29,47
60+
'''.split('\n')
61+
62+
# data = open('input.txt', 'r').read().split('\n')
63+
64+
data = [datum for datum in data][:-1]
65+
66+
print(solution(data))

2024/day5/main2.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
from graphlib import TopologicalSorter
2+
import pyperclip as pc
3+
4+
5+
def match_rules(update, rules):
6+
for start, end in rules:
7+
if start in update and end in update:
8+
if update.index(start) > update.index(end):
9+
return False
10+
return True
11+
12+
def reorder(update, rules):
13+
graph = {}
14+
for start, end in rules:
15+
if start not in update:
16+
continue
17+
if start not in graph:
18+
graph[start] = set()
19+
graph[start].add(end)
20+
topo_sorted = list(TopologicalSorter(graph).static_order())[::-1]
21+
mp = {n: i for i, n in enumerate(topo_sorted)}
22+
return list(sorted(update, key=lambda n: mp[n]))
23+
24+
25+
def solution(lines: list[str]):
26+
ret = 0
27+
rules = []
28+
updates = []
29+
for line in lines:
30+
if '|' in line:
31+
rules.append(tuple(line.split('|')))
32+
elif ',' in line:
33+
updates.append(line.split(','))
34+
35+
bad_updates = []
36+
for update in updates:
37+
if not match_rules(update, rules):
38+
bad_updates.append(update)
39+
40+
reordered_updates = []
41+
for update in bad_updates:
42+
reordered_update = reorder(update, rules)
43+
reordered_updates.append(reordered_update)
44+
ret += int(reordered_update[(len(reordered_update)-1) // 2])
45+
return ret
46+
47+
48+
# data = '''47|53
49+
# 97|13
50+
# 97|61
51+
# 97|47
52+
# 75|29
53+
# 61|13
54+
# 75|53
55+
# 29|13
56+
# 97|29
57+
# 53|29
58+
# 61|53
59+
# 97|53
60+
# 61|29
61+
# 47|13
62+
# 75|47
63+
# 97|75
64+
# 47|61
65+
# 75|61
66+
# 47|29
67+
# 75|13
68+
# 53|13
69+
#
70+
# 75,47,61,53,29
71+
# 97,61,53,29,13
72+
# 75,29,13
73+
# 75,97,47,61,53
74+
# 61,13,29
75+
# 97,13,75,29,47
76+
# '''.split('\n')
77+
78+
data = open('../day5/input.txt', 'r').read().split('\n')
79+
80+
s = solution(data)
81+
print(s)
82+
pc.copy(s)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy