summaryrefslogtreecommitdiff
path: root/2024/day14/solve.py
blob: acbd6af259ee9b6f29929233b990cb00a01451e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
input_file = "input"

def print_positions(robots, col_count, row_count, iter_count):
    print_data = {}
    for robot in robots:
        key = (robot[1], robot[0])
        if key not in print_data:
            print_data[key] = 0

        print_data[key] += 1

    longest_line_threshold = 10
    longest_line = 0
    print_this = False
    for row in range(row_count):
        for col in range(col_count):
            key = (row, col)
            if key in print_data:
                longest_line += 1
                if longest_line >= longest_line_threshold:
                    print_this = True
                    break
            else:
                longest_line = 0

        if print_this:
            break

    if print_this:
        print(f"Positions after {iter_count + 1:000} seconds")
        for row in range(row_count):
            for col in range(col_count):
                key = (row, col)
                if key in print_data:
                    print(print_data[key], end="")
                else:
                    print(".", end="")
            print()

        input()


def move_robots(robots: list[list[int]], col_count: int, row_count: int):
    for robot in robots:
        robot[0] += robot[2]
        robot[1] += robot[3]

        if robot[0] < 0:
            robot[0] += col_count
        elif robot[0] >= col_count:
            robot[0] -= col_count

        if robot[1] < 0:
            robot[1] += row_count
        elif robot[1] >= row_count:
            robot[1] -= row_count

data: list[list[int]] = []
with open(input_file) as f:
    for line in f:
        data.append([int(elem) for elem in line.strip().replace("p=", "").replace(" v=", ",").split(",")])

wideness = 101 if input_file == "input" else 11
height = 103 if input_file == "input" else 7

print_mode_on = True
# second part

range_max = 100 if not print_mode_on else 10000
for ii in range(range_max):
    move_robots(data, wideness, height)
    if print_mode_on:
        print_positions(data, wideness, height, ii)

middle_col = wideness // 2
middle_row = height // 2

quads = [[0, 0], [0, 0]]
for elem in data:
    if elem[0] == middle_col or elem[1] == middle_row:
        continue

    quads[0 if elem[0] < middle_col else 1][0 if elem[1] < middle_row else 1] += 1

result = quads[0][0] * quads[1][1] * quads[0][1] * quads[1][0]
print(result)