-
Notifications
You must be signed in to change notification settings - Fork 0
/
day2.scala
59 lines (51 loc) · 1.92 KB
/
day2.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
object Main extends App {
import scala.io.Source
import scala.annotation.tailrec
val filename2 = "input2.txt"
val input2 = Source.fromFile(filename2).getLines.toList
// part 1
def countLetters(s: String): (Int, Int) = {
val letters = s.distinct.toList
val count = letters.map(x => s.count(_ == x))
if (count.contains(2) && count.contains(3)) (1, 1)
else if (count.contains(2)) (1, 0)
else if (count.contains(3)) (0, 1)
else (0, 0)
}
val finalCounts = input2.map(countLetters(_)).foldLeft((0, 0)) { case ((acc1, acc2), (a, b)) => (acc1 + a, acc2 + b)}
println(finalCounts._1 * finalCounts._2)
// part 2
def compareStr(s1: String, s2: String): Boolean = {
@tailrec def compareStrAcc(xs1: String, xs2: String, diffs: Int): Boolean = {
xs1.toList match {
case Nil => true
case head :: _ if(head == xs2.head) => compareStrAcc(xs1.tail, xs2.tail, diffs)
case _ if (diffs == 1) => false
case _ => compareStrAcc(xs1.tail, xs2.tail, diffs + 1)
}
}
compareStrAcc(s1, s2, 0)
}
def findComm(ls: List[String]): String = {
@ tailrec def findComm_(x: String, xs: List[String], xsOr: List[String]): (String, String) = {
if (xs.isEmpty) findComm_(xsOr.head, xsOr.tail, xsOr.tail)
else {
if (compareStr(x, xs.head)) (x, xs.head)
else findComm_(x, xs.tail, xsOr)
}
}
def getCommon(s1: String, s2: String): String = {
@tailrec def getCommonAcc(xs1: String, xs2: String, common: List[Char]): String = {
xs1.toList match {
case Nil => common.mkString
case head :: _ if(head == xs2.head) => getCommonAcc(xs1.tail, xs2.tail, common ::: List(head))
case _ => getCommonAcc(xs1.tail, xs2.tail, common)
}
}
getCommonAcc(s1, s2, List())
}
val commons = findComm_(ls.head, ls.tail, ls.tail.tail)
getCommon(commons._1, commons._2)
}
println(findComm(input2))
}