@@ -2,55 +2,26 @@ import scala.io.*
22
33object Day05 extends App :
44
5- val day : String =
6- this .getClass.getName.drop(3 ).init
7-
8- val start1 : Long =
9- System .currentTimeMillis
5+ val day : String = getClass.getName.filter(_.isDigit).mkString
106
117 val strings : List [String ] =
128 Source
139 .fromResource(s " input $day.txt " )
1410 .getLines
1511 .toList
1612
17- val answer1 : Int =
18- val valid = for {
19- str <- strings
20- if str.count(" aeiou" .toList.contains) >= 3
21- if str.sliding(2 ).map(_.toArray).count(cs => cs(0 ) == cs(1 )) >= 1
22- if str.sliding(2 ).count(cs => cs == " ab" || cs == " cd" || cs == " pq" || cs == " xy" ) == 0
23- } yield str
24- valid.size
13+ val vowels = " [aeiou].*[aeiou].*[aeiou]" .r.unanchored
14+ val pair = " (.)\\ 1" .r.unanchored
15+ val naughty = " ab|cd|pq|xy" .r.unanchored
2516
17+ val start1 : Long = System .currentTimeMillis
18+ val answer1 : Int = strings.count(line => vowels.matches(line) && pair.matches(line) && ! naughty.matches(line))
2619 println(s " Answer AOC 2015 day $day part 1: ${answer1} [ ${System .currentTimeMillis - start1}ms] " )
2720
28- val start2 : Long =
29- System .currentTimeMillis
30-
31- val answer2 : Int =
32- def containsTwoOrMoreNonOverlappingPair (str : String ): Boolean =
33- def hasNonOverlappingPair (pair : String , todo : String , foundFirst : Boolean = false ): Boolean =
34- val index = todo.indexOf(pair)
35- if (index != - 1 && ! foundFirst) hasNonOverlappingPair(pair, todo.drop(index + 2 ), true )
36- else if (index != - 1 && foundFirst) true
37- else false
38-
39- (str.sliding(2 ).distinct).exists(pair => hasNonOverlappingPair(pair, str))
40-
41- def containsTwoSameCharsOneCharApart (str : String ): Boolean =
42- def hasTwoSameCharsOneCharApart (todo : List [Char ]): Boolean =
43- todo match
44- case c0 :: _ :: c2 :: tail if c0 == c2 => true
45- case c0 :: c1 :: c2 :: tail => hasTwoSameCharsOneCharApart(c1 :: c2 :: tail)
46- case _ => false
47- hasTwoSameCharsOneCharApart(str.toList)
48-
49- val filter = for {
50- str <- strings
51- if containsTwoOrMoreNonOverlappingPair(str)
52- if containsTwoSameCharsOneCharApart(str)
53- } yield str
54- filter.size
55-
21+
22+ val twoPair = " (..).*\\ 1" .r.unanchored
23+ val triple = " (.).\\ 1" .r.unanchored
24+
25+ val start2 : Long = System .currentTimeMillis
26+ val answer2 : Int = strings.count(line => twoPair.matches(line) && triple.matches(line))
5627 println(s " Answer AOC 2015 day $day part 2: ${answer2} [ ${System .currentTimeMillis - start2}ms] " )
0 commit comments