Commits

Steven! Ragnarök committed b4e22bd

Start work on puzzle 15.

  • Participants
  • Parent commits ababcca

Comments (0)

Files changed (4)

 syntax: glob
+*.swp
+*.swo
+.rvmrc
+.vimrc
+.redcar/*
 .DS_Store
+

File 13/lib/board.rb

   end
 
   # a move is valid when:
-  # 1. [ ] There is a piece at the first square
+  # 1. [x] There is a piece at the first square
   # 2. [ ] The last square is a valid destination for the piece.
   # 3. [ ] There is a clear path to the destination.
   # 4. [ ] The destination is empty or has a capturable piece.
   def valid_move? move
-    return false if not empty? move.first
-
+    return false if empty? move.first
     true
   end
 

File 15/lib/stacker.rb

+module Stacker
+  class Interpreter
+    attr_reader :stack
+
+    COMMANDS = %w[ADD SUBTRACT]
+
+    def initialize
+      @stack = []
+    end
+
+    def execute command
+      unless commands.include? command
+        @stack.push command
+      else
+        @stack = send command
+      end
+    end
+
+    def commands
+      public_methods.select{ |m| m =~ /[A-Z]+/ }.map(&:to_s)
+    end
+
+    def ADD
+      [@stack.map(&:to_i).inject(&:+)]
+    end
+
+    def SUBTRACT
+      [@stack.map(&:to_i).inject(&:-)]
+    end
+
+    def MULTIPLY
+      [@stack.map(&:to_i).inject(&:*)]
+    end
+
+    def DIVIDE
+      [@stack.map(&:to_i).inject(&:/)]
+    end
+  end
+end

File 15/stacker_test.rb

 #
 # gem "minitest"
 
+
 require "minitest/autorun"
+require "./lib/stacker"
 
 describe "Stacker::Interpreter" do
 
       2
       SUBTRACT
     ]
-    
+
     interpreter.stack.must_equal([-1])
   end
-  
+
   it "implements MULTIPLY" do
     execute %w[
       3
       10
       MULTIPLY
     ]
-    
+
     interpreter.stack.must_equal([30])
   end
 
       2
       DIVIDE
       ]
-      
+
     interpreter.stack.must_equal([3])
   end
   
   end
   
   it "implements <" do
+    skip
     execute %w[
       3
       10
   end
   
   it "implements >" do
+    skip
     execute %w[
       3
       10
   end
 
   it "implements =" do
+    skip
     execute %w[
       1
       1
   end
   
   it "implements the IF command" do
+    skip
     execute %w[
       :true
       IF
   end
   
   it "implements the IF command in nesting" do
+    skip
     execute %w[ 
        :true
        IF
   end
   
   it "implements the IF command in another 2-level nesting" do
+    skip
     execute %w[
       :false
       IF
   end
 
   it "implements the IF command in 3-level nesting" do
+    skip
     execute %w[
       :true
       IF
   end
 
   it "implements TIMES command" do
+    skip
     execute %w[
       5
       3
   end
 
   it "implements TIMES inside of IF command" do
+    skip
     execute %w[
       :true
       IF
   end
   
   it "implements IF inside of TIMES command" do
+    skip
     execute %w[
       5
       3
   end
 
   it "implements PROCEDURE command" do
+    skip
     execute %w[
       PROCEDURE\ DOUBLE
       2
   end
   
   it "implements DUP command" do
+    skip
     execute %w[
       1
       2
   end
 
   it "implements SWAP command" do
+    skip
     execute %w[
       1
       2
   end
   
   it "implements DROP" do
+    skip
     execute %w[
       1
       2
   end
   
   it "implements ROT" do
+    skip
     execute %w[
       :foo
       :bar
   end
 
   it "implements support for adding arbitrary symbols to the stack" do
+    skip
     execute %w[
       :foo
       :bar