-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.py
More file actions
68 lines (52 loc) · 1.55 KB
/
solution.py
File metadata and controls
68 lines (52 loc) · 1.55 KB
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
"""
Advent of Code 2025 - Day 3
"""
def part1(input_data):
"""
Solve part 1 of the puzzle.
Find the largest joltage battery with 2 digits.
"""
total = 0
for line in input_data:
batteries = [int(i) for i in line]
first_largest_num = max(batteries[:-1])
first_largest_num_idx = batteries.index(first_largest_num)
second_largest_num = max(batteries[first_largest_num_idx + 1 :])
total += (10 * first_largest_num) + second_largest_num
return total
def part2(input_data):
"""
Solve part 2 of the puzzle.
Find the largest joltage battery with 12 digits.
"""
total = 0
for line in input_data:
batteries = [int(i) for i in line]
digits_to_remove = len(batteries) - 12
result = []
for i, num in enumerate(batteries):
while (
result
and digits_to_remove > 0
and num > result[-1]
and len(batteries) - i > 12 - len(result)
):
result.pop()
digits_to_remove -= 1
result.append(num)
while len(result) > 12:
result.pop()
number = int("".join(map(str, result)))
total += number
return total
def main():
# Read input
with open("day03/input.txt", "r") as f:
input_data = f.read().strip().split("\n")
# Solve
result1 = part1(input_data)
result2 = part2(input_data)
print(f"Part 1: {result1}")
print(f"Part 2: {result2}")
if __name__ == "__main__":
main()