adventofcode-2021/day05/day05.py

68 lines
1.8 KiB
Python
Raw Normal View History

2021-12-05 07:02:18 +00:00
def ingest(input):
2021-12-06 19:22:55 +00:00
"""Parse the input data and load it into a working datastructure to solve
the problem"""
2021-12-05 07:02:18 +00:00
paths = []
for line in input:
print(line)
paths.append([[int(x) for x in point.split(",")] for point in
line.split(" -> ")])
return paths
2021-12-06 19:22:55 +00:00
def build_chart(paths):
"""Create an empty table using the largest x and y values"""
size_x = max([x[0][0] for x in paths] + [x[1][0] for x in paths])
size_y = max([y[0][1] for y in paths] + [y[1][1] for y in paths])
2021-12-12 05:20:04 +00:00
chart = [[0 for x in range(size_x + 1)] for y in range(size_y + 1)]
return chart
def graph_line(chart, line):
print("Line:", line)
line.sort()
if line[0][0] != line[1][0] and line[0][1] != line[1][1]:
return chart
# List out the x and y values that will need a marking
x = [x for x in range(line[0][0], line[1][0] + 1)]
y = [y for y in range(line[0][1], line[1][1] + 1)]
# Zip the two lists together
print("X:", x, "Y:", y)
if len(x) >= len(y):
xy = [(x, y[i % len(y)]) for i, x in enumerate(x)]
else:
xy = [(x[i % len(x)], y) for i, y in enumerate(y)]
print(xy)
for x, y in xy:
chart[y][x] = chart[y][x] + 1
2021-12-06 19:22:55 +00:00
return chart
2021-12-05 07:02:18 +00:00
def part1():
2021-12-06 19:22:55 +00:00
"""Solution for part one of day05"""
2021-12-05 07:02:18 +00:00
with open("day05.in", "r") as f:
paths = ingest([line.rstrip() for line in f])
2021-12-06 19:22:55 +00:00
chart = build_chart(paths)
2021-12-12 05:20:04 +00:00
for path in paths:
chart = graph_line(chart, path)
for line in chart:
for point in line:
if point != 0:
print(point, end="")
else:
print(".", end="")
print()
count = 0
for line in chart:
for pt in line:
count += 1 if pt >= 2 else 0
print(count)
2021-12-05 07:02:18 +00:00
def main():
print("=== PART 1 ===")
part1()
if __name__ == "__main__":
main()