Esempio n. 1
0
def test_1b():
    bodies = [
        Body(Point3(-8,-10, 0)),
        Body(Point3( 5,  5,10)),
        Body(Point3( 2, -7, 3)),
        Body(Point3( 9, -8,-3)),
    ]

    last_state = last(simulate(bodies, steps_limit=100))

    assert last_state == (
        (Point3(  8, -12, -9), Vector3(-7,   3,  0)),
        (Point3( 13,  16, -3), Vector3( 3, -11, -5)),
        (Point3(-29, -11, -1), Vector3(-3,   7,  4)),
        (Point3( 16, -13, 23), Vector3( 7,   1,  1)),
    )
    assert sum(b.energy for b in bodies) == 1940
    print("passed test_1b")
Esempio n. 2
0
    def place_scanner(self, reading: Reading, min_overlaps: int = 12) -> Scanner | None:
        if not self.fixed_readings:
            # first reading -> trivial
            first_scanner = Scanner(Vector3(0, 0, 0))
            self.fixed_readings[first_scanner] = reading
            return first_scanner

        # try matching the reading to match with any of a fixed reading
        for fixed_reading in self.fixed_readings.values():
            if match := fixed_reading.match(reading, min_overlaps):
                rotation, vector, reading_translated = match
                scanner = Scanner(vector, rotation)
                self.fixed_readings[scanner] = reading_translated
                return scanner
Esempio n. 3
0
def report_from_lines(lines: Iterable[str]) -> Iterable[Reading]:
    beacons_buffer: list[Vector3] = []

    for line in lines:
        line = line.strip()
        if not line:
            continue

        if line.startswith('---'):
            if beacons_buffer:
                yield Reading(beacons_buffer)
                beacons_buffer.clear()
        else:
            x, y, z = line.split(',')
            beacons_buffer.append(Vector3(int(x), int(y), int(z)))

    if beacons_buffer:
        yield Reading(beacons_buffer)
Esempio n. 4
0
 def from_str(cls, line: str) -> 'Particle':
     # 'p=<-201,-1266,-2683>, v=<-29,-181,-382>, a=<2,13,31>'
     vals = [int(val) for val in parse_line(line.strip(), "p=<$,$,$>, v=<$,$,$>, a=<$,$,$>")]
     assert len(vals) == 9
     return cls(pos=Point3(*vals[:3]), vel=Vector3(*vals[3:6]), acc=Vector3(*vals[6:]))
Esempio n. 5
0
 def only_z(self):
     return Body(Point3(0, 0, self.pos.z), Vector3(0, 0, self.vel.z))
Esempio n. 6
0
 def only_y(self):
     return Body(Point3(0, self.pos.y, 0), Vector3(0, self.vel.y, 0))
Esempio n. 7
0
 def only_x(self):
     return Body(Point3(self.pos.x, 0, 0), Vector3(self.vel.x, 0, 0))
Esempio n. 8
0
 def __init__(self, pos: Point3, vel: Vector3 = None):
     self.pos = pos
     self.vel = vel if vel is not None else Vector3.null()
Esempio n. 9
0
def test_1a():
    bodies = [
        Body(Point3(-1, 0, 2)),
        Body(Point3(2, -10, -7)),
        Body(Point3(4, -8, 8)),
        Body(Point3(3, 5, -1))
    ]
    r = list(simulate(bodies, steps_limit=10))
    assert len(r) == 11
    assert r[1] == (
        (Point3( 2, -1,  1), Vector3( 3, -1, -1)),
        (Point3( 3, -7, -4), Vector3( 1,  3,  3)),
        (Point3( 1, -7,  5), Vector3(-3,  1, -3)),
        (Point3( 2,  2,  0), Vector3(-1, -3,  1)),
    )
    assert r[2] == (
        (Point3( 5, -3, -1), Vector3( 3, -2, -2)),
        (Point3( 1, -2,  2), Vector3(-2,  5,  6)),
        (Point3( 1, -4, -1), Vector3( 0,  3, -6)),
        (Point3( 1, -4,  2), Vector3(-1, -6,  2)),
    )
    assert r[3] == (
        (Point3( 5, -6, -1), Vector3( 0, -3,  0)),
        (Point3( 0,  0,  6), Vector3(-1,  2,  4)),
        (Point3( 2,  1, -5), Vector3( 1,  5, -4)),
        (Point3( 1, -8,  2), Vector3( 0, -4,  0)),
    )
    assert r[4] == (
        (Point3( 2, -8,  0), Vector3(-3, -2,  1)),
        (Point3( 2,  1,  7), Vector3( 2,  1,  1)),
        (Point3( 2,  3, -6), Vector3( 0,  2, -1)),
        (Point3( 2, -9,  1), Vector3( 1, -1, -1)),
    )
    assert r[5] == (
        (Point3(-1, -9,  2), Vector3(-3, -1,  2)),
        (Point3( 4,  1,  5), Vector3( 2,  0, -2)),
        (Point3( 2,  2, -4), Vector3( 0, -1,  2)),
        (Point3( 3, -7, -1), Vector3( 1,  2, -2)),
    )
    assert r[6] == (
        (Point3(-1, -7,  3), Vector3( 0,  2,  1)),
        (Point3( 3,  0,  0), Vector3(-1, -1, -5)),
        (Point3( 3, -2,  1), Vector3( 1, -4,  5)),
        (Point3( 3, -4, -2), Vector3( 0,  3, -1)),
    )
    assert r[7] == (
        (Point3( 2, -2,  1), Vector3( 3,  5, -2)),
        (Point3( 1, -4, -4), Vector3(-2, -4, -4)),
        (Point3( 3, -7,  5), Vector3( 0, -5,  4)),
        (Point3( 2,  0,  0), Vector3(-1,  4,  2)),
    )
    assert r[8] == (
        (Point3( 5,  2, -2), Vector3( 3,  4, -3)),
        (Point3( 2, -7, -5), Vector3( 1, -3, -1)),
        (Point3( 0, -9,  6), Vector3(-3, -2,  1)),
        (Point3( 1,  1,  3), Vector3(-1,  1,  3)),
    )
    assert r[9] == (
        (Point3( 5,  3, -4), Vector3( 0,  1, -2)),
        (Point3( 2, -9, -3), Vector3( 0, -2,  2)),
        (Point3( 0, -8,  4), Vector3( 0,  1, -2)),
        (Point3( 1,  1,  5), Vector3( 0,  0,  2)),
    )
    assert r[10] == (
        (Point3( 2,  1, -3), Vector3(-3, -2,  1)),
        (Point3( 1, -8,  0), Vector3(-1,  1,  3)),
        (Point3( 3, -6,  1), Vector3( 3,  2, -3)),
        (Point3( 2,  0,  4), Vector3( 1, -1, -1)),
    )

    assert sum(b.energy for b in bodies) == 179

    print("passed test_1a")
Esempio n. 10
0
def sgn_v3(v3: Vector3):
    return Vector3(*(sgn(n) for n in v3))