From 1362f76e60c32a7bf0e7240b01524d16734f8dd4 Mon Sep 17 00:00:00 2001 From: Marcin Chrzanowski Date: Fri, 25 Dec 2020 15:23:39 +0100 Subject: Add day 8 --- 08/b.rb | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 08/b.rb (limited to '08/b.rb') diff --git a/08/b.rb b/08/b.rb new file mode 100644 index 0000000..ef4fce1 --- /dev/null +++ b/08/b.rb @@ -0,0 +1,46 @@ +code = [] + +File.readlines('input.txt').each do |line| + instruction, number = line.split + code.push [instruction, number.to_i] +end + +visited = {} +current_line = 0 +acc = 0 + +code.size.times do |i| + code_copy = code.map {|e| e.slice(0, 2)} + case code_copy[i][0] + when 'jmp' + code_copy[i][0] = 'nop' + when 'nop' + code_copy[i][0] = 'jmp' + end + + + visited = {} + current_line = 0 + acc = 0 + + while !visited[current_line] + if current_line == code.size + puts acc + break + elsif current_line > code.size + puts 'incorrect run' + break + end + + visited[current_line] = true + case code_copy[current_line][0] + when 'nop' + current_line += 1 + when 'acc' + acc += code_copy[current_line][1] + current_line += 1 + when 'jmp' + current_line += code_copy[current_line][1] + end + end +end -- cgit v1.2.3