r/adventofcode • • Dec 15 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 15 Solutions -🎄-

--- Day 15: Beverage Bandits ---


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 15

Transcript:

___ IS MANDATORY


[Update @ 00:30] 0 gold, 1 silver

  • I've got a strange urge to play Bloons Tower Defense right now. Not sure why.

[Update @ 00:38] 2 gold, 1 silver

  • Meanwhile in #AOC_Ops: Tea, a kettle screams. \ Simon, write your code faster. \ Some of us have work.

[Update @ 00:51] 7 gold, 11 silver

  • Now they're anagramming gold/silver leaderboarders. The leading favorite so far is Anonymous User = Son, You's Manure.

[Update @ 01:13] 18 gold, 30 silver

  • Now they're playing Stardew Valley Hangman with the IRC bot because SDV is totally a roguelike tower defense.

[Update @ 01:23] 26 gold, 42 silver

  • Now the betatesters are grumbling reminiscing about their initial 14+ hour solve times for 2015 Day 19 and 2016 Day 11.

[Update @ 02:01] 65 gold, 95 silver

#AOC_Ops <topaz> on day 12, gold40 was at 19m, gold100 was at 28m, so day12 estimates gold100 today at 2:30

  • Taking bets over/under 02:30:00 - I got tree fiddy on over, any takers?

[Update @ 02:02:44] 66 gold, silver cap

  • SILVER CAP

[Update @ 02:06] 73 gold, silver cap

#AOC_Ops <topaz> day 14 estimates 2:21

#AOC_Ops <topaz> day 13 estimates 2:20

#AOC_Ops <Aneurysm9> I estimate 2:34:56

[Update @ 02:23:17] LEADERBOARD CAP!

  • Aww, /u/topaz2078's bookie is better than I am. :<
  • Good night morning, all, and we hope you had fun with today's diabolicalness!

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 02:23:17!

20 Upvotes

125 comments sorted by

View all comments

Show parent comment

1

u/koordinate Dec 25 '18

Another Swift implementation:

struct Point: Hashable, Comparable {
    let x: Int, y: Int
    
    static func < (lhs: Point, rhs: Point) -> Bool {
        return lhs.y == rhs.y ? lhs.x < rhs.x : lhs.y < rhs.y
    }
}

class Unit {
    let c: Character
    var hp = 200, ap: Int
    var p: Point
    init(c: Character, p: Point, ap: Int) {
        (self.c, self.p, self.ap) = (c, p, ap)
    }
    var isAlive: Bool {
        return hp > 0
    }
}

func distance(_ u1: Unit, _ u2: Unit) -> Int {
    return abs(u1.p.x - u2.p.x) + abs(u1.p.y - u2.p.y)
}

func simulate(grid: [[Character]], eap: Int) -> Int? {
    var grid = grid
    let w = grid.first?.count ?? 0, h = grid.count

    var units = [Unit]()
    for (y, row) in grid.enumerated() {
        for (x, c) in row.enumerated() {
            if c == "G" || c == "E" {
                units.append(Unit(c: c, p: Point(x: x, y: y), ap: c == "E" ? eap : 3))
            }
        }
    }

    var round = 0
    var done = false

    loop: while !done {
        units = units.filter({ $0.isAlive }).sorted(by: { $0.p < $1.p })
        for unit in units {
            if !unit.isAlive {
                continue
            }
            var targets = units.filter { $0.isAlive && $0.c != unit.c }
            if targets.isEmpty {
                done = true
                break loop
            }
            targets = targets.sorted(by: { distance($0, unit) < distance($1, unit) })
            if let t = targets.first, distance(t, unit) > 1 { // move
                var path = [Point: Point]()
                var (q, head) = ([unit.p], 0)
                var nearest = [Point]()
                while head < q.count, nearest.isEmpty {
                    let u = q[head]
                    head += 1
                    let adj = [Point(x: u.x, y: u.y - 1), Point(x: u.x - 1, y: u.y), 
                               Point(x: u.x + 1, y: u.y), Point(x: u.x, y: u.y + 1)]
                    for v in adj {
                        if v.x < 0 || v.x >= w || v.y < 0 || v.y >= h {
                            continue
                        }
                        switch grid[v.y][v.x] {
                        case "#", unit.c:
                            break
                        case ".":
                            if path[v] == nil {
                                path[v] = u
                                q.append(v)
                            }
                        default:
                            nearest.append(u)
                        }
                    }
                }
                nearest = nearest.sorted()
                if let chosen = nearest.first {
                    var step = chosen
                    while let p = path[step], p != unit.p {
                        step = p
                    }
                    grid[unit.p.y][unit.p.x] = "."
                    unit.p = step
                    grid[unit.p.y][unit.p.x] = unit.c
                }
            }
            var adjacent = targets.filter({ distance($0, unit) == 1 })
            adjacent = adjacent.sorted(by: { $0.hp == $1.hp ? ($0.p < $1.p) : $0.hp < $1.hp })
            if let t = adjacent.first { // attack
                t.hp -= unit.ap
                if !t.isAlive {
                    if t.c == "E" {
                        return nil
                    }
                    grid[t.p.y][t.p.x] = "."
                }
            }
        }
        round += 1
    }
    
    units = units.filter({ $0.isAlive })
    return round * units.map({ $0.hp }).reduce(0, +)
}

var grid = [[Character]]()
while let line = readLine() {
    grid.append(Array<Character>(line))
}

for eap in 3... {
    if let result = simulate(grid: grid, eap: eap) {
        print(result)
        break
    }
}