-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay14.scala
47 lines (41 loc) · 1.42 KB
/
Day14.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
import scala.io.Source
import scala.math
object Day14 {
val input = Source.fromFile("./input/input14.txt").getLines().toArray
val one = scala.collection.mutable.Map[Long,Long]()
val two = scala.collection.mutable.Map[Long,Long]()
var bitmask = ""
def float: List[Char] => Iterator[List[Char]] = {
case 'X' :: tail => float('0' :: tail) ++ float('1' :: tail)
case b :: tail => float(tail).map(b :: _)
case Nil => Iterator(Nil)
}
for(l <- input) l match {
case s"mask = ${mask}" => bitmask = mask
case s"mem[${index}] = ${number}" => {
val i = index.toInt
val n = number.toLong.toBinaryString
one(i) = java.lang.Long.parseLong(
bitmask.zip("0" * (bitmask.length - n.length) + n).map{
case ('X', c) => c
case (b, c) => b
}.mkString,
2
)
val d = i.toBinaryString
for(j <- float( bitmask.zip("0" * (bitmask.length - d.length) + d)
.map{
case ('0', c) => c
case (b, c) => b
}.toList
)
.map(_.mkString)
.map(java.lang.Long.parseLong(_, 2).toLong)
) {
two(j) = number.toLong
}
}
}
println(one.values.sum)
println(two.values.sum)
}