a.py (1066B) [raw]
1 #!/usr/bin/env python3 2 import sys 3 import re 4 5 coord = re.compile("\d+,\d+") 6 fold = re.compile("fold along") 7 8 coords = [] 9 folds = [] 10 11 for l in sys.stdin: 12 l = l.strip() 13 if coord.match(l): 14 coords.append([int(x) for x in l.split(",")]) 15 elif fold.match(l): 16 split = l.split() 17 # x=8 18 folds.append(split[2].split("=")) 19 20 # Pt A we only care about the first split 21 # Assuming all points are originally unique, we just need to 22 # dedupe them after the fold! 23 d, v = folds[0] 24 v = int(v) 25 26 folded = {} 27 28 for c in coords: 29 x, y = c 30 if d == "x": 31 if x == v: 32 # lost to the fold 33 pass 34 elif x > v: 35 newx = v - (x - v) 36 folded[(newx, y)] = 1 37 else: 38 folded[(x, y)] = 1 39 elif d == "y": 40 if y == v: 41 # lost to the fold 42 pass 43 elif y > v: 44 newy = v - (y - v) 45 folded[(x, newy)] = 1 46 else: 47 folded[(x, y)] = 1 48 else: 49 raise Exception(f"Bad dir: {d}") 50 51 print(len(folded.keys()))