-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsolution.py
74 lines (60 loc) · 2.02 KB
/
solution.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
def parse_input(filename):
rules = []
sequences = []
is_rules = True
with open(filename, 'r') as f:
for line in f:
line = line.strip()
if not line:
is_rules = False
continue
if is_rules:
before, after = line.split('|')
rules.append((int(before), int(after)))
else:
nums = [int(x) for x in line.split(',')]
sequences.append(nums)
return rules, sequences
def check_order(sequence, rules):
for before, after in rules:
if before in sequence and after in sequence:
if sequence.index(before) > sequence.index(after):
return False
return True
def get_middle(sequence):
return sequence[len(sequence) // 2]
def part_one(rules, sequences):
total = 0
for sequence in sequences:
if check_order(sequence, rules):
total += get_middle(sequence)
return total
def order_sequence(sequence, rules):
nums = sequence.copy()
size = len(nums)
for i in range(size):
for j in range(size - i - 1):
for before, after in rules:
if nums[j] == after and nums[j+1] == before:
nums[j], nums[j+1] = nums[j+1], nums[j]
return nums
def part_two(rules, sequences):
total = 0
for sequence in sequences:
if not check_order(sequence, rules):
ordered = order_sequence(sequence, rules)
total += get_middle(ordered)
return total
def main():
print("Parsing input...")
rules, sequences = parse_input('input.txt')
print("\nProcessing Part 1...")
result1 = part_one(rules, sequences)
print("\nProcessing Part 2...")
result2 = part_two(rules, sequences)
# Final results display
print("\n=== Final Results ===")
print(f"Part 1: Sum of middle numbers from valid sequences: {result1}")
print(f"Part 2: Sum of middle numbers from fixed sequences: {result2}")
if __name__ == "__main__":
main()