File tree Expand file tree Collapse file tree 1 file changed +30
-0
lines changed
Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Original file line number Diff line number Diff line change 1+ from os import environ
2+ from functools import reduce
3+ from itertools import groupby
4+ from math import prod
5+
6+ lines = open ("06.sample.txt" if environ .get ("DEBUG" ) else "06.txt" ).read ().splitlines ()
7+
8+ print (
9+ sum (
10+ reduce (((int .__mul__ , int .__add__ )[c [- 1 ] == "+" ]), map (int , c [:- 1 ]))
11+ for c in zip (* [l .split () for l in lines if l .strip ()])
12+ )
13+ )
14+
15+ cols = ["" .join (c ) for c in zip (* [l .ljust (max (len (x ) for x in lines )) for l in lines ])]
16+
17+
18+ def do_block (g ):
19+ return (sum if any (c .endswith ("+" ) for c in g ) else prod )(
20+ int (c [:- 1 ].replace (" " , "" )) for c in g if c [:- 1 ].strip ()
21+ )
22+
23+
24+ print (
25+ sum (
26+ do_block (list (g ))
27+ for k , g in groupby (cols , key = lambda x : not x .strip ())
28+ if not k
29+ )
30+ )
You can’t perform that action at this time.
0 commit comments