r/adventofcode • • Dec 16 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 16 Solutions -🎄-

--- Day 16: Chronal Classification ---


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 16

Transcript:

The secret technique to beat today's puzzles is ___.


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:39:03!

16 Upvotes

139 comments sorted by

View all comments

Show parent comment

1

u/koordinate Dec 25 '18

Another Swift solution:

func addr(rx: inout [Int], a: Int, b: Int, c: Int) {
    rx[c] = rx[a] + rx[b]
}

func addi(rx: inout [Int], a: Int, b: Int, c: Int) {
    rx[c] = rx[a] + b
}

func mulr(rx: inout [Int], a: Int, b: Int, c: Int) {
    rx[c] = rx[a] * rx[b]
}

func muli(rx: inout [Int], a: Int, b: Int, c: Int) {
    rx[c] = rx[a] * b
}

func banr(rx: inout [Int], a: Int, b: Int, c: Int) {
    rx[c] = rx[a] & rx[b]
}

func bani(rx: inout [Int], a: Int, b: Int, c: Int) {
    rx[c] = rx[a] & b
}

func borr(rx: inout [Int], a: Int, b: Int, c: Int) {
    rx[c] = rx[a] | rx[b]
}

func bori(rx: inout [Int], a: Int, b: Int, c: Int) {
    rx[c] = rx[a] | b
}

func setr(rx: inout [Int], a: Int, b: Int, c: Int) {
    rx[c] = rx[a]
}

func seti(rx: inout [Int], a: Int, b: Int, c: Int) {
    rx[c] = a
}

func gtir(rx: inout [Int], a: Int, b: Int, c: Int) {
    rx[c] = a > rx[b] ? 1 : 0 
}

func gtri(rx: inout [Int], a: Int, b: Int, c: Int) {
    rx[c] = rx[a] > b ? 1 : 0 
}

func gtrr(rx: inout [Int], a: Int, b: Int, c: Int) {
    rx[c] = rx[a] > rx[b] ? 1 : 0 
}

func eqir(rx: inout [Int], a: Int, b: Int, c: Int) {
    rx[c] = a == rx[b] ? 1 : 0 
}

func eqri(rx: inout [Int], a: Int, b: Int, c: Int) {
    rx[c] = rx[a] == b ? 1 : 0 
}

func eqrr(rx: inout [Int], a: Int, b: Int, c: Int) {
    rx[c] = rx[a] == rx[b] ? 1 : 0 
}

let ops = [addi, addr, mulr, muli, banr, bani, borr, bori, setr, seti,
           gtir, gtri, gtrr, eqir, eqri, eqrr]

typealias Sample = (before: [Int], instruction: [Int], after: [Int])

func matchingOpIndices(sample: Sample) -> [Int] {
    let ins = sample.instruction
    let (before, a, b, c, after) = (sample.before, ins[1], ins[2], ins[3], sample.after)
    return ops.enumerated().filter({ _, op in
        var registers = before
        op(&registers, a, b, c)
        return registers == after
    }).map({ $0.0 })
}

func parseInts(_ line: String?) -> [Int]? {
    let digits = "0123456789"
    return line?.split(whereSeparator: { !digits.contains($0) }).compactMap({ Int($0) })
}

var samples = [Sample]()
while let line = readLine(), line.hasPrefix("Before:") {
    if let before = parseInts(line), 
        let instruction = parseInts(readLine()), 
        let after = parseInts(readLine()),
        let _ = readLine() {
        samples.append((before, instruction, after))
    }
}

var program = [[Int]]()
if readLine()?.isEmpty == true {
    while let instruction = parseInts(readLine()) {
        program.append(instruction)
    }
}

print(samples.filter({ matchingOpIndices(sample: $0).count >= 3 }).count)

var choices = Array(repeating: Set(0..<ops.count), count: ops.count)
for sample in samples {
    let opcode = sample.instruction[0]
    let m = Set(matchingOpIndices(sample: sample))
    for i in 0..<ops.count {
        if !m.contains(i) {
            choices[i].remove(opcode)
        }
    }
}

for _ in 0..<ops.count {
    for i in 0..<ops.count {
        if choices[i].count == 1, let opcode = choices[i].first {
            for k in 0..<ops.count {
                if k != i {
                    choices[k].remove(opcode)
                }
            }
        }
    }
}

var opcodeToIndex = Array(repeating: 0, count: ops.count)
var isValidAssignment = true
for (i, opcodes) in choices.enumerated() {
    if opcodes.count == 1, let opcode = opcodes.first {
        opcodeToIndex[opcode] = i
    } else {
        isValidAssignment = false
    }        
}

if isValidAssignment {
    var registers = [0, 0, 0, 0]
    for inst in program {
        let (opcode, a, b, c) = (inst[0], inst[1], inst[2], inst[3])
        let op = ops[opcodeToIndex[opcode]]
        op(&registers, a, b, c)
    }
    print(registers[0])
}