From 19147d460c4d34731fdcb9b31e895e5eeda20559 Mon Sep 17 00:00:00 2001 From: Andrew Tomaka Date: Tue, 17 Dec 2024 01:13:47 -0500 Subject: [PATCH] Part 1 --- 17/input | 5 ++++ 17/main.rb | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 17/input create mode 100755 17/main.rb diff --git a/17/input b/17/input new file mode 100644 index 0000000..a12959d --- /dev/null +++ b/17/input @@ -0,0 +1,5 @@ +Register A: 17323786 +Register B: 0 +Register C: 0 + +Program: 2,4,1,1,7,5,1,5,4,1,5,5,0,3,3,0 diff --git a/17/main.rb b/17/main.rb new file mode 100755 index 0000000..8d5892c --- /dev/null +++ b/17/main.rb @@ -0,0 +1,79 @@ +#!/usr/bin/env ruby + +require "debug" + +input = (ARGV.first.nil? ? DATA : ARGF) + .readlines(chomp: true) +split = input.index("") + +registers = input[...split] + .map { _1.split(": ") } + .map(&:last) + .map(&:to_i) + .zip(%i[A B C]) + .map(&:reverse) + .to_h +program = input[split+1] + .split(" ") + .last + .split(",") + .map(&:to_i) + +OPERANDS = [ + ->() { 0 }, ->() { 1 }, ->() { 2 }, ->() { 3 }, + ->() { registers[:A] }, + ->() { registers[:B] }, + ->() { registers[:C] }, + ->() { :noop }, +] + +stdout = [] + +i = 0 +while i < program.size + opcode = program[i] + literal_operand = program[i+1] + combo_operand = OPERANDS[literal_operand].call + + i += 2 + + case opcode + when 0 + numerator = registers[:A] + denominator = 2 ** combo_operand + registers[:A] = numerator / denominator + when 1 + registers[:B] = registers[:B] ^ literal_operand + when 2 + registers[:B] = combo_operand % 8 + when 3 + if registers[:A] == 0 + :noop + else + i = literal_operand + end + when 4 + registers[:B] = registers[:B] ^ registers[:C] + when 5 + stdout << combo_operand % 8 + when 6 + numerator = registers[:A] + denominator = 2 ** combo_operand + registers[:B] = numerator / denominator + when 7 + numerator = registers[:A] + denominator = 2 ** combo_operand + registers[:C] = numerator / denominator + end +end + +p registers + +puts stdout.join(",") + +__END__ +Register A: 729 +Register B: 0 +Register C: 0 + +Program: 0,1,5,4,3,0