<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/'><id>tag:blogger.com,1999:blog-2399258289155141422.post255813948019679515..comments</id><updated>2009-03-05T03:09:35.821-03:00</updated><title type='text'>Comments on Jonhnny Weslley / My blog: Escape from Zurg</title><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://blog.jonhnnyweslley.net/feeds/255813948019679515/comments/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2399258289155141422/255813948019679515/comments/default'/><link rel='alternate' type='text/html' href='http://blog.jonhnnyweslley.net/2008/08/escape-from-zurg.html'/><author><name>Jonhnny Weslley</name><uri>http://www.blogger.com/profile/12114421477154945891</uri><email>jonhnnyweslley@gmail.com</email></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>1</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-2399258289155141422.post-7111844001261908039</id><published>2009-03-05T03:09:00.000-03:00</published><updated>2009-03-05T03:09:00.000-03:00</updated><title type='text'>Nice work. I'm in the process of learning Scala, a...</title><content type='html'>Nice work. I&amp;#39;m in the process of learning Scala, and I&amp;#39;ve always found it helpful to look at other people&amp;#39;s code, pick up some tricks from them and add my own five cents. So here&amp;#39;s my take:&lt;BR/&gt;&lt;BR/&gt;object ToyStory {&lt;BR/&gt;  case class Toy(name: String, time: Int) {&lt;BR/&gt;    override def toString = name&lt;BR/&gt;  }&lt;BR/&gt;&lt;BR/&gt;  abstract class Direction&lt;BR/&gt;  case object Left extends Direction &lt;BR/&gt;  case object Right extends Direction&lt;BR/&gt;&lt;BR/&gt;  case class Move(direction: Direction, toys: List[Toy]) {&lt;BR/&gt;    def cost = Iterable.max(toys.map(_.time))&lt;BR/&gt;&lt;BR/&gt;    override def toString = &lt;BR/&gt;      toys.mkString(&amp;quot;&amp;quot;, &amp;quot; and &amp;quot;, &amp;quot;&amp;quot;) +&lt;BR/&gt;      (if (toys.size == 1) &amp;quot; moves &amp;quot; else &amp;quot; move &amp;quot;) +&lt;BR/&gt;      direction.toString.toLowerCase&lt;BR/&gt;  }&lt;BR/&gt;&lt;BR/&gt;  type Path = List[Move]&lt;BR/&gt;&lt;BR/&gt;  def pairs[A](s: List[A]) =&lt;BR/&gt;    for ((first, index) &amp;lt;- s.toStream.zipWithIndex; second &amp;lt;- s.drop(index + 1))&lt;BR/&gt;      yield List(first, second)&lt;BR/&gt;  &lt;BR/&gt;  def solutions(toys: List[Toy], limit: Int) = {&lt;BR/&gt;    def solve_right(on_left: List[Toy],&lt;BR/&gt;                    on_right: List[Toy], limit: Int) : Stream[Path] =&lt;BR/&gt;      for (pair &amp;lt;- pairs(on_left);&lt;BR/&gt;           move = Move(Right, pair);&lt;BR/&gt;           remaining = limit - move.cost if remaining &amp;gt;= 0;&lt;BR/&gt;           path &amp;lt;- solve_left(on_left -- pair, pair ::: on_right, remaining))&lt;BR/&gt;        yield move :: path&lt;BR/&gt;&lt;BR/&gt;    def solve_left(on_left: List[Toy],&lt;BR/&gt;                   on_right: List[Toy], limit: Int) : Stream[Path] =&lt;BR/&gt;      on_left match {&lt;BR/&gt;        case Nil =&amp;gt; Stream(Nil)&lt;BR/&gt;        case _ =&amp;gt;&lt;BR/&gt;          for (toy  &amp;lt;- on_right.toStream;&lt;BR/&gt;               move = Move(Left, List(toy));&lt;BR/&gt;               remaining = limit - move.cost if remaining &amp;gt;= 0;&lt;BR/&gt;               path &amp;lt;- solve_right(toy :: on_left, on_right - toy, remaining))&lt;BR/&gt;            yield move :: path&lt;BR/&gt;      }&lt;BR/&gt;    &lt;BR/&gt;    solve_right(toys, Nil, limit)&lt;BR/&gt;  }&lt;BR/&gt;&lt;BR/&gt;  val toys =&lt;BR/&gt;    List(Toy(&amp;quot;Buzz&amp;quot;, 5), Toy(&amp;quot;Woody&amp;quot;, 10), Toy(&amp;quot;Rex&amp;quot;, 20), Toy(&amp;quot;Hamm&amp;quot;, 25))&lt;BR/&gt;&lt;BR/&gt;  def main(args : Array[String]) : Unit =&lt;BR/&gt;    for (path &amp;lt;- solutions(toys, 60))&lt;BR/&gt;      println(&amp;quot;Solution: &amp;quot; + path.mkString(&amp;quot;&amp;quot;, &amp;quot;, &amp;quot;, &amp;quot;.&amp;quot;))&lt;BR/&gt;}</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2399258289155141422/255813948019679515/comments/default/7111844001261908039'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2399258289155141422/255813948019679515/comments/default/7111844001261908039'/><link rel='alternate' type='text/html' href='http://blog.jonhnnyweslley.net/2008/08/escape-from-zurg.html?showComment=1236233340000#c7111844001261908039' title=''/><author><name>Olaf</name><uri>http://www.blogger.com/profile/11231627146285886887</uri><email>noreply@blogger.com</email></author><thr:in-reply-to xmlns:thr='http://purl.org/syndication/thread/1.0' href='http://blog.jonhnnyweslley.net/2008/08/escape-from-zurg.html' ref='tag:blogger.com,1999:blog-2399258289155141422.post-255813948019679515' source='http://www.blogger.com/feeds/2399258289155141422/posts/default/255813948019679515' type='text/html'/></entry></feed>