Something went wrong. Try again.
Code for the Advent of Code event
aoc advent-of-code
Something went wrong. Try again.
878 B · 48 lines
Ruby
at rust
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849#!/usr/bin/env ruby# frozen_string_literal: true
program = File.read('input.txt').split(',').map(&:to_i)
def run(program, noun, verb) memory = program.dup memory[1] = noun memory[2] = verb
run = true pc = 0
while run opcode = memory[pc]
case opcode when 1 a = memory[pc + 1] b = memory[pc + 2] c = memory[pc + 3] memory[c] = memory[a] + memory[b] pc += 4 when 2 a = memory[pc + 1] b = memory[pc + 2] c = memory[pc + 3] memory[c] = memory[a] * memory[b] pc += 4 when 99 run = false end end
memoryend
part1 = run program, 12, 2puts "Part 1: #{part1[0]}"
PART2_TARGET = 19_690_720
part2 = (0..99).to_a.repeated_permutation(2).find do |(noun, verb)| result = run program, noun, verb result[0] == PART2_TARGETend
puts "Part 2: #{(100 * part2[0]) + part2[1]}"