[WIP] Solution to problem 5 part 2 in Python
This commit is contained in:
parent
53812f85a7
commit
b26e52c4ea
@ -59,7 +59,7 @@
|
||||
from collections import defaultdict
|
||||
from typing import DefaultDict, Tuple
|
||||
|
||||
with open("files/P5.txt") as f:
|
||||
with open("files/test.txt") as f:
|
||||
points = [
|
||||
points
|
||||
for line in f.read().strip().split("\n")
|
||||
@ -103,5 +103,62 @@ def part_1() -> None:
|
||||
print(sum(1 for key in grid.values() if key > 1))
|
||||
|
||||
|
||||
# --- Part Two ---
|
||||
|
||||
# Unfortunately, considering only horizontal and vertical lines doesn't give
|
||||
# you the full picture; you need to also consider diagonal lines.
|
||||
|
||||
# Because of the limits of the hydrothermal vent mapping system, the lines in
|
||||
# your list will only ever be horizontal, vertical, or a diagonal line at
|
||||
# exactly 45 degrees. In other words:
|
||||
|
||||
# An entry like 1,1 -> 3,3 covers points 1,1, 2,2, and 3,3.
|
||||
# An entry like 9,7 -> 7,9 covers points 9,7, 8,8, and 7,9.
|
||||
|
||||
# Considering all lines from the above example would now produce the following
|
||||
# diagram:
|
||||
|
||||
# 1.1....11.
|
||||
# .111...2..
|
||||
# ..2.1.111.
|
||||
# ...1.2.2..
|
||||
# .112313211
|
||||
# ...1.2....
|
||||
# ..1...1...
|
||||
# .1.....1..
|
||||
# 1.......1.
|
||||
# 222111....
|
||||
|
||||
# You still need to determine the number of points where at least two lines
|
||||
# overlap. In the above example, this is still anywhere in the diagram with a 2
|
||||
# or larger - now a total of 12 points.
|
||||
|
||||
# Consider all of the lines. At how many points do at least two lines overlap?
|
||||
|
||||
|
||||
def part_2() -> None:
|
||||
grid: DefaultDict[Tuple[int, int], int] = defaultdict(int)
|
||||
for idx in range(len(points) // 2):
|
||||
if x_1[idx] == x_2[idx]:
|
||||
start, stop, step = get_range(y_1[idx], y_2[idx])
|
||||
for pos in range(start, stop, step):
|
||||
grid[x_1[idx], pos] += 1
|
||||
elif y_1[idx] == y_2[idx]:
|
||||
start, stop, step = get_range(x_1[idx], x_2[idx])
|
||||
for pos in range(start, stop, step):
|
||||
grid[pos, y_1[idx]] += 1
|
||||
elif abs(x_1[idx] - x_2[idx]) == abs(y_1[idx] - y_2[idx]):
|
||||
if x_2[idx] > x_1[idx]:
|
||||
start_x, stop_x, step_x = get_range(x_1[idx], x_2[idx])
|
||||
start_y, stop_y, step_y = get_range(y_1[idx], y_2[idx])
|
||||
pos_x = [pos_x for pos_x in range(start_x, stop_x, step_x)]
|
||||
pos_y = [pos_y for pos_y in range(start_y, stop_y, step_y)]
|
||||
for px, py in zip(pos_x, pos_y):
|
||||
grid[px, py] += 1
|
||||
|
||||
print(sum(1 for key in grid.values() if key > 1))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
part_1()
|
||||
# part_1()
|
||||
part_2()
|
||||
|
Loading…
Reference in New Issue
Block a user