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

3

u/nuvan Dec 16 '18 edited Dec 16 '18

Ruby 177/321

I really thought I could do better when I read the description, as I've played around with simple VMs before (eg the Synacor Challenge). Ah well, still fun.

I cost myself at least 20 minutes on part 2 because I kept attempting to work out the opcode values using the initial register settings from the samples, instead of the actual instructions. :facepalm: :headdesk:

OPCODES = {
	addr: -> regs, a, b, c { regs[c] = regs[a] + regs[b] },
	addi: -> regs, a, b, c { regs[c] = regs[a] + b },
	mulr: -> regs, a, b, c { regs[c] = regs[a] * regs[b]},
	muli: -> regs, a, b, c { regs[c] = regs[a] * b  },
	banr: -> regs, a, b, c { regs[c] = regs[a] & regs[b] },
	bani: -> regs, a, b, c { regs[c] = regs[a] & b },
	borr: -> regs, a, b, c { regs[c] = regs[a] | regs[b] },
	bori: -> regs, a, b, c { regs[c] = regs[a] | b },
	setr: -> regs, a, b, c { regs[c] = regs[a] },
	seti: -> regs, a, b, c { regs[c] = a },
	gtir: -> regs, a, b, c { regs[c] = a > regs[b] ? 1 : 0 },
	gtri: -> regs, a, b, c { regs[c] = regs[a] > b ? 1 : 0 },
	gtrr: -> regs, a, b, c { regs[c] = regs[a] > regs[b] ? 1 : 0 },
	eqir: -> regs, a, b, c { regs[c] = a == regs[b] ? 1 : 0 },
	eqri: -> regs, a, b, c { regs[c] = regs[a] == b ? 1 : 0 },
	eqrr: -> regs, a, b, c { regs[c] = regs[a] == regs[b] ? 1 : 0 },
}

def solve
	opcodes = (0...16).map{ |n| [n,nil] }.to_h
	part1_count = 0
	samples = 0
	smallest_sizes = {}
	idx = 0
	@input.lines.each_slice(4) do |before,input,after,_|
		samples += 1
		registers = /Before:\s+\[(\d+), (\d+), (\d+), (\d+)\]/.match(before)[1..-1].map(&:to_i)
		expected_regs = /After:\s+\[(\d+), (\d+), (\d+), (\d+)\]/.match(after)[1..-1].map(&:to_i)

		op, a, b, out = /(\d+) (\d+) (\d+) (\d+)/.match(input)[1..-1].map(&:to_i)

		idx += 4
		rs = nil
		possibles = OPCODES.select do |opcode,oper|
			rs = registers.dup
			oper.call(rs,a,b,out)
			rs == expected_regs
		end

		part1_count += 1 if possibles.size >= 3
		if possibles.size == 1
			opcodes[op] = possibles.keys.first
			smallest_sizes[op] = 1
		else
			begin
				if opcodes[op].nil?
					opcodes[op] = possibles.keys
					smallest_sizes[op] = possibles.keys.size
				elsif opcodes[op].is_a?(Array)
					intersection = opcodes[op] & possibles.keys
					if smallest_sizes[op] > intersection.size
						smallest_sizes[op] = intersection.size
					end
					opcodes[op] = intersection
					if opcodes[op].size == 1
						opcodes[op] = opcodes[op].first
					end
				end
			rescue => e
				puts e
				puts op
				puts possibles.keys
				puts opcodes
				puts smallest_sizes
				raise
			end
		end
	rescue
		break
	end

	changed = true
	until !changed do
		changed = false
		known_ops = opcodes.values.select{|op| op.is_a?(Symbol)}
		opcodes.each do |k,v|
			next if v.is_a?(Symbol)
			res = v - known_ops
			if res != v
				changed = true
				if res.size == 1
					opcodes[k] = res.first
				else
					opcodes[k] = res
				end
			end
		end
	end

	puts "There are #{opcodes.values.uniq.size} known opcocdes"
	puts "There are #{part1_count} samples out of #{samples} that behave like 3 or more opcodes"

	registers = [0,0,0,0]
	instrs = 0
	@lines[idx..-1].each do |line|
		next if line.empty?
		instrs += 1
		if instrs == 1
			puts "The first instruction is #{line}"
		end
		op, a, b, out = /(\d+) (\d+) (\d+) (\d+)/.match(line)[1..-1].map(&:to_i)
		OPCODES[opcodes[op]].call(registers,a,b,out)
	end

	puts "The value of register 0 after running the program is #{registers.join(" ")} (#{instrs} instructions were run)"
end

0

u/BluFoot Dec 16 '18

Tabs?! In Ruby?! HERESY!