2018 day 1

This commit is contained in:
Aly 2018-12-01 10:21:28 -08:00
parent f5b37afa96
commit da03fe8314
No known key found for this signature in database
GPG Key ID: 555B7346639DDAC3
5 changed files with 42 additions and 8 deletions

View File

@ -7,6 +7,7 @@ object Main {
val years = Map(
"2016" -> y2016.!,
"2017" -> y2017.!,
"2018" -> y2018.!,
)
def main(args: Array[String]): Unit = {

View File

@ -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)
}
}

View File

@ -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 = {

View 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
}
}

View File

@ -0,0 +1,9 @@
package aoc
package object y2018 extends Year {
override def days: Map[String, Day] = Map(
"1" -> Day01
)
}