generated from public/aoc_template
66 lines
1.5 KiB
Python
66 lines
1.5 KiB
Python
from collections import defaultdict
|
|
|
|
from tools.aoc import AOCDay
|
|
from tools.math import mul
|
|
from typing import Any
|
|
|
|
|
|
class Day(AOCDay):
|
|
inputs = [
|
|
[
|
|
(4277556, "input6_test"),
|
|
(4076006202939, "input6"),
|
|
],
|
|
[
|
|
(3263827, "input6_test"),
|
|
(7903168391557, "input6"),
|
|
]
|
|
]
|
|
|
|
def parse_input(self, part2: bool = False):
|
|
lines = self.getInput()
|
|
ops = lines[-1].split()
|
|
|
|
ret = defaultdict(list)
|
|
if not part2:
|
|
for line in lines[:-1]:
|
|
for i, num in enumerate(map(int, line.split())):
|
|
ret[i].append(num)
|
|
else:
|
|
rotated = defaultdict(str)
|
|
for line in lines[:-1]:
|
|
for i, c in enumerate(line):
|
|
rotated[i] += c
|
|
|
|
i = 0
|
|
for _, line in sorted(rotated.items()):
|
|
if line.strip():
|
|
ret[i].append(int(line))
|
|
else:
|
|
i += 1
|
|
|
|
return [x for _, x in sorted(ret.items())], ops
|
|
|
|
|
|
def calc(self, part2: bool = False) -> int:
|
|
ans = 0
|
|
numbers, ops = self.parse_input(part2)
|
|
for i, nums in enumerate(numbers):
|
|
if ops[i] == '+':
|
|
ans += sum(nums)
|
|
else:
|
|
ans += mul(nums)
|
|
|
|
return ans
|
|
|
|
def part1(self) -> Any:
|
|
return self.calc()
|
|
|
|
def part2(self) -> Any:
|
|
return self.calc(part2=True)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
day = Day(2025, 6)
|
|
day.run(verbose=True)
|