Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

2 роки тому
2 роки тому
2 роки тому
2 роки тому
2 роки тому
2 роки тому
2 роки тому
2 роки тому
2 роки тому
2 роки тому
2 роки тому
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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]: # Vertical line: X stays constant, Y changes
  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]: # Horizontal line: Y stays constant, X changes
  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: # Diagonal line: X and Y both change
  18. # These are only ever 45° angles, so each changes by 1 per iteration.
  19. if start_pos[0] > end_pos[0]:
  20. start_pos, end_pos = end_pos, start_pos
  21. i, j = start_pos
  22. end_i, end_j = end_pos[0], end_pos[1]
  23. # I've arranged this so X always increases. Y can either increase or decrease.
  24. # I don't like that so much repeats.
  25. # What I WANT to do here is be able to set a toggle variable so that
  26. # if Y decreases, the second condition flips from <= to >=,
  27. # and then j += dir, which is either 1 or -1.
  28. # But I can't find a way to do it without eval() bullshit.
  29. if j <= end_j: # there should be no cases where they're equal, but...
  30. while i <= end_i and j <= end_j:
  31. self.board[j][i] = self.board[j][i] + 1
  32. i += 1
  33. j += 1
  34. else:
  35. while i <= end_i and j >= end_j:
  36. self.board[j][i] = self.board[j][i] + 1
  37. i += 1
  38. j -= 1
  39. def count_inters(self):
  40. return sum([len([el for el in line if el >= 2]) for line in self.board])
  41. def __repr__(self):
  42. outstr = ""
  43. for line_num in range(10):
  44. outstr += f"{' '.join(str(self.board[line_num]))}"
  45. return outstr
  46. def __str__(self):
  47. outstr = ""
  48. for line_num in range(10):
  49. outstr += f"{' '.join(str(self.board[line_num]))}"
  50. return outstr
  51. def main():
  52. with open("aoc5-1.txt", "r") as file:
  53. lines = [line.strip() for line in file.readlines()]
  54. # Each line is a pair of x,y coordinates...es separated by " -> "
  55. lines = [[list(map(int, el.split(","))) for el in line.split(" -> ")] for line in lines]
  56. # god, this feels goofy, but it works
  57. size_x = max([max(line[0][0], line[1][0]) for line in lines])
  58. size_y = max([max(line[0][1], line[1][1]) for line in lines])
  59. board_map = FloorMap(size_x, size_y)
  60. for line in lines:
  61. board_map.add_line(*line)
  62. print(board_map.count_inters())
  63. if __name__ == "__main__":
  64. main()