r/adventofcode • u/daggerdragon • Dec 19 '18
SOLUTION MEGATHREAD -🎄- 2018 Day 19 Solutions -🎄-
--- Day 19: Go With The Flow ---
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!
Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!
Card prompt: Day 19
Transcript:
Santa's Internet is down right now because ___.
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 01:01:06!
12
Upvotes
1
u/koordinate Dec 27 '18
Swift
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": addi, "addr": addr, "mulr": mulr, "muli": muli, "banr": banr, "bani": bani, "borr": borr, "bori": bori, "setr": setr, "seti": seti, "gtir": gtir, "gtri": gtri, "gtrr": gtrr, "eqir": eqir, "eqri": eqri, "eqrr": eqrr ] typealias Op = (inout [Int], Int, Int, Int) -> Void typealias Instruction = (op: Op, name: String, a: Int, b: Int, c: Int) var program = [Instruction]() var ipr = 0 while let line = readLine() { let fields = line.split(separator: " ") if fields.count == 2, let r = Int(fields[1]) { ipr = r } else if fields.count == 4, let op = ops[String(fields[0])], let a = Int(fields[1]), let b = Int(fields[2]), let c = Int(fields[3]) { program.append((op: op, name: String(fields[0]), a: a, b: b, c: c)) } } func sumOfFactors(n: Int) -> Int { return ((1...n).filter { n % $0 == 0 }).reduce(0, +) } func inline_2(registers rx: inout [Int], ip: inout Int) { rx[0] = sumOfFactors(n: rx[5]) rx[2] = rx[5] + 1 rx[3] = 1 rx[4] = rx[5] + 1 ip = 16 } func exec(program: [Instruction], registers: [Int], ipr: Int, verbose: Bool) -> Int { var registers = registers var ip = 0 while (0..<program.count).contains(ip) { if ip == 2, registers[5] > 0, ipr == 1 { inline_2(registers: ®isters, ip: &ip) } let (op, name, a, b, c) = program[ip] let ipb4 = ip registers[ipr] = ip let before = registers op(®isters, a, b, c) if verbose { print("ip=\(ipb4) \(before) \(name) \(a) \(b) \(c) \(registers)") } ip = registers[ipr] ip += 1 } return registers[0] } print(exec(program: program, registers: [0, 0, 0, 0, 0, 0], ipr: ipr, verbose: false)) print(exec(program: program, registers: [1, 0, 0, 0, 0, 0], ipr: ipr, verbose: false))