Browse Source

Day 5

master
Noëlle 2 years ago
parent
commit
5f4a7e01dd
No known key found for this signature in database
2 changed files with 120 additions and 0 deletions
  1. 55
    0
      aoc5-1.py
  2. 65
    0
      aoc5-2.py

+ 55
- 0
aoc5-1.py View File

@@ -0,0 +1,55 @@
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()

+ 65
- 0
aoc5-2.py View File

@@ -0,0 +1,65 @@
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
elif start_pos[1] == end_pos[1]:
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
else:
if start_pos[0] > end_pos[0]:
start_pos, end_pos = end_pos, start_pos
i, j = start_pos
end_i, end_j = end_pos[0], end_pos[1]
if j <= end_j: # there should be no cases where they're equal, but...
while i <= end_i and j <= end_j:
self.board[j][i] = self.board[j][i] + 1
i += 1
j += 1
else:
while i <= end_i and j >= end_j:
self.board[j][i] = self.board[j][i] + 1
i += 1
j -= 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]
# 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()

Loading…
Cancel
Save