Compare commits

...

2 Commits

Author SHA1 Message Date
Mikael CAPELLE
b21c50562f Refactor 2024 day 3.
All checks were successful
continuous-integration/drone/push Build is passing
2024-12-03 15:22:37 +01:00
Mikael CAPELLE
211483f679 Add .drone.yml for CI.
All checks were successful
continuous-integration/drone Build is passing
2024-12-03 14:12:42 +01:00
2 changed files with 19 additions and 9 deletions

12
.drone.yml Normal file
View File

@ -0,0 +1,12 @@
---
kind: pipeline
type: docker
name: default
steps:
- name: tests
image: python:3.10-slim
commands:
- pip install poetry
- poetry install
- poetry run poe lint

View File

@ -4,19 +4,19 @@ from typing import Iterator
def extract_multiply(line: str) -> Iterator[int]: def extract_multiply(line: str) -> Iterator[int]:
for m in re.findall(r"mul\(([0-9]{1,3}),\s*([0-9]{1,3})\)", line): for m in re.finditer(r"mul\(([0-9]{1,3}),\s*([0-9]{1,3})\)", line):
yield int(m[0]) * int(m[1]) yield int(m.group(1)) * int(m.group(2))
def remove_disabled_parts(line: str) -> str: def valid_memory_blocks(line: str) -> Iterator[str]:
fixed_line, accumulate = "", True accumulate = True
while line: while line:
if accumulate: if accumulate:
if (dont_i := line.find("don't()")) != -1: if (dont_i := line.find("don't()")) != -1:
fixed_line += line[:dont_i] yield line[:dont_i]
line, accumulate = line[dont_i:], False line, accumulate = line[dont_i:], False
else: else:
fixed_line += line yield line
line = "" line = ""
else: else:
if (do_i := line.find("do()")) != -1: if (do_i := line.find("do()")) != -1:
@ -24,13 +24,11 @@ def remove_disabled_parts(line: str) -> str:
else: else:
line = "" line = ""
return fixed_line
line = sys.stdin.read().strip() line = sys.stdin.read().strip()
answer_1 = sum(extract_multiply(line)) answer_1 = sum(extract_multiply(line))
answer_2 = sum(extract_multiply(remove_disabled_parts(line))) answer_2 = sum(sum(extract_multiply(block)) for block in valid_memory_blocks(line))
print(f"answer 1 is {answer_1}") print(f"answer 1 is {answer_1}")
print(f"answer 2 is {answer_2}") print(f"answer 2 is {answer_2}")