#!/usr/bin/env python3 from pathlib import Path def fold(i, dots): new_dots = set() for d in dots: if i[0] == 'x': n = (i[1] - abs(d[0] - i[1]), d[1]) if d[0] != i[1]: new_dots.add(n) elif i[0] == 'y': n = (d[0], i[1] - abs(d[1] - i[1])) if d[1] != i[1]: new_dots.add(n) return new_dots def part_1(input): result = 0 dots = set() instructions = [] for line in input: if ',' in line: x, y = line.strip().split(',') dots.add((int(x), int(y))) elif 'fold along' in line: d, c = line.strip().split()[-1].split('=') instructions.append([d, int(c)]) dots = fold(instructions.pop(0), dots) result = len(dots) print("Part 1 result:", result) def part_2(input): result = 0 dots = set() instructions = [] for line in input: if ',' in line: x, y = line.strip().split(',') dots.add((int(x), int(y))) elif 'fold along' in line: d, c = line.strip().split()[-1].split('=') instructions.append([d, int(c)]) while len(instructions): dots = fold(instructions.pop(0), dots) max_x = 0 max_y = 0 for d in dots: max_x = max(max_x, d[0] + 1) max_y = max(max_y, d[1] + 1) print("Part 2 result:") for y in range(max_y): for x in range(max_x): if (x, y) in dots: print('█', end='') else: print('░', end='') print() input = list() p = Path(__file__).with_name('input.txt') with open(p) as f: input = f.readlines() part_1(input) part_2(input)