Skip to content

Instantly share code, notes, and snippets.

@taichi
Created August 17, 2012 19:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save taichi/3381668 to your computer and use it in GitHub Desktop.
Save taichi/3381668 to your computer and use it in GitHub Desktop.
class Baseball {
def strike = 0, ball = 0, out = 0
def s = {
if(2 < ++strike) {
strike = 0; o()
}
}
def b = {
if(3 < ++ball) {
h()
}
}
def o = {
if(2 < ++out) {
strike = ball = out = 0
}
}
def f = {
if(strike < 2) {
s()
}
}
def h = { strike = ball = 0 }
def p = { h(); o() }
def resolve = {
it.collect {
this[it](); "$out$strike$ball"
}.join(",")
}
}
import static org.junit.Assert.*
import org.junit.Test
class BaseballTest {
@Test
void test() {
'''s -> 010
sss -> 010,020,100
bbbb -> 001,002,003,000
ssbbbb -> 010,020,021,022,023,000
hsbhfhbh -> 000,010,011,000,010,000,001,000
psbpfpbp -> 100,110,111,200,210,000,001,100
ppp -> 100,200,000
ffffs -> 010,020,020,020,100
ssspfffs -> 010,020,100,200,210,220,220,000
bbbsfbppp -> 001,002,003,013,023,000,100,200,000
sssbbbbsbhsbppp -> 010,020,100,101,102,103,100,110,111,100,110,111,200,000,100
ssffpffssp -> 010,020,020,020,100,110,120,200,210,000'''.eachLine {
def (i,o) = it.split(' -> ')
Baseball target = new Baseball()
assert target.resolve(i) == o
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment