summaryrefslogtreecommitdiffstats
path: root/solutions/py/d12.py
blob: df4a54f73667e2a4db8f8f5cb007b7346e45544c (plain) (blame)
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import itertools
import primefac

class Moon(object):
    def __init__(self, x, y, z):
        self.x = x
        self.y = y
        self.z = z
        self.dx = 0
        self.dy = 0
        self.dz = 0

    def step(self):
        self.x += self.dx
        self.y += self.dy
        self.z += self.dz

    def gx(self, other):
        if self.x < other.x:
            self.dx += 1
            other.dx -= 1

    def gy(self, other):
        if self.y < other.y:
            self.dy += 1
            other.dy -= 1

    def gz(self, other):
        if self.z < other.z:
            self.dz += 1
            other.dz -= 1

    def gravity(self, other):
        self.gx(other)
        self.gy(other)
        self.gz(other)

    def get_x(self):
        return self.x, self.dx

    def get_y(self):
        return self.y, self.dy

    def get_z(self):
        return self.z, self.dz

    def get(self):
        return self.get_x, self.get_y, self.get_z

def pt1(input):
    moons = []

    for line in input:
        dims = line.rstrip().split(",")
        x = int(dims[0][3:])
        y = int(dims[1][3:])
        z = int(dims[2][3:-1])
        moons.append(Moon(x, y, z))

    for _ in range(1000):
        for pair in itertools.permutations(moons, 2):
            pair[0].gravity(pair[1])
        for moon in moons:
            moon.step()

    tot = 0
    for moon in moons:
        pot = abs(moon.x) + abs(moon.y) + abs(moon.z)
        kin = abs(moon.dx) + abs(moon.dy) + abs(moon.dz)
        tot += pot*kin
    return tot

def pt2(input):
    moons = []
    xs = 0
    ys = 0
    zs = 0

    for line in input:
        dims = line.rstrip().split(",")
        x = int(dims[0][3:])
        y = int(dims[1][3:])
        z = int(dims[2][3:-1])
        moons.append(Moon(x, y, z))

    # x
    states = {}
    i = -1
    while 1:
        i += 1
        if tuple(moon.get_x() for moon in moons) in states:
            xs = i
            break
        states[tuple(moon.get_x() for moon in moons)] = i
        for pair in itertools.permutations(moons, 2):
            pair[0].gx(pair[1])
        for moon in moons:
            moon.step()

    # y
    states = {}
    i = -1
    while 1:
        i += 1
        if tuple(moon.get_y() for moon in moons) in states:
            ys = i
            break
        states[tuple(moon.get_y() for moon in moons)] = i
        for pair in itertools.permutations(moons, 2):
            pair[0].gy(pair[1])
        for moon in moons:
            moon.step()

    # z
    states = {}
    i = -1
    while 1:
        i += 1
        if tuple(moon.get_z() for moon in moons) in states:
            zs = i
            break
        states[tuple(moon.get_z() for moon in moons)] = i
        for pair in itertools.permutations(moons, 2):
            pair[0].gz(pair[1])
        for moon in moons:
            moon.step()

    factors = [primefac.factorint(n) for n in (xs, ys, zs)]
    nums = {}
    for factor in factors:
        for n in factor:
            nums[n] = max(nums.get(n, 0), factor[n])
    ans = 1
    for n in nums:
        ans *= n ** nums[n]
    return ans

if __name__ == "__main__":
    import cProfile

    input = open("../input/12", "r").readlines()
    cProfile.run("pt1(input)")
    cProfile.run("pt2(input)")
    print(pt1(input))
    print(pt2(input))