Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 12 additions & 41 deletions 2015/src/main/scala/Day05.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,26 @@ import scala.io.*

object Day05 extends App:

val day: String =
this.getClass.getName.drop(3).init

val start1: Long =
System.currentTimeMillis
val day: String = getClass.getName.filter(_.isDigit).mkString

val strings: List[String] =
Source
.fromResource(s"input$day.txt")
.getLines
.toList

val answer1: Int =
val valid = for {
str <- strings
if str.count("aeiou".toList.contains) >= 3
if str.sliding(2).map(_.toArray).count(cs => cs(0) == cs(1)) >= 1
if str.sliding(2).count(cs => cs == "ab" || cs == "cd" || cs == "pq" || cs == "xy") == 0
} yield str
valid.size
val vowels = "[aeiou].*[aeiou].*[aeiou]".r.unanchored
val pair = "(.)\\1".r.unanchored
val naughty = "ab|cd|pq|xy".r.unanchored

val start1: Long = System.currentTimeMillis
val answer1: Int = strings.count(line => vowels.matches(line) && pair.matches(line) && !naughty.matches(line))
println(s"Answer AOC 2015 day $day part 1: ${answer1} [${System.currentTimeMillis - start1}ms]")

val start2: Long =
System.currentTimeMillis

val answer2: Int =
def containsTwoOrMoreNonOverlappingPair(str: String): Boolean =
def hasNonOverlappingPair(pair: String, todo: String, foundFirst: Boolean = false): Boolean =
val index = todo.indexOf(pair)
if (index != -1 && !foundFirst) hasNonOverlappingPair(pair, todo.drop(index + 2), true)
else if (index != -1 && foundFirst) true
else false

(str.sliding(2).distinct).exists(pair => hasNonOverlappingPair(pair, str))

def containsTwoSameCharsOneCharApart(str: String): Boolean =
def hasTwoSameCharsOneCharApart(todo: List[Char]): Boolean =
todo match
case c0 :: _ :: c2 :: tail if c0 == c2 => true
case c0 :: c1 :: c2 :: tail => hasTwoSameCharsOneCharApart(c1 :: c2 :: tail)
case _ => false
hasTwoSameCharsOneCharApart(str.toList)

val filter = for {
str <- strings
if containsTwoOrMoreNonOverlappingPair(str)
if containsTwoSameCharsOneCharApart(str)
} yield str
filter.size


val twoPair = "(..).*\\1".r.unanchored
val triple = "(.).\\1".r.unanchored

val start2: Long = System.currentTimeMillis
val answer2: Int = strings.count(line => twoPair.matches(line) && triple.matches(line))
println(s"Answer AOC 2015 day $day part 2: ${answer2} [${System.currentTimeMillis - start2}ms]")