From b39d71d124270bd271d880e567f643c856f866f2 Mon Sep 17 00:00:00 2001 From: Jeff Yates Date: Wed, 9 Dec 2020 10:36:57 -0500 Subject: [PATCH] solved part 2 --- day08/solution2.rb | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100755 day08/solution2.rb diff --git a/day08/solution2.rb b/day08/solution2.rb new file mode 100755 index 0000000..f724787 --- /dev/null +++ b/day08/solution2.rb @@ -0,0 +1,40 @@ +#!/bin/env ruby + +def run_code (instructions) + acc = 0 + tracker = Array.new(instructions.length, false) + i = 0 + success = false + while tracker[i] == false do + tracker[i] = true + case instructions[i][0..2] + when 'acc' + acc += instructions[i].split(' ')[1].to_i + i += 1 + when 'jmp' + i += instructions[i].split(' ')[1].to_i + when 'nop' + i += 1 + end + success = true if i == instructions.length + end + return success, acc, tracker +end + +input = File.readlines('./input').to_ary.map(&:strip) +succ, acc, tracker = false, 0, [] + +input.each.with_index do |instruction, i| + mod = input.map(&:clone) + if instruction[0..2] == 'jmp' + mod[i][0..2] = 'nop' + elsif instruction[0..2] == 'nop' + mod[i][0..2] = 'jmp' + else + next + end + succ, acc, tracker = run_code(mod) + break if succ == true +end + +p acc \ No newline at end of file