-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3.scala
33 lines (28 loc) · 991 Bytes
/
3.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
case class Cut(id: Int, x: Int, y: Int, width: Int, height: Int)
val pattern = raw"#(\d+) @ (\d+),(\d+): (\d+)x(\d+)".r
val cuts = io.Source.stdin.getLines
.map {
case pattern(id, x, y, width, height) => Cut(id.toInt, x.toInt, y.toInt, width.toInt, height.toInt)
}
.toList
val applied = cuts.foldLeft(Array.ofDim[Int](1000 , 1000))((matrix, cut) => {
matrix.zipWithIndex.map {
case (row, rowNumber) =>
if (rowNumber >= cut.y && rowNumber < cut.y + cut.height) {
row.slice(0, cut.x) ++ row.slice(cut.x, cut.x + cut.width).map(_ + 1) ++ row.drop(cut.x + cut.width)
} else {
row
}
}
})
println(applied.foldLeft(0)((sum, row) => sum + row.count(_ > 1)))
cuts
.filter(cut => applied.zipWithIndex.forall {
case (row, rowNumber) =>
if (rowNumber >= cut.y && rowNumber < cut.y + cut.height) {
row.slice(cut.x, cut.x + cut.width).forall(_ == 1)
} else {
true
}
})
.foreach(println)