-
-
Save sangkeon/3e9ba1592baeafbd0ecce37a09a12a1c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.io.Source; | |
object Main { | |
def main(args: Array[String]): Unit = { | |
val lines = groupBlanksIterator(Source.fromFile("day6in.txt").getLines).toList | |
val partone = lines.map(countGroup (_)).sum | |
println(partone) | |
val parttwo = lines.map(countGroup2(_)).sum | |
println(parttwo) | |
} | |
def countGroup(group: List[String]) : Int = { | |
group.flatten.toSet.size | |
} | |
def countGroup2(group: List[String]) = { | |
group.flatten.groupBy(a => a).values.filter(b => b.size == group.size).size | |
} | |
def groupBlanksIterator(xs:Iterator[String]) = | |
new Iterator[List[String]] | |
{ def hasNext = xs.hasNext; def next = xs.takeWhile(_.nonEmpty).toList} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment