2024 day 5

This commit is contained in:
rawhide kobayashi 2024-12-05 00:38:45 -06:00
parent 6edb1ba904
commit 68708d28f2
Signed by: rawhide_k
GPG Key ID: E71F77DDBC513FD7
4 changed files with 1471 additions and 0 deletions

70
2024/Day 5/gold.py Normal file

@ -0,0 +1,70 @@
import math
with open('updates.txt', 'r') as f:
updates = f.read().splitlines()
with open('rules.txt', 'r') as f:
rules = f.read().splitlines()
good_updates = []
corrected_updates = []
middle_page_count = 0
corrected_middle_page_count = 0
for update in updates:
good_update = False
for rule in rules:
num_a, num_b = rule.split('|')
if num_a in update and num_b in update:
if update.find(num_a) < update.find(num_b):
good_update = True
else:
good_update = False
break
if good_update:
good_updates.append(update)
else:
update_parse = list(map(int, update.split(',')))
corrected = False
while not corrected:
for rule in rules:
num_a, num_b = map(int, rule.split('|'))
if num_a in update_parse and num_b in update_parse:
if update_parse.index(num_a) > update_parse.index(num_b):
num_a, num_b = map(int, rule.split('|'))
num_a_index = update_parse.index(num_a)
num_b_index = update_parse.index(num_b)
update_parse.pop(num_b_index)
update_parse.insert(num_a_index, num_b)
for rule in rules:
num_a, num_b = map(int, rule.split('|'))
if num_a in update_parse and num_b in update_parse:
if update_parse.index(num_a) < update_parse.index(num_b):
corrected = True
else:
corrected = False
break
corrected_updates.append(update_parse)
print(update_parse)
for update in good_updates:
update_parse = list(map(int, update.split(',')))
middle_page_count += update_parse[math.floor(len(update_parse) / 2)]
for update in corrected_updates:
print(update)
print(update[math.floor(len(update) / 2)])
corrected_middle_page_count += update[math.floor(len(update) / 2)]
print(f'middle_page_count: {middle_page_count}')
print(f'corrected_middle_page_count: {corrected_middle_page_count}')

32
2024/Day 5/main.py Normal file

@ -0,0 +1,32 @@
import math
with open('updates.txt', 'r') as f:
updates = f.read().splitlines()
with open('rules.txt', 'r') as f:
rules = f.read().splitlines()
good_updates = []
middle_page_count = 0
for update in updates:
good_update = False
for rule in rules:
num_a, num_b = rule.split('|')
if num_a in update and num_b in update:
if update.find(num_a) < update.find(num_b):
good_update = True
else:
good_update = False
break
if good_update:
good_updates.append(update)
for update in good_updates:
update_parse = list(map(int, update.split(',')))
middle_page_count += update_parse[math.floor(len(update_parse) / 2)]
print(middle_page_count)

1176
2024/Day 5/rules.txt Normal file

File diff suppressed because it is too large Load Diff

193
2024/Day 5/updates.txt Normal file

