-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay6.scala
43 lines (28 loc) · 1.08 KB
/
Day6.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import scala.io._
import scala.collection.immutable.Set
object day6
{
def main(args:Array[String]):Unit ={
val aoc = io.Source.fromFile("inputs/06-input.txt").getLines.mkString
def getDistinctCharactersIndex(datastream: String, distinctnumber: Int): Int = {
var distinctCharactersIndex = -1
for (i <- 0 until datastream.length) {
if (datastream.substring(i, i + distinctnumber).toSet.size == distinctnumber) {
distinctCharactersIndex = i + distinctnumber
return distinctCharactersIndex
}
}
return distinctCharactersIndex
}
def funcgetDistinctCharactersIndex(datastream: String, distinctnumber: Int): Int = {
val distinctCharactersIndex = (0 until datastream.length).find { i =>
datastream.substring(i, i + distinctnumber).toSet.size == distinctnumber
}.map { i => i + distinctnumber }.getOrElse(-1)
return distinctCharactersIndex
}
println(getDistinctCharactersIndex(aoc,4))
println(getDistinctCharactersIndex(aoc,14))
println(funcgetDistinctCharactersIndex(aoc,4))
println(funcgetDistinctCharactersIndex(aoc,14))
}
}