Sha256: 21d98e27e0daef2197942d46414ddb4f77f2a51e7a1ea809464e709503430131

Contents?: true

Size: 1.33 KB

Versions: 1

Compression:

Stored size: 1.33 KB

Contents

#!/usr/bin/env ruby

require "logicle"


USAGE_MESSAGE = "logicle <CIRCUIT FILE TO SOLVE> "


# Exit with usage message if user forgets to provide an input file.
Kernel.abort(USAGE_MESSAGE) if ARGV.length == 0


# Capture inputs from the command line.
circuit_file = ARGV.shift


# Instantiate the Logicle::Simulator instance.
simulator = Logicle::Simulator.new(circuit_file)
puts "Loaded main circuit from file: #{ circuit_file }"


# Prompt the user to enter states for each of the switch inputs.
switches = simulator.inputs
puts "The circuit to be solved requires #{ switches.count } input values (on/off)."

switches.each_with_index do |switch, index|
  $stdout.printf("Switch %d on? (Y/n):>> ", index + 1)
  input = $stdin.gets.chomp.downcase.chars.first
  case input
  when "y"
    switch.type = :on
  when "n"
    switch.type = :off
  else
    puts "Invalid input!"
    redo
  end
end


# Evaluate the circuit.
simulator.evaluate


# Print out the bulb states.
bulbs = simulator.outputs
bulbs.each_with_index do |bulb, i|
  puts "Bulb \##{ i + 1 }:  #{ bulb.state ? "on" : "off" }"
end


# Ask the user to save the result file.
$stdout.write "Save output? (y/N): "
save_output_flag = $stdin.gets.chomp

if save_output_flag =~ /\Ay/i
  output_filename = File.realdirpath(circuit_file).sub(/\.tgf\Z/, "_solved.tgf")
  simulator.save(output_filename)
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
logicle-0.1.1 bin/logicle