@ -0,0 +1,193 @@
62,65,92,74,49,52,28,88,57,61,81,99,15
61,99,55,39,35,76,25,77,83,13,29,45,47
84,97,38,34,94,65,57,81,19,66,74
49,52,51,88,66,19,43,53,48,61,81,99,15,55,39,12,35,76,37
13,58,25,62,12,37,97,29,42,87,77
94,75,58,38,62,29,56,26,18,97,47,49,24,42,45,22,17,13,74,84,83,65,87
48,64,37,13,24,42,75
75,22,18,94,26,62,97,65,38,92,17,84,74,49,52,51,88,34,66
34,66,43,53,81,64,13
52,28,88,61,81,15,12
19,53,48,64,55,99,56,34,43,77,88,76,66,25,35
12,35,76,37,14,25,83,56,13,58,24,29,42,45,75,47,87,22,18,94,26,62,97
48,81,84,28,92,34,43,66,51,57,62,97,17,65,49,19,15,88,38,52,61,99,53
48,58,29,53,37,25,64,76,77,55,15,83,45
22,24,35,45,75,87,26,77,56,14,76,12,62,37,97,18,83,58,13,94,47
49,52,28,51,88,57,66,19,43,53,61,81,99,15,64,55,39,12,35,76,37
26,97,65,38,49,52,88,48,99
34,57,66,19,43,53,48,61,81,99,15,64,55,39,12,35,76,37,14,25,77,83,13
45,75,22,84,28
34,76,15,49,55,84,66,19,35,64,12
35,76,37,14,25,77,83,56,58,24,42,45,47,87,22,18,62,97,65
42,76,48,29,81,45,58,99,53,61,14,83,24,35,64
55,61,77,53,43,83,57,48,99,15,66,56,14,25,12
84,49,52,51,88,34,57,19,43,48,61,99,55,35,76
52,28,34,57,66,53,48,61,99,55,12,35,76,37,14
74,49,52,28,51,88,34,57,43,53,48,81,99,64,55,12,35,76,37
76,65,87,83,58,38,26,77,42,25,47,13,97,24,45,18,22,29,37,56,75,62,94
53,99,15,12,76,25,77,29,45
43,53,48,61,81,99,15,64,55,39,12,35,76,37,14,25,83,56,13,58,24,29,42
39,14,37,64,61,58,56
99,65,55,88,28,48,19
88,38,26,99,19,66,17,49,34,43,53,61,28,65,74,92,62
35,39,45,12,83,56,47,42,25,75,37,81,24,76,99,87,64,14,29
55,81,61,43,25,88,35,48,66,12,15,14,34,52,37,99,19,64,57
75,47,87,22,18,94,26,62,97,65,38,92,17,84,74,49,52,28,51,88,34,57,19
37,14,25,77,83,56,13,24,42,45,47,87,22,18,94,26,62,97,65,38,92
58,45,47,18,94,26,92,17,52,28,51
76,37,58,24,42,45,47
56,13,24,29,42,47,87,22,18,94,26,62,97,65,38,92,17,84,74,49,52
34,88,84,66,75,19,57,94,62,47,49
45,75,47,87,22,18,94,26,62,97,65,38,17,84,74,49,52,28,51,88,34,57,66
28,84,52,49,42,26,18,38,97,75,17,94,87
35,99,58,61,48,37,15,19,55,64,53,81,13,56,43,39,24
57,19,53,99,12,37,58
75,25,83,14,64,12,77,39,56,45,35,81,61,99,76,13,55,47,24,58,29,42,37
83,14,92,62,38,42,24,75,26,97,29,94,13,37,47,18,25,45,22
45,75,47,87,22,18,94,26,62,65,38,92,17,84,74,49,52,28,51,88,34,57,66
52,64,55,17,38,92,43,53,65,51,28,74,66,88,19,81,99,84,57,48,61,15,49
83,56,58,24,42
18,94,26,62,97,65,38,92,17,84,74,49,52,28,51,88,34,57,66,19,43,53,61
17,74,49,28,51,88,66,53,61,99,55,12,35
57,66,19,43,53,48,61,64,55,39,12,76,37,14,25,77,83,13,58
43,58,61,39,57,14,37,77,66,81,55,48,99,12,13,64,15,53,19,56,76
55,39,12,76,37,14,25,77,83,56,13,58,24,29,42,45,75,47,87,22,18,94,26
65,92,17,84,52,28,51,88,34,66,19,43,53,48,61,81,99,64,55
52,28,51,88,34,57,66,19,43,53,48,61,99,15,64,55,39,12,35,37,25
56,42,18,65,14,45,94,22,26,58,75,47,25
76,37,14,25,77,56,13,58,24,29,42,45,75,87,18,94,26,62,97,65,38
81,97,62,17,43,28,15,51,38
65,17,84,74,49,52,51,34,43,48,61,81,99,15,55
92,74,52,66,53,81,12
24,45,75,47,22,94,62,97,65,92,17,74,52,51,88
49,52,88,34,57,66,19,53,61,81,99,15,64,39,12,35,37
17,84,74,49,52,28,51,88,34,57,66,19,43,53,48,81,99,15,39,12,35
37,58,87,45,47,55,94,22,35,42,18,26,77
62,97,65,92,49,34,57,43,53,48,81,99,15
28,88,34,57,66,19,43,53,48,61,81,99,15,64,55,12,76,37,14,25,77
34,57,19,43,53,48,61,81,99,15,64,55,39,12,35,76,37,14,25,77,83,56,13
29,92,94,77,24,62,58,42,65,97,84,47,75,22,25,87,83,26,18
74,28,51,34,57,19,43,48,61,81,15,64,55,12,35,76,37
35,12,76,25,42,13,37,26,77,87,39,29,94,56,45,47,14,58,75,22,24,83,62
94,26,97,52,88,34,57,43,81
75,47,87,18,94,26,62,97,65,38,92,17,84,49,52,51,88,34,57,66,19
35,83,81,15,24
29,39,55,64,13,47,12,18,42,87,24,94,14
49,52,28,51,88,34,57,66,19,53,48,61,81,99,15,55,39,12,35,37,14
48,61,81,15,55,39,35,76,37,14,77,56,13,58,24,29,42,45,75
39,35,14,83,24,29,45,87,22,26,62
43,53,61,81,15,55,39,76,25,77,58,29,42
49,81,61,48,55,88,15,12,14,37,51,57,66,34,99,52,64,39,76
97,47,37,87,75,83,77,94,45,56,76,42,25,13,29,22,18,58,14,38,65
53,48,15,12,35,13,58,29,45
42,22,12,47,75,35,18,24,58,45,97,29,13,76,25,26,37,87,94,56,83
49,47,26,74,65,38,19,57,84,92,66,43,87,52,97,94,17,34,18,28,62
55,39,12,35,76,14,77,56,13,29,45,75,47,87,22,18,26
58,97,28,84,18,29,42,92,13,87,47
24,64,76,13,83,15,77,61,56,42,75,12,81,39,45,25,29,37,14,48,58,99,55
92,74,49,28,57,53,81
58,24,29,42,45,75,47,87,22,18,94,26,62,97,65,38,92,17,84,49,52,28,51
74,43,84,28,19,87,62,38,94,88,18,66,47,34,65
34,66,61,15,55,35,25,77,83
29,42,45,47,22,65,38,92,49,28,51,88,34
13,24,29,42,45,75,47,87,94,65,74,49,28
37,25,77,83,56,42,45,47,62,38,92
77,56,24,42,45,75,26,62,97,17,74
57,66,19,43,53,48,61,81,99,15,55,39,12,35,37,25,56
48,35,76,64,77,37,43,39,61,15,99
83,56,58,24,42,45,87,22,94,97,65,92,84,74,49
77,64,14,22,37,29,39,13,24,76,83,99,35
58,66,99,15,43,25,64,76,12,61,19,35,13,14,39
37,14,58,42,47,87,22,65,92
55,61,42,75,81,15,58,64,83,13,25,14,48,56,24,29,39,77,45,99,12
92,43,65,52,66,61,74,81,84,62,15,53,97,28,19,57,88,34,17,51,49
26,62,97,65,38,92,17,84,74,49,52,28,51,34,57,66,19,43,53,48,61,81,99
47,87,22,18,94,62,65,92,74,49,52,28,88,57,43
92,52,94,81,57,49,97
25,77,83,56,13,58,29,42,45,75,87,22,94,26,62,97,65,17,84
84,94,97,45,47,66,92,26,49,88,57
47,42,94,24,12,87,37,64,35
38,87,51,97,65,94,52,26,66,57,49,18,75,92,62,17,88,84,47,34,74,45,22
26,97,84,88,66,61,99
66,43,62,65,87,94,17,57,49,97,19,52,47
28,84,65,66,74,38,55,99,64
14,77,83,13,29,42,45,75,47,22,26,62,65
62,65,38,92,52,88,15
84,88,66,19,61,81,12
14,55,76,48,88,43,49,28,57,37,15,53,66,19,39,52,64,81,51
87,22,18,94,26,62,97,65,38,92,17,84,74,49,52,28,51,88,34,19,53
28,42,38,87,22,45,97,34,92,51,17,26,47,29,94,18,49,52,74,75,65,84,62
97,17,18,26,45,13,22,29,94,25,38,87,42,56,47,14,62,24,58
17,84,74,52,28,88,34,57,66,19,43,99,64,55,35
48,99,15,64,39,12,37,14,83,56,13,24,42,45,75
14,25,77,83,56,13,58,24,29,42,45,75,47,22,18,94,26,62,97,65,38,92,17
55,25,15,35,58,24,19,77,39,66,12,13,37,53,56,81,76,14,43,61,48,83,64
14,19,83,48,76,24,13,61,29,15,25,37,12,99,81
88,34,99,64,35,83,56
22,94,26,62,97,65,92,17,84,74,28,88,34,57,66,19,48
43,83,53,25,51,57,37
99,77,64,28,37,12,15,43,25
84,52,51,34,19,43,48,61,99,15,64,55,39,35,76
56,24,38,75,62,29,74,26,47,17,94,97,45,87,52,49,42,84,13
13,58,24,29,42,87,94,26,65,74,28
97,65,38,92,17,84,74,49,52,28,51,88,34,57,66,19,53,48,61,81,99,15,64
42,94,56,76,25,58,37,45,14,18,12,22,87,47,24,75,26,97,13
26,49,28,57,22,45,87,66,18,92,65
42,18,92,17,84,34,57
52,28,51,88,34,57,66,19,43,53,48,61,81,99,15,55,39,12,35,76,37,14,25
47,38,92,17,84,74,49,88,34,66,43
76,53,15,19,51,37,66,83,35,48,61,88,55,77,81
87,94,97,65,38,92,84,49,52,28,51,88,66,43,53
19,52,99,49,66,34,53,81,61,64,57,74,12,55,43,39,15,92,17,88,28
55,39,12,35,14,13,42,45,75
29,84,24,75,18,38,47,22,87,28,42,49,74,51,17,65,88,26,62,45,97,92,52
87,58,49,42,75,84,52
53,48,61,81,99,15,64,55,39,12,35,76,37,25,77,56,13,58,24,42,45
52,57,66,48,61,37,25
55,12,35,37,14,25,77,13,58,29,42,45,75,47,87,22,18
81,15,64,39,12,76,25,77,83,56,13,24,42,45,75,47,87
18,38,17,84,49,52,34,19,43,48,61
88,52,66,97,74,17,92,94,65,45,75,84,87,51,47
19,53,48,99,55,12,76,14,25,77,83,56,13,58,29
13,24,29,45,87,94,26,62,65,84,74,49,28
77,55,75,26,42,37,29
37,56,42,45,47,13,35,55,22,83,75,64,58,76,15,14,18
66,97,26,65,57,92,88,62,18,51,74,94,38,48,53,19,49,52,34
56,26,18,92,49,38,83,13,47,97,94
49,52,88,19,43,81,99,15,12,35,76,37,14
37,83,35,24,13,43,12,99,77,66,64,14,55,39,48,58,76
17,26,51,22,38,42,57,74,84,92,34
29,42,45,75,47,87,18,26,62,97,38,17,74,52,34
19,43,53,48,61,81,64,55,12,35,76,14,25,77,83,58,24
99,28,15,14,12,55,37,49,51
28,12,53,81,43,74,76,57,55,64,35,52,37,34,99,49,51,88,66,48,19,39,61
18,94,26,62,65,92,17,84,74,52,51,88,34,57,53,48,61
39,12,35,76,37,14,25,77,83,56,13,24,29,42,45,75,47,87,22,18,94,26,62
25,77,83,56,13,58,24,29,42,45,75,47,22,18,94,26,62,97,65,38,92,17,84
57,66,19,43,48,61,81,99,15,64,55,39,35,76,37,25,83,56,58
53,55,57,81,28,48,74,39,17,38,52,61,66,15,84,51,92
22,77,12,42,39,14,45,37,29,94,18,83,55,75,47,24,35,76,56,13,58,26,25
58,25,22,42,26,14,62,29,18,56,83
34,76,37,25,83,56,13
77,56,24,42,45,22,18,26,62,97,38,92,17,84,74
29,87,94,26,65,17,84,74,49,52,28,88,34
15,64,55,39,12,35,14,25,77,24,29,42,45,75,47,87,18
47,65,43,94,74,38,62
22,18,94,26,62,97,92,17,84,52,28,88,34,66,43,53,48
28,51,57,66,43,53,99,64,55,12,76,37,25
24,83,58,35,14,45,64,75,15,29,25,76,81,55,42,39,77,13,61,47,56
26,62,97,65,38,92,17,84,74,49,52,28,88,34,57,66,19,43,53,48,61,81,99
81,15,12,35,14,25,56,13,58,75,87
37,14,25,77,83,56,13,58,29,42,45,75,87,22,94,26,62,97,65,38,92
64,55,37,58,45,22,94
38,92,17,84,74,49,52,28,51,88,34,57,19,43,53,48,61,81,99,15,64,55,39
15,64,55,39,12,35,76,37,14,25,77,83,56,58,24,29,42,45,75,47,87,22,18
14,25,77,83,56,58,24,29,42,45,75,47,87,22,18,94,26,97,65,92,17
65,26,38,62,24,29,94,45,84,17,51,42,52,87,28
56,13,29,42,45,87,22,18,26,62,97,65,92,84,74,49,52
97,65,38,92,17,84,74,52,28,51,88,34,57,66,19,43,53,48,61,81,99,15,64
22,62,97,17,74,49,28,51,43,53,48
35,99,25,12,76,53,64,48,15,19,52,57,61,43,51,81,14,88,66,37,55,34,28
22,18,94,26,62,97,65,38,17,84,74,49,52,28,51,88,34,57,66,19,43,53,48
74,94,97,47,65,29,49,87,18,13,24,52,84,58,22,56,75,42,17,62,38
65,92,17,74,49,52,28,34,57,66,43,48,61,81,99,64,55