mirror of
https://github.com/mx42/adventofcode.git
synced 2026-01-14 05:49:52 +01:00
24 lines
457 B
Scala
24 lines
457 B
Scala
/**
|
|
* Usage: scala part2.scala < input
|
|
*/
|
|
|
|
import scala.io.StdIn
|
|
import scala.collection.Iterator
|
|
|
|
val items = Stream
|
|
.continually(StdIn.readLine)
|
|
.takeWhile(_ != null)
|
|
.map(Integer.parseInt)
|
|
.toList
|
|
|
|
println(
|
|
Stream
|
|
.continually(items.toStream)
|
|
.flatten
|
|
.scanLeft(0)(_ + _)
|
|
.scanLeft(Map.empty[Int, Int]){case (acc, item) => acc + (item -> (acc.getOrElse(item, 0) + 1)) }
|
|
.filter(_.count(_._2 == 2) > 0)
|
|
.map(_.maxBy(_._2))
|
|
.head
|
|
)
|