Skip to content

Instantly share code, notes, and snippets.

@sangkeon
Last active January 8, 2021 15:58
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 sangkeon/3e9ba1592baeafbd0ecce37a09a12a1c to your computer and use it in GitHub Desktop.
Save sangkeon/3e9ba1592baeafbd0ecce37a09a12a1c to your computer and use it in GitHub Desktop.
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