From 489fa5d88ed59b800ec9f33f7a7174b95c2848f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Wed, 16 Dec 2020 18:46:43 +0100 Subject: 16: list comprehensions --- 20/py/d16.py | 18 ++---------------- 1 file changed, 2 insertions(+), 16 deletions(-) (limited to '20/py/d16.py') diff --git a/20/py/d16.py b/20/py/d16.py index e9f478b..f6b7f46 100644 --- a/20/py/d16.py +++ b/20/py/d16.py @@ -4,14 +4,7 @@ import sys def pt1(_in): - things = [] - for line in _in[:20]: - thing = [] - line = line.strip().split(": ") - for a in line[1].split(" or "): - b = a.split("-") - thing.append(range(int(b[0]), int(b[1])+1)) - things.append(thing) + constraints = [[range(int((b := a.split("-"))[0]), int(b[1])+1) for a in line.split(": ")[1].split(" or ")] for line in _in[:20]] res = 0 for line in _in[25:]: for n in line.strip().split(","): @@ -27,14 +20,7 @@ def pt1(_in): def pt2(_in): - constraints = [] - for line in _in[:20]: - thing = [] - line = line.strip().split(": ") - for a in line[1].split(" or "): - b = a.split("-") - thing.append(range(int(b[0]), int(b[1])+1)) - constraints.append(thing) + constraints = [[range(int((b := a.split("-"))[0]), int(b[1])+1) for a in line.split(": ")[1].split(" or ")] for line in _in[:20]] tickets = [] for line in _in[25:]: -- cgit v1.2.1