Day 2
This commit is contained in:
parent
089c7f34db
commit
ee395cd25a
2 changed files with 47 additions and 0 deletions
1
input/day02
Normal file
1
input/day02
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
990244-1009337,5518069-5608946,34273134-34397466,3636295061-3636388848,8613701-8663602,573252-688417,472288-533253,960590-988421,7373678538-7373794411,178-266,63577667-63679502,70-132,487-1146,666631751-666711926,5896-10827,30288-52204,21847924-21889141,69684057-69706531,97142181-97271487,538561-555085,286637-467444,93452333-93519874,69247-119122,8955190262-8955353747,883317-948391,8282803943-8282844514,214125-236989,2518-4693,586540593-586645823,137643-211684,33-47,16210-28409,748488-837584,1381-2281,1-19
|
||||||
46
src/day02.scala
Normal file
46
src/day02.scala
Normal file
|
|
@ -0,0 +1,46 @@
|
||||||
|
package aoc
|
||||||
|
package day02
|
||||||
|
|
||||||
|
import scala.annotation.tailrec
|
||||||
|
|
||||||
|
val dayNumber = "02"
|
||||||
|
|
||||||
|
@main def part1: Unit =
|
||||||
|
println(part1(loadInput(dayNumber)))
|
||||||
|
|
||||||
|
@main def part2: Unit =
|
||||||
|
println(part2(loadInput(dayNumber)))
|
||||||
|
|
||||||
|
def part1(input: String): String =
|
||||||
|
|
||||||
|
def isValid(id: String): Boolean =
|
||||||
|
if id.length % 2 == 0 then
|
||||||
|
val (left, right) = id.splitAt(id.length / 2)
|
||||||
|
left != right
|
||||||
|
else true
|
||||||
|
|
||||||
|
input
|
||||||
|
.split(",")
|
||||||
|
.map { case s"$from-$to" => from.toLong to to.toLong }
|
||||||
|
.map(_.filterNot(id => isValid(id.toString)).sum)
|
||||||
|
.sum
|
||||||
|
.toString
|
||||||
|
|
||||||
|
def part2(input: String): String =
|
||||||
|
|
||||||
|
def isValid(id: Long): Boolean =
|
||||||
|
@tailrec
|
||||||
|
def rec(id: String, groupsCount: Int): Boolean =
|
||||||
|
if groupsCount < 1 then true
|
||||||
|
else if id.length % groupsCount == 0 then
|
||||||
|
id.grouped(groupsCount).distinct.length > 1 && rec(id, groupsCount - 1)
|
||||||
|
else rec(id, groupsCount - 1)
|
||||||
|
|
||||||
|
rec(id.toString, id.toString.length - 1)
|
||||||
|
|
||||||
|
input
|
||||||
|
.split(",")
|
||||||
|
.map { case s"$from-$to" => from.toLong to to.toLong }
|
||||||
|
.map(_.filterNot(isValid).sum)
|
||||||
|
.sum
|
||||||
|
.toString
|
||||||
Loading…
Add table
Add a link
Reference in a new issue