Skip to content

Commit

Permalink
Gentle refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric Bowman committed Jan 27, 2014
1 parent f56fa51 commit e407ee7
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 31 deletions.
22 changes: 11 additions & 11 deletions People.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
#Alice 5
#Bob 10
#Candace 20
#Dave 25
#Eric 15
#Fred 10
#George 15
Alice 5
Bob 10
Candace 20
Dave 25
Eric 15
Fred 10
George 15
#Henry 5
#Isaac 10
#James 15
#Karen 20
Alice 5
Bob 3
Candace 7
Dave 6
#Alice 5
#Bob 3
#Candace 7
#Dave 6
24 changes: 12 additions & 12 deletions src/main/scala/Bridge.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ import java.util.Date
trait Bridge {

// set of people involved
val people: Set[Person]
def people: Set[Person]

// scale factor for simplifying the problem
val scale: Int
def scale: Int

def progress(batteryCharge: Int): Unit = {}

Expand All @@ -34,7 +34,7 @@ trait Bridge {

// Lazily stream until we find a solution, however long
// that takes, starting from the heuristic mentioned
def solve: Option[(Int, Stream[Stream[State]])] = {
lazy val solve: Option[(Int, Stream[Stream[State]])] = {
Stream.from(sum).map {
i =>
progress(i)
Expand Down Expand Up @@ -150,22 +150,22 @@ object BridgeApp extends App with Bridge {
// load contents of source file into scaledPeople
val scaledPeople: Set[Person] = io.Source.fromFile(args(0)).getLines().foldLeft(Set[Person]()) {
(set: Set[Person], line: String) =>
line.split("\\s+").toList match {
case first :: _ if first.startsWith("#") => set
case name :: minutes :: Nil => set + Person(name = name, crossingTime = minutes.toInt)
line.split("\\s+") match {
case Array(first, _*) if first.startsWith("#") => set
case Array(name, minutes) => set + Person(name = name, crossingTime = minutes.toInt)
case _ => set
}
}

// We may be able to transform this to a simpler problem, if a greatest common divisor exists
// for the set of crossing times. We return from this block here the set of people and crossing
// times to solve, as well as a scaling factor to multiply the results by, to match the actual
// input instead of the
GCD.commonDivisor(scaledPeople.map(_.crossingTime)) match {
case Some(divisor) =>
(scaledPeople.map(p => p.copy(crossingTime = (p.crossingTime / divisor))), divisor)
case None =>
(scaledPeople, 1)
// input instead of the scaled data
GCD.commonDivisor(scaledPeople.map(_.crossingTime)).map {
case divisor =>
(scaledPeople.map(p => p.copy(crossingTime = p.crossingTime / divisor)), divisor)
}.getOrElse {
(scaledPeople, 1)
}
}

Expand Down
16 changes: 8 additions & 8 deletions src/main/scala/GCD.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ object GCD {
/**
* Computes the prime factors of the given argument.
*/
def primeFactors(num: Int): List[Int] = {
var n = num;
def primeFactors(num: Int): Seq[Int] = {
var n = num
var i = 2
val pending = collection.mutable.ListBuffer[Int]()
while (i <= n) {
Expand All @@ -36,14 +36,14 @@ object GCD {
if (n > 1) {
pending += n
}
pending.toList
pending.toSeq
}

/**
* Returns the lowest common divisor, if any, between the two arguments.
*/
def lcd(x: Int, y: Int): Option[Int] = {
((primeFactors(x) intersect primeFactors(y)).toSet -- Set(1)) match {
(primeFactors(x) intersect primeFactors(y)).toSet - 1 match {
case empty if empty.isEmpty => None
case full => Some(full.product)
}
Expand All @@ -53,10 +53,10 @@ object GCD {
* Returns a common divisor, if one exists, across the list of numbers.
*/
def commonDivisor(numbers: Iterable[Int]): Option[Int] = {
numbers.toList match {
case Nil => None
case a :: Nil => None
case head :: tail => (Option(head) /: tail) {
numbers.headOption match {
case None => None
case Some(a) if numbers.tail.isEmpty => None
case head => (head /: numbers.tail) {
case (None, _) => None
case (Some(a), b) => lcd(a, b)
}
Expand Down

0 comments on commit e407ee7

Please sign in to comment.