-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.scala
More file actions
53 lines (51 loc) · 1.68 KB
/
solution.scala
File metadata and controls
53 lines (51 loc) · 1.68 KB
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
val instructions = scala.io.Source.fromFile("data.txt").getLines.toList
// Define display size
val ROW = 6
val COL = 50
// Define regex
val rectReg = """rect (\d+)x(\d+)"""r
val rotColReg = """rotate column x=(\d+) by (\d+)"""r
val rotRowReg = """rotate row y=(\d+) by (\d+)"""r
def rect(x: Int, y: Int, display: List[(Int, Int)]): List[(Int,Int)] = {
val newLights = (0 until x).flatMap(i => (0 until y).map(j => (i,j))).toList
(newLights ::: display).distinct
}
def rotCol(col: Int, by: Int, display: List[(Int, Int)]): List[(Int,Int)] = {
display.map(i => {
if(col == i._1) {
(i._1, (i._2 + by) % ROW)
} else { i }
})
}
def rotRow(row: Int, by: Int, display: List[(Int, Int)]): List[(Int,Int)] = {
display.map(i => {
if(row == i._2) {
((i._1 + by) % COL, i._2)
} else { i }
})
}
def doInstruction(instructions: List[String], display: List[(Int,Int)]): List[(Int, Int)] = {
if(instructions.length == 0){
return display
}
val newDisplay: List[(Int,Int)] = instructions.head match {
case rectReg(d1,d2) => rect(d1.toInt, d2.toInt, display)
case rotColReg(d1,d2) => rotCol(d1.toInt, d2.toInt, display)
case rotRowReg(d1,d2) => rotRow(d1.toInt, d2.toInt, display)
case _ => {
println(s"No match...")
display
}
}
doInstruction(instructions.tail, newDisplay)
}
val code = doInstruction(instructions, List())
println(code.length)
// Part 2
val display = (0 until COL).flatMap(i => (0 until ROW).map(j => (i,j))).toList
val lines = display.groupBy(x => x._2)
for(i <- 0 until ROW) {
val line: List[String] = lines(i).map(px => if(code.contains(px)){"#"}else{" "})
val lineStr = line.reduce((acc, curr) => acc + curr)
println(lineStr)
}