Skip to content

Commit

Permalink
Create Day24.scala
Browse files Browse the repository at this point in the history
  • Loading branch information
EricJin987 committed Dec 31, 2015
1 parent 080cb6e commit 1b085c1
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions Day24.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package Day24
object Day24 extends App{
def existsGroup(packages: List[Long],weight: Long, combSize: Int, restGroupSize: Int):Boolean =
packages.combinations(combSize).filter(_.sum==weight).toList match{
case _ if restGroupSize==1 => packages.sum==weight
case List() if combSize < packages.size => existsGroup(packages,weight,combSize+1,restGroupSize)
case List() => false
case successGroup =>successGroup.exists(g => existsGroup(packages.diff(g),weight,1,restGroupSize-1))
}
def choose(packages: List[Long],weight: Long, combSize: Int, restGroupSize: Int):Long =
packages.combinations(combSize).filter(p => p.sum==weight && existsGroup(packages.diff(p),weight,combSize,restGroupSize)).toList match{
case List() => choose(packages,weight,combSize+1,restGroupSize)
case group => group.sortBy(_.product).head.product
}
val input = scala.io.Source.fromFile("1.txt").getLines.map(_.toLong).toList
val weight1 = input.sum/3
val weight2 = input.sum/4
val part1 = choose(input,weight1,1,2)
println(part1)
val part2 = choose(input,weight2,1,3)
println(part2)
}

0 comments on commit 1b085c1

Please sign in to comment.