Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions advent_of_code/2025(python)/day3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import heapq


def solve_day3_part1(input_):
res = 0
for line in input_:
rel = {}
max_ch, second_max_ch = '', ''
for i, ch in enumerate(line):
rel[ch] = i
if max_ch == '' or (ch > max_ch and i < len(line)-1):
second_max_ch = ''
max_ch = ch
elif second_max_ch == '' or ch > second_max_ch:
second_max_ch = ch

if rel[max_ch] < rel[second_max_ch]:
res += int(f"{max_ch}{second_max_ch}")
else:
res += int(f"{second_max_ch}{max_ch}")

return res


def solve_day3_part2(input_):
res = 0
for line in input_:
# construct a priority-queue, sort based on (largest, left-most) order
pq = []
for i, ch in enumerate(line):
pq.append((-1*int(ch), -1*(len(line)-i)))

numbers = 12
powers = ""
prev_index = float('inf')
heapq.heapify(pq)
while numbers > 0:
invalid_item = []
while True:
curr = heapq.heappop(pq)
if -1*curr[1] >= numbers and -1*(curr[1]) < prev_index:
powers += str(-1*curr[0])
prev_index = -1*curr[1]
break
else:
invalid_item.append(curr)

# add original back
for item in invalid_item:
heapq.heappush(pq, item)
numbers -= 1

res += int(powers)

return res
23 changes: 23 additions & 0 deletions advent_of_code/2025(python)/test_day3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import unittest

from day3 import solve_day3_part1, solve_day3_part2

question_input = [
"987654321111111",
"811111111111119",
"234234234234278",
"818181911112111",
]


class TestSample(unittest.TestCase):

def test_part1(self):
self.assertEqual(solve_day3_part1(question_input), 357)

def test_part2(self):
self.assertEqual(solve_day3_part2(question_input), 3121910778619)


if __name__ == "__main__":
unittest.main()
Loading