day 6
This commit is contained in:
parent
5701b1c876
commit
92f17f3046
|
|
@ -0,0 +1,22 @@
|
|||
import aocd
|
||||
|
||||
import operator
|
||||
import functools
|
||||
|
||||
def parse(data):
|
||||
op_table = {'+': operator.add, '*': operator.mul}
|
||||
|
||||
*nums, ops = (row.split() for row in data.strip().split('\n'))
|
||||
|
||||
nums = zip(*(map(int, row) for row in nums)) # zip(*l) <- ancient trick for transposing l; learned it from golfing :3
|
||||
|
||||
ops = (op_table[op] for op in ops)
|
||||
return zip(ops, nums)
|
||||
|
||||
def main(data):
|
||||
return sum(functools.reduce(f, nums) for f, nums in parse(data))
|
||||
|
||||
if __name__ == '__main__':
|
||||
solution = main(aocd.get_data(day=6, year=2025))
|
||||
print(solution)
|
||||
aocd.submit(solution, part='a', day=6, year=2025)
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
import aocd
|
||||
|
||||
import operator
|
||||
import functools
|
||||
|
||||
def parse(data):
|
||||
op_table = {'+': operator.add, '*': operator.mul}
|
||||
|
||||
cols = zip(*data.strip('\n').split('\n'))
|
||||
|
||||
problems = [[]]
|
||||
for col in cols:
|
||||
if any(c != ' ' for c in col):
|
||||
# Not an empty line
|
||||
problems[-1].append(col)
|
||||
else:
|
||||
# Empty line
|
||||
problems.append([])
|
||||
|
||||
return ((op_table[p[0][-1]], [int(''.join(col[:-1])) for col in p]) for p in problems)
|
||||
|
||||
def main(data):
|
||||
return sum(functools.reduce(f, nums) for f, nums in parse(data))
|
||||
|
||||
if __name__ == '__main__':
|
||||
solution = main(aocd.get_data(day=6, year=2025))
|
||||
print(solution)
|
||||
aocd.submit(solution, part='b', day=6, year=2025)
|
||||
Loading…
Reference in New Issue