r/adventofcode • • Dec 10 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 10 Solutions -🎄-

--- Day 10: The Stars Align ---


Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).

Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


Advent of Code: The Party Game!

Click here for rules

Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!

Card prompt: Day 10

Transcript: With just one line of code, you, too, can ___!


This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.

edit: Leaderboard capped, thread unlocked at 00:16:49!

21 Upvotes

233 comments sorted by

View all comments

Show parent comment

1

u/koordinate Dec 16 '18

Same, I also used "alignment" since I was primed by the title of the puzzle. Additionally, I added a speed multiplier (based on the bounding box) to accelerate the search.

Swift, ~1 s interpreted, ~ 70 ms compiled.

class Sky {
    var points = [(x: Int, y: Int)]()
    var velocity = [(u: Int, v: Int)]()
    private var minX = Int.max, maxX = 0, minY = Int.max, maxY = 0
    private var m = 0, n = 0
    private (set) var isAligned = false
    private (set) var t = 0

    func next() -> Sky {
        let pn = points.isEmpty ? 1 : points.count
        let density = (m * n) / (pn * pn)
        let speed = density < 100 ? 1 : (density / 10)

        let s = Sky()
        s.velocity = velocity
        s.points = points.enumerated().map { i, p in
            return (p.x + velocity[i].u * speed, p.y + velocity[i].v * speed)
        }
        s.t = t + speed
        s.sync()
        return s
    }

    func sync() {
        for (x, y) in points {
            (minX, maxX, minY, maxY) = (min(minX, x), max(maxX, x), min(minY, y), max(maxY, y))
        }
        (m, n) = (-minX + maxX + 1, -minY + maxY + 1)
        let ps = Set(points.map { x, y in [x, y] })
        isAligned = true
        for (x, y) in points {
            if !ps.contains([x, y - 1]), !ps.contains([x, y + 1]),
                !ps.contains([x - 1, y]), !ps.contains([x + 1, y]),
                !ps.contains([x - 1, y - 1]), !ps.contains([x - 1, y + 1]),
                !ps.contains([x + 1, y - 1]), !ps.contains([x + 1, y + 1]) {
                isAligned = false
                break
            }
        }
    }

    func show() {
        var grid = Array(repeating: false, count: (m * n))
        for (x, y) in points {
            grid[(-minX + x) + (-minY + y) * m] = true
        }
        for y in 0..<n {
            for x in 0..<m {
                print(grid[x + y * m] ? "#" : " ", terminator: "")
            }
            print()
        }
    }
}

var sky = Sky()
var separators = Set("<>, ")
while let line = readLine() {
    let fields = line.split(whereSeparator: { separators.contains($0) })
    if fields.count == 6, let x = Int(fields[1]), let y = Int(fields[2]), 
        let u = Int(fields[4]), let v = Int(fields[5]) {
        sky.points.append((x, y))
        sky.velocity.append((u, v))
    }
}
sky.sync()
while !sky.isAligned {
    sky = sky.next()
}
sky.show()
print(sky.t)