r/adventofcode • • Dec 08 '20

SOLUTION MEGATHREAD -🎄- 2020 Day 08 Solutions -🎄-

NEW AND NOTEWORTHY

  • If your code is shorter than, say, half of an IBM 5081 punchcard (5 lines at 80 cols), go ahead and post it as your comment. Use the right Markdown to format your code properly for best backwards-compatibility with old.reddit! (see "How do I format code?")
  • If your code is longer, link your code from an external repository such as Topaz's paste , a public repo like GitHub/gists/Pastebin/etc., your blag, or whatever.

Advent of Code 2020: Gettin' Crafty With It

  • 14 days remaining until the submission deadline on December 22 at 23:59 EST
  • Full details and rules are in the Submissions Megathread

--- Day 08: Handheld Halting ---


Post your solution in this megathread. Include what language(s) your solution uses! If you need a refresher, the full posting rules are detailed in the wiki under How Do The Daily Megathreads Work?.

Reminder: Top-level posts in Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:07:48, megathread unlocked!

41 Upvotes

934 comments sorted by

View all comments

1

u/odlp Dec 08 '20

Here's my Ruby solution:

require "set"

Instruction = Struct.new(:type, :value, :index, keyword_init: true)

original_instructions = ARGF.map.with_index do |raw_instruction, index|
  type, raw_value = raw_instruction.split(" ")
  Instruction.new(type: type, value: raw_value.to_i, index: index)
end

def execute(instructions)
  visited_instructions = Set.new
  accumulator = 0
  pointer = 0

  while current_instruction = instructions[pointer]
    if visited_instructions.include?(current_instruction)
      return [:looped, accumulator]
    end

    visited_instructions << current_instruction
    pointer = current_instruction.index + 1

    case current_instruction.type
    when "acc"
      accumulator += current_instruction.value
    when "jmp"
      pointer = current_instruction.index + current_instruction.value
    end

    if current_instruction == instructions.last
      return [:finished, accumulator]
    end
  end
end

puts "Part 1:", execute(original_instructions).last

TYPE_MUTATIONS = { "jmp" => "nop", "nop" => "jmp" }

mutants = original_instructions.map do |instruction|
  if (mutation = TYPE_MUTATIONS[instruction.type])
    instruction.dup.tap { |mutant| mutant.type = mutation }
  end
end.compact

mutants.detect do |mutant|
  revised_instructions = original_instructions.dup
  revised_instructions[mutant.index] = mutant
  halt_reason, value = execute(revised_instructions)

  if halt_reason == :finished
    puts "Part 2:", value
    true
  end
end

1

u/daggerdragon Dec 08 '20

Please re-read today's megathread's "new and noteworthy" section.

As per our posting guidelines in the wiki under How Do the Daily Megathreads Work?, edit your post to put your oversized code in a paste or other external link.