r/adventofcode • • Dec 09 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 9 Solutions -🎄-

--- Day 9: Marble Mania ---


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 9

Transcript:

Studies show that AoC programmers write better code after being exposed to ___.


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:29:13!

22 Upvotes

283 comments sorted by

View all comments

Show parent comment

1

u/koordinate Dec 15 '18

I also observed that a home-brew linked list was quite slow, taking ~10 seconds.

class Node {
    var value: Int?
    weak var previous: Node?
    weak var next: Node?
}

func simulate(playerCount: Int, lastMarble: Int) -> Int {
    guard lastMarble > 0 else {
        return 0
    }

    var score = Array(repeating: 0, count: playerCount)
    var node: Node? = Node()
    node?.value = 0
    node?.previous = node
    node?.next = node
    // The Swift runtime (as of 4.2.1) crashes when deiniting long lists (really), so instead of
    // using next pointers, keep the nodes alive by keeping them in an array.
    var nodes = [node]
    var player = 0
    for marble in 1...lastMarble {
        if marble % 23 == 0 {
            for _ in 0..<7 {
                node = node?.previous
            }
            score[player] += marble + (node?.value ?? 0)
            node?.previous?.next = node?.next
            node?.next?.previous = node?.previous
            node = node?.next
        } else {
            node = node?.next
            let newNode = Node()
            newNode.value = marble
            newNode.next = node?.next
            newNode.previous = node
            node?.next = newNode
            newNode.next?.previous = newNode
            node = newNode
            nodes.append(newNode)
        }
        player = (player + 1) % playerCount
    }
    return score.max() ?? 0
}

while let line = readLine() {
    let fields = line.split(separator: " ")
    if fields.count >= 8, let playerCount = Int(fields[0]), let lastMarble = Int(fields[6]) {
        print(simulate(playerCount: playerCount, lastMarble: lastMarble))
        print(simulate(playerCount: playerCount, lastMarble: lastMarble * 100))
    }
}

Interestingly, while writing that I found what seems like a Swift bug. The following code crashes, at least on my machine:

class Node {
    var next: Node?
}

func makeList(n: Int) {
    let node = Node()
    for _ in 0...n {
        let newNode = Node()
        newNode.next = node.next
        node.next = newNode
    }
}

makeList(n: 100  * 100) // doesn't crash
makeList(n: 1000 * 100) // Illegal instruction: 4 

To speed up the linked lists, I found this comment useful. Here is a Swift implementation using the array-backed list discussed there:

func simulate(playerCount: Int, lastMarble: Int) -> Int {
    guard lastMarble > 0 else {
        return 0
    }

    var score = Array(repeating: 0, count: playerCount)
    var marbles = [0], next = [0], previous = [0]
    var i = 0
    for marble in 1...lastMarble {
        if marble % 23 == 0 {
            for _ in 0..<7 {
                i = previous[i]
            }
            score[(marble - 1) % playerCount] += marble + marbles[i]
            marbles[i] = -1
            next[previous[i]] = next[i]
            previous[next[i]] = previous[i]
            i = next[i]
        } else {
            i = next[i]
            marbles.append(marble)
            next.append(next[i])
            previous.append(i)
            previous[next[i]] = marbles.count - 1
            next[i] = marbles.count - 1
            i = marbles.count - 1
        }
    }
    return score.max() ?? 0
}

while let line = readLine() {
    let fields = line.split(separator: " ")
    if fields.count >= 8, let playerCount = Int(fields[0]), let lastMarble = Int(fields[6]) {
        print(simulate(playerCount: playerCount, lastMarble: lastMarble))
        print(simulate(playerCount: playerCount, lastMarble: lastMarble * 100))
    }
}

This takes 1.6 seconds (when interpreted. When compiled with optimisations, it takes 400 ms).