2018 day 1
This commit is contained in:
parent
f5b37afa96
commit
da03fe8314
5 changed files with 42 additions and 8 deletions
|
@ -7,6 +7,7 @@ object Main {
|
|||
val years = Map(
|
||||
"2016" -> y2016.!,
|
||||
"2017" -> y2017.!,
|
||||
"2018" -> y2018.!,
|
||||
)
|
||||
|
||||
def main(args: Array[String]): Unit = {
|
||||
|
|
|
@ -22,4 +22,12 @@ package object aoc {
|
|||
|
||||
}
|
||||
|
||||
def uniquePrefix[A](as: Stream[A]): Stream[A] = {
|
||||
def go(as: Stream[A], seen: Set[A]): Stream[A] = as match {
|
||||
case h #:: t if !seen(h) => h #:: go(t, seen + h)
|
||||
case _ => Stream.empty[A]
|
||||
}
|
||||
go(as, Set.empty)
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -4,14 +4,6 @@ import aoc._
|
|||
|
||||
object Day06 extends Day {
|
||||
|
||||
def uniquePrefix[A](as: Stream[A]): Stream[A] = {
|
||||
def go(as: Stream[A], seen: Set[A]): Stream[A] = as match {
|
||||
case h #:: t if !seen(h) => h #:: go(t, seen + h)
|
||||
case _ => Stream.empty[A]
|
||||
}
|
||||
go(as, Set.empty)
|
||||
}
|
||||
|
||||
case class State(blocks: List[Int]) {
|
||||
|
||||
def step: State = {
|
||||
|
|
24
aoc/src/main/scala/aoc/y2018/Day01.scala
Normal file
24
aoc/src/main/scala/aoc/y2018/Day01.scala
Normal file
|
@ -0,0 +1,24 @@
|
|||
package aoc.y2018
|
||||
|
||||
import aoc._
|
||||
|
||||
object Day01 extends Day {
|
||||
|
||||
override def part1(input: String): String = {
|
||||
input.lines.map(_.toInt).sum.toString
|
||||
}
|
||||
|
||||
override def part2(input: String): String = {
|
||||
println(input.lines.size)
|
||||
val i: List[Int] = input.lines.map(_.toInt).toList
|
||||
val inet = i.zipWithIndex.map {
|
||||
case (f, index) => f + i.take(index).sum
|
||||
}
|
||||
val net = i.sum
|
||||
val fcs = 0 #:: Stream.iterate(inet)(cl => cl.map(_ + net)).flatten
|
||||
val us = uniquePrefix(fcs)
|
||||
val nf = fcs(us.size)
|
||||
nf.toString
|
||||
}
|
||||
|
||||
}
|
9
aoc/src/main/scala/aoc/y2018/package.scala
Normal file
9
aoc/src/main/scala/aoc/y2018/package.scala
Normal file
|
@ -0,0 +1,9 @@
|
|||
package aoc
|
||||
|
||||
package object y2018 extends Year {
|
||||
|
||||
override def days: Map[String, Day] = Map(
|
||||
"1" -> Day01
|
||||
)
|
||||
|
||||
}
|
Loading…
Reference in a new issue