@@ -0,0 +1,1795 @@
+# Mathematical Thinking in Computer Science%0A# Week 2%0A#%0A# 16 diagonals.%0A%0Aimport enum%0Aimport sys%0A%0Afrom typing import List, Tuple%0A%0A%0ATOTAL_LINES = 16%0ASIZE = 5%0A%0A%0Aclass Square(enum.Enum):%0A  EMPTY = 0%0A  UP = 1%0A  DOWN = 2%0A%0A%0Adef is_valid(p_pos: Tuple%5Bint, int%5D, p_val: Square,%0A             new: Tuple%5Bint, int%5D, val: Square) -%3E bool:%0A  # print(p_pos, p_val, new, val)%0A  if p_val == Square.EMPTY or val == Square.EMPTY:%0A    return True%0A%0A  if p_pos%5B0%5D == new%5B0%5D or p_pos%5B1%5D == new%5B1%5D:%0A    return p_val == val%0A%0A  if p_pos%5B1%5D == new%5B1%5D - 1:%0A    return p_val == Square.UP or p_val != val%0A%0A  return p_val == Square.DOWN or p_val != val%0A%0A%0Adef print_grid(grid: List%5BList%5BSquare%5D%5D) -%3E None:%0A  for row in grid:%0A    for x in row:%0A      sys.stdout.write(('.', '/', '%5C%5C')%5Bx.value%5D)%0A    sys.stdout.write('%5Cn')%0A%0A%0Adef solve(grid: List%5BList%5BSquare%5D%5D, row: int, col: int, lines: int) -%3E None:%0A  # print(row, col, lines)%0A  if lines == TOTAL_LINES:%0A    print_grid(grid)%0A    sys.stdout.write('%5Cn')%0A    sys.stdin.readline()%0A    return%0A%0A  if row == len(grid):%0A    return%0A%0A  for x in reversed(Square):%0A%0A    valid = True%0A    for r, c in %5B(-1, -1), (-1, 0), (-1, 1), (0, -1)%5D:%0A      if not ((row + r %3C 0 or col + c %3C 0 or col + c %3E= len(grid%5B0%5D)) or %5C%0A          (0 %3C= row + r and 0 %3C= col + c %3C len(grid%5B0%5D) and %5C%0A          is_valid((row + r, col + c), grid%5Brow+r%5D%5Bcol+c%5D, (row, col), x))):%0A        valid = False%0A        break%0A    if not valid:%0A      continue%0A%0A    grid%5Brow%5D%5Bcol%5D = x%0A    solve(grid,%0A        row if col + 1 %3C len(grid%5B0%5D) else row + 1,%0A        col + 1 if col + 1 %3C len(grid%5B0%5D) else 0,%0A        lines + 1 if x != Square.EMPTY else lines)%0A    grid%5Brow%5D%5Bcol%5D = Square.EMPTY%0A%0A%0Adef main(argv):%0A  grid = %5B %5BSquare.EMPTY%5D * SIZE for _ in range(SIZE) %5D%0A  solve(grid, 0, 0, 0)%0A%0A%0Aif __name__ == '__main__':%0A  main(sys.argv)%0A

Implement 16-diagonals solution.