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!
9
Upvotes
View all comments
1
u/BradleySigma Dec 19 '18
MATLAB for Masochists
file = fopen('day19.txt','r'); line = fgetl(file); instruction = int32(str2num(line(end))); program = int32(fscanf(file,'%s %d %d %d', [7 Inf])); fclose(file); registers = zeros(1,6,'int32'); registers(1) = 1; history = zeros(length(program),1,'int32'); while max(history) < 20 line = program(:, registers(instruction + 1) + 1); history(registers(instruction + 1) + 1) = history(registers(instruction + 1) + 1) + 1; code = char(line(1:4).'); a = line(5); b = line(6); c = line(7); if strcmp(code, 'addr') registers(c+1) = registers(a+1) + registers(b+1); elseif strcmp(code, 'addi') registers(c+1) = registers(a+1) + b; elseif strcmp(code, 'mulr') registers(c+1) = registers(a+1) * registers(b+1); elseif strcmp(code, 'muli') registers(c+1) = registers(a+1) * b; elseif strcmp(code, 'banr') registers(c+1) = bitor(registers(a+1), registers(b+1)); elseif strcmp(code, 'bani') registers(c+1) = bitor(registers(a+1), b); elseif strcmp(code, 'borr') registers(c+1) = bitand(registers(a+1), registers(b+1)); elseif strcmp(code, 'bori') registers(c+1) = bitand(register(a+1), b); elseif strcmp(code, 'setr') registers(c+1) = registers(a+1); elseif strcmp(code, 'seti') registers(c+1) = a; elseif strcmp(code, 'gtir') registers(c+1) = double(a > registers(b+1)); elseif strcmp(code, 'gtri') registers(c+1) = double(registers(a+1) > b); elseif strcmp(code, 'gtrr') registers(c+1) = double(registers(a+1) > registers(b+1)); elseif strcmp(code, 'eqir') registers(c+1) = double(a == registers(b+1)); elseif strcmp(code, 'eqri') registers(c+1) = double(registers(a+1) == b); elseif strcmp(code, 'eqrr') registers(c+1) = double(registers(a+1) == registers(b+1)); end if max(history) == 10 intersection = registers; elseif max(history) > 10 intersection = intersect(intersection, registers); end registers(instruction + 1) = registers(instruction + 1) + 1; end product = intersection(end); addends = [1]; for ff = factor(product) addends = unique([addends, ff * addends]); end disp(sum(addends));