mirror of
https://github.com/mx42/adventofcode.git
synced 2026-01-14 13:59:51 +01:00
Adding day 1
This commit is contained in:
12
day1/part1.scala
Normal file
12
day1/part1.scala
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
/**
|
||||||
|
* Usage: scala part1.scala < input
|
||||||
|
*/
|
||||||
|
|
||||||
|
import scala.io.StdIn
|
||||||
|
import scala.collection.Iterator
|
||||||
|
|
||||||
|
println(Iterator
|
||||||
|
.continually(StdIn.readLine)
|
||||||
|
.takeWhile(_ != null)
|
||||||
|
.map(Integer.parseInt)
|
||||||
|
.sum)
|
||||||
23
day1/part2.scala
Normal file
23
day1/part2.scala
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
/**
|
||||||
|
* 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
|
||||||
|
)
|
||||||
Reference in New Issue
Block a user