mirror of
https://github.com/mx42/adventofcode.git
synced 2026-01-14 05:49:52 +01:00
Adding day 2
This commit is contained in:
18
day2/part1.scala
Normal file
18
day2/part1.scala
Normal file
@@ -0,0 +1,18 @@
|
||||
import scala.io.StdIn._
|
||||
|
||||
val res = Iterator
|
||||
.continually(readLine)
|
||||
.takeWhile(_ != null)
|
||||
.toList
|
||||
.map {
|
||||
entry => val counts = entry
|
||||
.groupBy(_.charValue)
|
||||
.mapValues(_.length)
|
||||
.values
|
||||
(counts.find(_ == 2), counts.find(_ == 3))
|
||||
}
|
||||
.foldLeft((0, 0)) {
|
||||
(acc, item) => (acc._1 + item._1.map(_ => 1).getOrElse(0),
|
||||
acc._2 + item._2.map(_ => 1).getOrElse(0)) }
|
||||
|
||||
println(res._1 * res._2)
|
||||
16
day2/part2.scala
Normal file
16
day2/part2.scala
Normal file
@@ -0,0 +1,16 @@
|
||||
import scala.io.StdIn._
|
||||
|
||||
|
||||
val items = Iterator
|
||||
.continually(readLine)
|
||||
.takeWhile(_ != null)
|
||||
.toList
|
||||
|
||||
val res = items
|
||||
.combinations(2)
|
||||
.map(x => x(0) zip x(1))
|
||||
.dropWhile(_.count(t => t._1 != t._2) != 1)
|
||||
.map(_.filter(t => t._1 == t._2).map(_._1))
|
||||
.next
|
||||
|
||||
println(res.mkString(""))
|
||||
Reference in New Issue
Block a user