solve day 3 part 2
This commit is contained in:
parent
b65e1a3153
commit
aa8cb30920
2 changed files with 27 additions and 6 deletions
|
|
@ -10,17 +10,38 @@ def get_lines(filename: str) -> list:
|
|||
return [line.strip() for line in file.readlines()]
|
||||
|
||||
|
||||
def main():
|
||||
# lines = get_lines("sample-input.txt")
|
||||
lines = get_lines("input.txt")
|
||||
|
||||
def part1_get_result_sum(lines: list) -> int:
|
||||
result_sum = 0
|
||||
for line in lines:
|
||||
matches = re.findall(r"mul\((\d+),(\d+)\)", line)
|
||||
for match in matches:
|
||||
result_sum += int(match[0]) * int(match[1])
|
||||
return result_sum
|
||||
|
||||
print("Part 1: The sum of the results is", result_sum)
|
||||
|
||||
def part2_get_result_sum(lines: list) -> int:
|
||||
result_sum = 0
|
||||
active = True
|
||||
pattern = re.compile(r"mul\((\d{1,3}),(\d{1,3})$")
|
||||
for line in lines:
|
||||
chunks = line.split(")")
|
||||
for chunk in chunks:
|
||||
if active:
|
||||
if chunk.endswith("don't("):
|
||||
active = False
|
||||
elif match := re.search(pattern, chunk):
|
||||
result_sum += int(match.group(1)) * int(match.group(2))
|
||||
elif chunk.endswith("do("):
|
||||
active = True
|
||||
return result_sum
|
||||
|
||||
|
||||
def main():
|
||||
# lines = get_lines("sample-input.txt")
|
||||
lines = get_lines("input.txt")
|
||||
|
||||
print("Part 1: The sum of the results is", part1_get_result_sum(lines))
|
||||
print("Part 2: The sum of the results is", part2_get_result_sum(lines))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
|
|
|||
|
|
@ -1 +1 @@
|
|||
xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))
|
||||
xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue