summaryrefslogtreecommitdiff
path: root/06/main.py
blob: cf9f85c19e5bff4c648e2414940010634ee18b12 (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
#!/usr/bin/python

import numpy as np



def solve_partn(partn, lines):
    # In part 1, we treat an int32 as a boolean: 1 = True, 0 = False
    grid = np.zeros((1000, 1000), dtype="int32")

    for line in lines:
        words = line.split()
        if words[0] == "turn":
            xi, yi = [int(n) for n in words[2].split(",")]
            xf, yf = [int(n) + 1 for n in words[4].split(",")]

            if partn == 1:
                if words[1] == "on":
                    grid[xi : xf, yi : yf] = 1
                elif words[1] == "off":
                    grid[xi : xf, yi : yf] = 0
            else: # partn == 2
                if words[1] == "on":
                    grid[xi : xf, yi : yf] += 1
                elif words[1] == "off":
                    grid[xi : xf, yi : yf] -= 1
                    grid = np.clip(grid, 0, 1000000)

        elif words[0] == "toggle":
            xi, yi = [int(n) for n in words[1].split(",")]
            xf, yf = [int(n) + 1 for n in words[3].split(",")]

            if partn == 1:
                grid[xi : xf, yi : yf] = 1 - grid[xi : xf, yi : yf] # NOT
            else: # partn == 2
                grid[xi : xf, yi : yf] += 2

    return np.sum(grid)



def main():
    # Read instructions from input text file
    with open("input.txt", "r") as f:
        lines = f.read().splitlines()

    print("Part 1 solution:", solve_partn(1, lines)) #   400410 for me
    print("Part 2 solution:", solve_partn(2, lines)) # 15343601 for me



if __name__ == "__main__":
    main()