You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

aoc5-2.py 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. class FloorMap:
  2. def __init__(self, size_x, size_y):
  3. self.board = []
  4. for _ in range(size_y+1):
  5. self.board.append([0]*(size_x+1))
  6. def add_line(self, start_pos, end_pos):
  7. if start_pos[0] == end_pos[0]:
  8. if start_pos[1] > end_pos[1]:
  9. start_pos, end_pos = end_pos, start_pos
  10. for i in range(start_pos[1], end_pos[1]+1):
  11. self.board[i][start_pos[0]] = self.board[i][start_pos[0]] + 1
  12. elif start_pos[1] == end_pos[1]:
  13. if start_pos[0] > end_pos[0]:
  14. start_pos, end_pos = end_pos, start_pos
  15. for i in range(start_pos[0], end_pos[0]+1):
  16. self.board[start_pos[1]][i] = self.board[start_pos[1]][i] + 1
  17. else:
  18. if start_pos[0] > end_pos[0]:
  19. start_pos, end_pos = end_pos, start_pos
  20. i, j = start_pos
  21. end_i, end_j = end_pos[0], end_pos[1]
  22. if j <= end_j: # there should be no cases where they're equal, but...
  23. while i <= end_i and j <= end_j:
  24. self.board[j][i] = self.board[j][i] + 1
  25. i += 1
  26. j += 1
  27. else:
  28. while i <= end_i and j >= end_j:
  29. self.board[j][i] = self.board[j][i] + 1
  30. i += 1
  31. j -= 1
  32. def count_inters(self):
  33. return sum([len([el for el in line if el >= 2]) for line in self.board])
  34. def __repr__(self):
  35. outstr = ""
  36. for line_num in range(10):
  37. outstr += f"{' '.join(str(self.board[line_num]))}"
  38. return outstr
  39. def __str__(self):
  40. outstr = ""
  41. for line_num in range(10):
  42. outstr += f"{' '.join(str(self.board[line_num]))}"
  43. return outstr
  44. def main():
  45. with open("aoc5-1.txt", "r") as file:
  46. lines = [line.strip() for line in file.readlines()]
  47. # Each line is a pair of x,y coordinates...es separated by " -> "
  48. lines = [[list(map(int, el.split(","))) for el in line.split(" -> ")] for line in lines]
  49. # god, this feels goofy
  50. size_x = max([max(line[0][0], line[1][0]) for line in lines])
  51. size_y = max([max(line[0][1], line[1][1]) for line in lines])
  52. board_map = FloorMap(size_x, size_y)
  53. for line in lines:
  54. board_map.add_line(*line)
  55. print(board_map.count_inters())
  56. if __name__ == "__main__":
  57. main()