Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- with open("2021_day13_input.txt", "r", encoding="utf-8") as ifile:
- dots, instructions = map(str.splitlines, ifile.read().split("\n\n"))
- dot_dict = dict(zip((dots := [(int((x := line.split(","))[0]), int(x[1])) for line in dots]), [True]*len(dots)))
- instructions = [((x := line.split()[2].split("="))[0], int(x[1])) for line in instructions]
- max_x, max_y = max([k[0] for k in dot_dict.keys()]), max([k[1] for k in dot_dict.keys()])
- def fold_vertically(after_line, dot_dict):
- for y_add in range(1, max_y-after_line+1):
- for x in range(max_x+1):
- if (x, after_line+y_add, ) in dot_dict:
- dot_dict.pop((x, after_line+y_add, ))
- dot_dict[(x, after_line-y_add, )] = True
- def fold_horizontally(after_column, dot_dict):
- for x_add in range(1, max_x-after_column+1):
- for y in range(max_y+1):
- if (after_column+x_add, y,) in dot_dict:
- dot_dict.pop((after_column+x_add, y,))
- dot_dict[(after_column-x_add, y,)] = True
- def print_doc_dict(dot_dict):
- _max_x, _max_y = max([k[0] for k in dot_dict.keys()]), max([k[1] for k in dot_dict.keys()])
- for y in range(_max_y+1):
- line = ""
- for x in range(max_x+1):
- if (x, y,) in dot_dict:
- line += "X"
- else:
- line += "."
- print(line)
- for pos, instruction in enumerate(instructions):
- match instruction[0]:
- case "x":
- fold_horizontally(instruction[1], dot_dict)
- case "y":
- fold_vertically(instruction[1], dot_dict)
- if pos == 0:
- print("Nr of dots after the first fold: {}".format(len(dot_dict)))
- print_doc_dict(dot_dict)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement