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
1 change: 0 additions & 1 deletion advent_of_code/2024(python)/day1.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,3 @@ def solve_day1_part2(inp:list[str]) -> int:
res += left_item * right_counter.get(left_item, 0)

return res

40 changes: 40 additions & 0 deletions advent_of_code/2024(python)/day3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import re

def solve_day3_part1(instruction:str) -> int:

res = 0
match = re.findall(r'mul\((\d+),(\d+)\)', instruction)
for (item1, item2) in match:
res += int(item1) * int(item2)

return res

def _is_do_command(instruction:str, i:int) -> bool:
return (i+3)<len(instruction) and instruction[i:i+4] == "do()"

def _is_dont_command(instruction:str, i:int) -> bool:
return (i+6)<len(instruction) and instruction[i:i+7] == "don't()"

def solve_day3_part2(instruction:str) -> int:

res = 0
index = 0

new_instruction = "do()" + instruction + "don't()"
while index < len(new_instruction):
# !^don't()
if(index==len(new_instruction)-7) and new_instruction[index:] == "don't()":
break

left_index = right_index = 0
while not _is_do_command(new_instruction, index):
left_index = index
index += 1

while not _is_dont_command(new_instruction, index):
right_index = index
index += 1

res += solve_day3_part1(new_instruction[left_index:right_index+1])

return res
Loading
Loading