diff options
author | Tomasz Kramkowski <tk@the-tk.com> | 2021-12-05 05:42:28 +0000 |
---|---|---|
committer | Tomasz Kramkowski <tk@the-tk.com> | 2021-12-05 05:42:28 +0000 |
commit | ce5768a2f6e1d70545136d181a721ffe55508b12 (patch) | |
tree | 6e137fb98107ea7ad514f5b0d78a4f48e370d884 /5.py | |
parent | cdf7dacff15f62dde0766e4d189e65c4d55659c0 (diff) | |
download | aoc2021-ce5768a2f6e1d70545136d181a721ffe55508b12.tar.gz aoc2021-ce5768a2f6e1d70545136d181a721ffe55508b12.tar.xz aoc2021-ce5768a2f6e1d70545136d181a721ffe55508b12.zip |
day 5: first draft
Diffstat (limited to '5.py')
-rw-r--r-- | 5.py | 39 |
1 files changed, 39 insertions, 0 deletions
@@ -0,0 +1,39 @@ +from collections import defaultdict, namedtuple +from utils import open_day + +Pos = namedtuple('Pos', ['x', 'y']) + +vents = [ + tuple( + Pos(*map(int, h.split(','))) + for h in l.rstrip().split(' -> ') + ) + for l in open_day(5).read().rstrip().split('\n') +] + +def gen(frm, to): + if to < frm: + to = to - 1 + increment = -1 + else: + to = to + 1 + increment = 1 + for i in range(frm, to, increment): + yield i + +points = defaultdict(int) + +for vent in vents: + if vent[0].x == vent[1].x: + for y in gen(vent[0].y, vent[1].y): + points[(vent[0].x, y)] += 1 + elif vent[0].y == vent[1].y: + for x in gen(vent[0].x, vent[1].x): + points[(x, vent[0].y)] += 1 +print(sum(n >= 2 for n in points.values())) + +for vent in vents: + if vent[0].x != vent[1].x and vent[0].y != vent[1].y: + for x, y in zip(gen(vent[0].x, vent[1].x), gen(vent[0].y, vent[1].y)): + points[(x, y)] += 1 +print(sum(n >= 2 for n in points.values())) |