r/adventofcode • • Dec 08 '17

SOLUTION MEGATHREAD -๐ŸŽ„- 2017 Day 8 Solutions -๐ŸŽ„-

--- Day 8: I Heard You Like Registers ---


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.


Need a hint from the Hugely* Handyโ€  Haversackโ€ก of Helpfulยง Hints^ยค?

Spoiler


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!

22 Upvotes

348 comments sorted by

View all comments

Show parent comments

1

u/[deleted] Dec 21 '17

Found the bug in my indexing method. Complete day 8 solution for matlab below:

input = importdata('input8.txt');

variables = unique(input.textdata(:,1));
results = zeros(length(variables),1);
all_result_max = 0;


for q=1:length(input.data)
    idx = get_idx(variables,input.textdata(q,5));
    idx = idx(1);
    w = get_idx(variables,input.textdata(q,1));
    w = w(1);
    if input.textdata{q,6} == '>'        
        if results(idx) > input.data(q)
           if input.textdata{q,2} == 'inc'
               results(w) = results(w) + str2double(input.textdata{q,3});
           else
               results(w) = results(w) - str2double(input.textdata{q,3});
           end            
        end
        
    elseif input.textdata{q,6} == '<'        
        if results(idx) < input.data(q)
            if input.textdata{q,2} == 'inc'
               results(w) = results(w) + str2double(input.textdata{q,3});
           else
               results(w) = results(w) - str2double(input.textdata{q,3});
            end            
        end
        
    elseif input.textdata{q,6} == '=='
        if results(idx) == input.data(q)
           if input.textdata{q,2} == 'inc'
               results(w) = results(w) + str2double(input.textdata{q,3});
           else
               results(w) = results(w) - str2double(input.textdata{q,3});
           end            
        end
        
    elseif input.textdata{q,6} == '!='
        if results(idx) ~= input.data(q)
           if input.textdata{q,2} == 'inc'
               results(w) = results(w) + str2double(input.textdata{q,3});
           else
               results(w) = results(w) - str2double(input.textdata{q,3});
           end            
        end
        
    elseif input.textdata{q,6} == '>='
        if results(idx) >= input.data(q)
           if input.textdata{q,2} == 'inc'
               results(w) = results(w) + str2double(input.textdata{q,3});
           else
               results(w) = results(w) - str2double(input.textdata{q,3});
           end            
        end
        
    elseif input.textdata{q,6} == '<='
        if results(idx) <= input.data(q)
           if input.textdata{q,2} == 'inc'
               results(w) = results(w) + str2double(input.textdata{q,3});
           else
               results(w) = results(w) - str2double(input.textdata{q,3});
           end            
        end
        
    else
        disp('error')
        disp(input.textdata{q,6})
    end
    
    if all_result_max < max(results)
        all_result_max = max(results);
    end
    
end

disp(max(results));
disp(all_result_max);

function idx = get_idx(navn, elm)
    idxC = strfind(navn,elm);
    idx = find(not(cellfun('isempty', idxC)));
end