|
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- import pdb
-
- class FloorMap:
- def __init__(self, size_x, size_y):
- self.board = []
- for _ in range(size_y+1):
- self.board.append([0]*(size_x+1))
-
- def add_line(self, start_pos, end_pos):
- if start_pos[0] == end_pos[0]:
- if start_pos[1] > end_pos[1]:
- start_pos, end_pos = end_pos, start_pos
- for i in range(start_pos[1], end_pos[1]+1):
- self.board[i][start_pos[0]] = self.board[i][start_pos[0]] + 1
- else:
- if start_pos[0] > end_pos[0]:
- start_pos, end_pos = end_pos, start_pos
- for i in range(start_pos[0], end_pos[0]+1):
- self.board[start_pos[1]][i] = self.board[start_pos[1]][i] + 1
-
- def count_inters(self):
- return sum([len([el for el in line if el >= 2]) for line in self.board])
-
- def __repr__(self):
- outstr = ""
- for line_num in range(10):
- outstr += f"{' '.join(str(self.board[line_num]))}"
- return outstr
-
- def __str__(self):
- outstr = ""
- for line_num in range(10):
- outstr += f"{' '.join(str(self.board[line_num]))}"
- return outstr
-
-
- def main():
- with open("aoc5-1.txt", "r") as file:
- lines = [line.strip() for line in file.readlines()]
- # Each line is a pair of x,y coordinates...es separated by " -> "
- lines = [[list(map(int, el.split(","))) for el in line.split(" -> ")] for line in lines]
- # For 5-1, only care about the ones where, of x1,y1 -> x2,y2, x1 == x2 or y1 == y2
- # i.e. only horizontal or vertical
- lines = [line for line in lines if line[0][0] == line[1][0] or line[0][1] == line[1][1]]
- # god, this feels goofy
- size_x = max([max(line[0][0], line[1][0]) for line in lines])
- size_y = max([max(line[0][1], line[1][1]) for line in lines])
- board_map = FloorMap(size_x, size_y)
- for line in lines:
- board_map.add_line(*line)
- print(board_map.count_inters())
-
-
- if __name__ == "__main__":
- main()
|