b.py (1285B) [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 21 prev_folded = {} 22 for d, v in folds: 23 v = int(v) 24 folded = {} 25 for c in coords: 26 x, y = c 27 if d == "x": 28 if x == v: 29 # lost to the fold 30 pass 31 elif x > v: 32 newx = v - (x - v) 33 folded[(newx, y)] = 1 34 else: 35 folded[(x, y)] = 1 36 elif d == "y": 37 if y == v: 38 # lost to the fold 39 pass 40 elif y > v: 41 newy = v - (y - v) 42 folded[(x, newy)] = 1 43 else: 44 folded[(x, y)] = 1 45 else: 46 raise Exception(f"Bad dir: {d}") 47 48 coords = folded.keys() 49 prev_folded = folded 50 51 # Just hope they fit on a terminal! 52 print() 53 for y in range(6): 54 for x in range(120): 55 if (x, y) in prev_folded: 56 print("#", end="") 57 else: 58 print(" ", end="") 59 print()