2016-12-08 - Two-Factor Authentication
(original .ipynb)def rotate_column(c, by, grid): prev_column = [line[c] for line in grid] rows = len(grid) for r, col in enumerate(prev_column): grid[(r+by)%rows][c] = col def rotate_row(r, by, grid): prev_row = [c for c in grid[r]] cols = len(prev_row) for c, col in enumerate(prev_row): grid[r][(c+by)%cols] = prev_row[c] def rect(width, height, grid): for r in range(height): for c in range(width): grid[r][c] = "#" def parse_and_execute(line, grid): if line[:4] == "rect": _, size = line[4:].split(" ") width, height = size.split("x") rect(int(width), int(height), grid) return True elif line[:10] == "rotate row": r, by = line[13:].split(" by ") rotate_row(int(r), int(by), grid) return True elif line[:10] == "rotate col": c, by = line[16:].split(" by ") rotate_column(int(c), int(by), grid) return True return False
puzzle_input_str = open("puzzle_input/day8.txt").read() test_input_str = """rect 3x2 rotate column x=1 by 1 rotate row y=0 by 4 rotate column x=1 by 1""" rows = 6 cols = 50 def execute_draw_instructions(instructions): grid = [] for line in range(rows): grid.append([" "] * cols) for l, line in enumerate(instructions): if not parse_and_execute(line, grid): raise Exception(f"weird input at line {l}: {line}") return grid def part_one(input_str): grid = execute_draw_instructions(input_str.split("\n")) return sum( sum(1 for c in row if c == "#") for row in grid ) assert 6 == part_one(test_input_str) print("part one:", part_one(puzzle_input_str))
part one: 106
def part_two(input_str): grid = execute_draw_instructions(input_str.split("\n")) for row in grid: yield "".join(row) print("part two:\n\n" + "\n".join(part_two(puzzle_input_str)))
part two: ## #### # #### # ## # ##### ## ### # # # # # # # # # ## # # # # ### # ### # # # # # ### # # # # # # # # # # # # ## # # # # # # # # # # # # # ## # #### #### #### ## # # ## ###