2017 - Day 20 (Particle Swarm)

View puzzle description on AdventOfCode.com
Part 1
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
#!/usr/bin/env python3

#Advent of Code
#2017 Day 20, Part 1
#Solution by James C. (https://github.com/JamesMCo)

import os, sys
sys.path.append(os.path.abspath("../.."))
import unittest, util.read
from util.tests import run

def solve(puzzle_input):
    accs = []
    for particle in puzzle_input:
        accs.append(sum(abs(int(x)) for x in particle.split("<")[3].split(">")[0].split(",")))

    return accs.index(min(accs))

def main():
    puzzle_input = util.read.as_lines()

    particle = solve(puzzle_input)

    print("The particle that stays closest to <0,0,0> is " + str(particle) + ".")

class AOC_Tests(unittest.TestCase):
    def test_ex1(self):
        self.assertEqual(solve(["p=< 3,0,0>, v=< 2,0,0>, a=<-1,0,0>",
                                "p=< 4,0,0>, v=< 0,0,0>, a=<-2,0,0>"]), 0)

if __name__ == "__main__":
    run(main)
Part 2
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
#!/usr/bin/env python3

#Advent of Code
#2017 Day 20, Part 2
#Solution by James C. (https://github.com/JamesMCo)

import os, sys
sys.path.append(os.path.abspath("../.."))
import unittest, util.read
from util.tests import run

import collections

def solve(puzzle_input):
    Position     = collections.namedtuple("Position",     ["x", "y", "z"])
    Velocity     = collections.namedtuple("Velocity",     ["x", "y", "z"])
    Acceleration = collections.namedtuple("Acceleration", ["x", "y", "z"])

    particles = []

    positions = {}
    nochange = 0

    class Particle:
        def __init__(self, position, velocity, acceleration):            
            self.position     = Position(*position)
            self.velocity     = Velocity(*velocity)
            self.acceleration = Acceleration(*acceleration)

            particles.append(self)

        def step(self):
            self.velocity = Velocity(self.velocity.x + self.acceleration.x,
                                     self.velocity.y + self.acceleration.y,
                                     self.velocity.z + self.acceleration.z)
            self.position = Position(self.position.x + self.velocity.x,
                                     self.position.y + self.velocity.y,
                                     self.position.z + self.velocity.z)
            if p.position in positions:
                positions[p.position] += 1
            else:
                positions[p.position]  = 1

    furthest = 0
    for p in puzzle_input:
        pos = [int(x) for x in p.split("<")[1].split(">")[0].split(",")]
        vel = [int(x) for x in p.split("<")[2].split(">")[0].split(",")]
        acc = [int(x) for x in p.split("<")[3].split(">")[0].split(",")]
        Particle(pos, vel, acc)

        if max(abs(x) for x in pos) > furthest:
            furthest = max(abs(x) for x in pos)

    max_wait = (furthest**2 + furthest**2 + furthest**2)**0.5
    while nochange <= max_wait:
        count = len(particles)
        positions = {}

        for p in particles:
            p.step()

        new_particles = []
        for p in particles:
            if positions[p.position] == 1:
                new_particles.append(p)
        particles = new_particles

        if len(particles) == count:
            nochange += 1
        else:
            nochange  = 0

    return count

def main():
    puzzle_input = util.read.as_lines()

    count = solve(puzzle_input)

    print("The number of particles that remain after all collisions is " + str(count) + ".")

class AOC_Tests(unittest.TestCase):
    def test_ex1(self):
        self.assertEqual(solve(["p=<-6,0,0>, v=< 3,0,0>, a=< 0,0,0>",
                                "p=<-4,0,0>, v=< 2,0,0>, a=< 0,0,0>",
                                "p=<-2,0,0>, v=< 1,0,0>, a=< 0,0,0>",
                                "p=< 3,0,0>, v=<-1,0,0>, a=< 0,0,0>"]), 1)

if __name__ == "__main__":
    run(main)
Return to solution index