Skip to content

Instantly share code, notes, and snippets.

@albertein
Created December 14, 2021 00:22
Show Gist options
  • Save albertein/939b7e94d1b8708d434710c532faa4eb to your computer and use it in GitHub Desktop.
Save albertein/939b7e94d1b8708d434710c532faa4eb to your computer and use it in GitHub Desktop.
def key(x, y):
return "{0}-{1}".format(x, y)
def fold(points, axis, fold_point):
fold_changes = []
for point_key in points:
x, y = points[point_key]
if axis == 'y':
if y > fold_point:
fold_changes.append((x, y, x, fold_point - (y - fold_point)))
else:
if x > fold_point:
fold_changes.append((x, y, fold_point - (x - fold_point), y))
for old_x, old_y, new_x, new_y in fold_changes:
points.pop(key(old_x, old_y), None)
points[key(new_x, new_y)] = (new_x, new_y)
def pretty_print(points):
point_list = sorted(points.values(), key = lambda item: (item[1], item[0]))
last_y = -1
idx = 0
line = ''
last_x = 0
while idx < len(point_list):
x, y = point_list[idx]
if y > last_y:
# New line
last_x = 0
print(line)
line = ''
line += ' ' * (x - last_x - 1)
line += '#'
last_x = x
last_y = y
idx += 1
print(line)
if __name__ == '__main__':
with open('input.txt') as data:
points = {}
reading_folds = False
for line in data:
line = line.strip()
if not line:
reading_folds = True
continue
if not reading_folds:
x, y = line.split(',')
points[key(x, y)] = (int(x), int(y))
else:
axis, value = line.split(' ')[2].split('=')
fold(points, axis, int(value))
print(len(points))
pretty_print(points)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment