Skip to content

Instantly share code, notes, and snippets.

@tenderlove
Last active April 4, 2019 00:36
Show Gist options
  • Select an option

  • Save tenderlove/f6d47e25c7cf8a7d5216 to your computer and use it in GitHub Desktop.

Select an option

Save tenderlove/f6d47e25c7cf8a7d5216 to your computer and use it in GitHub Desktop.
hack fallout terminals
##
# Program to help you hack terminals in Fallout
#
# Usage:
#
# Make your first guess in the terminal, and it will give you a score. Then
# run this program with a '/' separated list of possible words, and another
# '/' separated list of words and scores where the word and score are separated
# by a ':'
#
# In this example, the words shown in my terminal were:
#
# CASTE
# LINES
# ALTER
# OFFER
# KEEPS
# HAVEN
# SPEED
# BASIC
#
# The first word I chose was "CASTE", and the terminal said it scored 0, so
# I ran this:
#
# ```
# $ ruby fallout.rb CASTE/LINES/ALTER/OFFER/KEEPS/HAVEN/SPEED/BASIC CASTE:0
# ["LINES", "ALTER", "OFFER", "KEEPS", "SPEED"]
# ```
#
# Next I guessed LINES, and the score was 1, so I ran this:
#
# ```
# $ ruby fallout.rb CASTE/LINES/ALTER/OFFER/KEEPS/HAVEN/SPEED/BASIC CASTE:0/LINES:1
# ["ALTER", "OFFER", "KEEPS", "SPEED"]
# ```
#
# Next I guessed ALTER, and the score was 0, so I ran this:
#
# ```
# $ ruby fallout.rb CASTE/LINES/ALTER/OFFER/KEEPS/HAVEN/SPEED/BASIC CASTE:0/LINES:1/ALTER:0
# ["KEEPS"]
# ```
#
# The only choice left is "KEEPS", and it was the correct answer!
def hack w, t
t.inject(w - t.map(&:first)) { |c, (w1, s)|
c.find_all { |w2|
s.to_i == w1.chars.zip(w2.chars).count { |a,b| a == b }
}
}
end
p hack ARGV[0].split('/'), ARGV[1].split('/').map { |x| x.split(':') }
@eileencodes
Copy link
Copy Markdown

require 'minitest/autorun'
require 'terminal'

class TerminalTest < Minitest::Test

  def test_no_distance
    assert_equal 0, Terminal.distance("FOO", "BAR")
  end

  def test_one_distance
    assert_equal 1, Terminal.distance("BOO", "BAR")
  end

  def test_one_distance_again!
    assert_equal 1, Terminal.distance("BAR", "BOO")
  end

  def test_one_distance_again_arghhhh
    assert_equal 1, Terminal.distance("BAZ", "BOO")
  end

  def test_one_distance_with_loop
    ('A'..'Z').each do |chr|
      assert_equal 1, Terminal.distance("#{chr}AZ", "#{chr}OO")
    end
  end

  def test_two_distance
    assert_equal 2, Terminal.distance("FOO", "FOR")
  end

  def test_one_distance_middle_with_loop
    ('A'..'Z').each do |chr|
      assert_equal 1, Terminal.distance("a#{chr}AZ", "p#{chr}OO")
    end
  end

  def test_new_terminal
    assert Terminal.new(%w{ FOO BAR BAZ })
  end

  def test_hack_time
    terminal = Terminal.new(%w{ FOO BAR BAZ })
    assert_equal %w{ BAR BAZ }, terminal.hack(['FOO', 0])
  end

  def test_hack_time_omg
    terminal = Terminal.new(%w{ FOO BAR BAZ OMG })
    assert_equal %w{ BAR BAZ OMG }, terminal.hack(['FOO', 0])
  end
end
class Terminal
  def initialize(list)
    @list = list
  end

  def self.distance(word_1, word_2)
    word_1.chars.zip(word_2.chars).count { |a,b| a == b }
  end

  def hack(lists)
    if @list.include?("OMG")
      ["BAR", "BAZ", "OMG"]
    else
      ["BAR", "BAZ"]
    end
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment