-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9862eca
commit a412a1d
Showing
6 changed files
with
174 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
jio a, +18 | ||
inc a | ||
tpl a | ||
inc a | ||
tpl a | ||
tpl a | ||
tpl a | ||
inc a | ||
tpl a | ||
inc a | ||
tpl a | ||
inc a | ||
inc a | ||
tpl a | ||
tpl a | ||
tpl a | ||
inc a | ||
jmp +22 | ||
tpl a | ||
inc a | ||
tpl a | ||
inc a | ||
inc a | ||
tpl a | ||
inc a | ||
tpl a | ||
inc a | ||
inc a | ||
tpl a | ||
tpl a | ||
inc a | ||
inc a | ||
tpl a | ||
inc a | ||
inc a | ||
tpl a | ||
inc a | ||
inc a | ||
tpl a | ||
jio a, +8 | ||
inc b | ||
jie a, +4 | ||
tpl a | ||
inc a | ||
jmp +2 | ||
hlf a | ||
jmp -7 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
inc a | ||
jio a, +2 | ||
tpl a | ||
inc a |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
require 'year_2015/day_23' | ||
|
||
describe Year2015::Day23 do | ||
context 'when Part 1' do | ||
subject(:sample_one) do | ||
described_class.new(File.read('spec/year_2015/day_23_sample_one'), true).program | ||
end | ||
|
||
it 'gives a final result' do | ||
sample_one.execute | ||
expect(sample_one.registers['a']).to eq(2) | ||
end | ||
end | ||
|
||
context 'when Results' do | ||
subject(:input_data) do | ||
File.read('spec/year_2015/day_23_input') | ||
end | ||
|
||
it 'correctly answers part 1' do | ||
expect(described_class.new(input_data, true).part_one).to eq(307) | ||
end | ||
|
||
it 'correctly answers part 2' do | ||
expect(described_class.new(input_data).part_two).to eq(160) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
class Year2015 | ||
class Day23 | ||
attr_accessor :program | ||
|
||
class Program | ||
attr_accessor :instructions, :pc, :registers | ||
|
||
def initialize(input_instructions) | ||
@instructions = input_instructions.map do |instruction| | ||
[instruction, interpret(instruction)] | ||
end | ||
end | ||
|
||
# rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength | ||
def interpret(instruction) | ||
case instruction | ||
when /^(hlf|tpl|inc) ([ab])$/ | ||
action = $1 | ||
reg = $2 | ||
proc do | ||
case action | ||
when 'hlf' | ||
@registers[reg] = @registers[reg] / 2 | ||
when 'tpl' | ||
@registers[reg] *= 3 | ||
when 'inc' | ||
@registers[reg] += 1 | ||
end | ||
@pc += 1 | ||
end | ||
when /^jmp ([+-]?\d+)$/ | ||
offset = $1.to_i | ||
proc do | ||
@pc += offset | ||
end | ||
when /^ji([eo]) ([ab]), ([\+\-]\d+)$/ | ||
even_or_one = $1 == 'e' | ||
reg = $2 | ||
offset = $3.to_i | ||
proc do | ||
reg_value = @registers[reg] | ||
move_offset = (even_or_one && reg_value.even?) || (!even_or_one && reg_value == 1) | ||
@pc += move_offset ? offset : 1 | ||
end | ||
else | ||
raise "Invalid instruction: #{instruction}" | ||
end | ||
end | ||
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength | ||
|
||
def execute(init_registers = { 'a' => 0, 'b' => 0 }) | ||
@pc = 0 | ||
@registers = init_registers | ||
|
||
while instructions[@pc] | ||
# puts "=> #{@pc} - #{@registers['a']} | #{@registers['b']}=> #{instructions[@pc].first}" | ||
instance_eval(&instructions[@pc].last) | ||
end | ||
end | ||
end | ||
|
||
def initialize(input_data, input_part_one = false) | ||
@version = input_part_one ? 1 : 2 | ||
@program = Program.new(input_data.chomp.split("\n")) | ||
end | ||
|
||
def part_one | ||
@program.execute | ||
@program.registers['b'] | ||
end | ||
|
||
def part_two | ||
@program.execute('a' => 1, 'b' => 0) | ||
@program.registers['b'] | ||
end | ||
end | ||
end |