Skip to content

Commit

Permalink
2024 day 3
Browse files Browse the repository at this point in the history
  • Loading branch information
Ted Cassirer committed Dec 3, 2024
1 parent 534fa88 commit d1f741a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 8 deletions.
13 changes: 5 additions & 8 deletions aoc_cas/aoc2024/day1.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,23 @@
def _parse_lists(data: str) -> tuple[list[int], list[int]]:
left_list, right_list = [], []
for line in data.splitlines():
left, right = line.split(" ")
left_list.append(int(left))
right_list.append(int(right))
left, right = map(int, line.split(" "))
left_list.append(left)
right_list.append(right)
return left_list, right_list


def part_a(data: str) -> int:
left, right = _parse_lists(data)
left.sort()
right.sort()
total_dist = 0
for l, r in zip(left, right):
total_dist += abs(l - r)
return total_dist
return sum(abs(r - l) for l, r in zip(left, right))


def part_b(data: str) -> int:
left, right = _parse_lists(data)
left_count, right_count = Counter(left), Counter(right)
return sum(k * left_count[k] * right_count[k] for k in left_count.keys())
return sum(k * left_count[k] * right_count[k] for k in left_count.keys() & right_count.keys())


if __name__ == "__main__":
Expand Down
29 changes: 29 additions & 0 deletions aoc_cas/aoc2024/day3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import re

from aoc_cas.cli import solve

MUL_REGEX = re.compile(r"mul\((\d+,\d+)\)")
DO_DONT_REGEX = re.compile(r"((don't\(\)).+?(do\(\)))|don't\(\).+?$")


def _sum_mults(data: str) -> int:
result = 0
for match in MUL_REGEX.findall(data):
n1, n2 = map(int, match.split(","))
result += n1 * n2
return result


def part_a(data: str) -> int:
return _sum_mults(data)


def part_b(data: str) -> int:
donts_removed = DO_DONT_REGEX.sub("", data.replace("\n", ""))
return _sum_mults(donts_removed)


if __name__ == "__main__":
from aoc_cas.util import solve_with_examples

solve_with_examples(year=2024, day=3)
8 changes: 8 additions & 0 deletions tests/fixtures/2024/3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))
mul
-
161
===
xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))
-
48

0 comments on commit d1f741a

Please sign in to comment.