Skip to content

Commit f926045

Browse files
committed
Solution: Day 3 - part 1 & 2
1 parent 854f58f commit f926045

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

solutions/2025/3/main.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# 'Advent of code' solution for year {year} day {day}
2+
import os
3+
import sys
4+
5+
DIR_PATH = os.path.dirname(os.path.realpath(__file__))
6+
7+
def _get_input():
8+
content = None
9+
if os.path.isfile(os.path.join(DIR_PATH, "input.txt")):
10+
with open(os.path.join(DIR_PATH, "input.txt"), "r", encoding="utf-8") as file:
11+
content = file.read().strip().splitlines()
12+
return content
13+
else:
14+
print("Error! Input file does not exist!")
15+
sys.exit()
16+
17+
def part_1(i: list[str]):
18+
result = 0
19+
20+
for bt_pack in i:
21+
max_num: int = 0
22+
second_max: int = 0
23+
24+
for index, batt in enumerate(bt_pack):
25+
if index != len(bt_pack)-1 and int(batt) > max_num:
26+
max_num = int(batt)
27+
second_max = 0
28+
elif int(batt) > second_max:
29+
second_max = int(batt)
30+
31+
result += max_num*10 + second_max
32+
33+
print(f"[P1]: Sum of all joltages: {result}")
34+
35+
def part_2(i: list[str]):
36+
result = 0
37+
required_batts = 12
38+
39+
for bt_pack in i:
40+
to_remove = len(bt_pack) - required_batts
41+
42+
value = ""
43+
for batt in bt_pack:
44+
while (to_remove > 0
45+
and len(value) > 0
46+
and int(value[-1]) < int(batt)):
47+
to_remove -= 1
48+
value = value[:-1]
49+
50+
value += batt
51+
52+
result += int(value[:12])
53+
54+
print(f"[P2]: Sum of all joltages: {result}")
55+
56+
if __name__ == "__main__":
57+
inputs = _get_input()
58+
if not inputs:
59+
print("Error! Input file is empty!")
60+
sys.exit()
61+
62+
part_1(inputs)
63+
part_2(inputs)
64+
65+
sys.exit()

0 commit comments

Comments
 (0)