-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.scala
164 lines (132 loc) · 4.47 KB
/
main.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import scala.io.Source
case class Point3D(var x: Int, var y: Int, var z: Int)
case class Block(id: String, start: Point3D, end: Point3D)
object Main {
def parsePoint(line: String): Point3D = {
val coords = line.split(",")
Point3D(coords(0).toInt, coords(1).toInt, coords(2).toInt)
}
def parseBlock(id: String, line: String): Block = {
val split = line.split("~")
Block(id, parsePoint(split(0)), parsePoint(split(1)))
}
def parseFile(filePath: String): List[Block] = {
try {
Source.fromFile(filePath).getLines().zipWithIndex.map {
case (line, index) => parseBlock(f"$index%04d", line)
}.toList
} catch {
case e: Exception => {
println("Error reading file: " + e.getMessage)
List()
}
}
}
def fallOn(b: Block, onto: Block) {
val fallHeight = (b.start.z - onto.end.z) - 1
b.start.z -= fallHeight
b.end.z -= fallHeight
}
def intersects_in_z_axis(b1: Block, b2: Block): Boolean = {
!(b1.start.x > b2.end.x ||
b1.end.x < b2.start.x ||
b1.start.y > b2.end.y ||
b1.end.y < b2.start.y)
}
def onTop(block: Block, onTopOf: Block): Boolean = {
onTopOf.end.z + 1 == block.start.z
}
def simulateFalling(blocks: List[Block]) {
var blocksInAir = blocks.sortWith( (a, b) => a.start.z < b.start.z );
// we add a single ground block to simplifiy the calculations
val groundBlock = Block("-", Point3D(0, 0, 0), Point3D(9,9, 0))
var fallenBlocks = List(groundBlock)
while (blocksInAir.size > 0) {
blocksInAir match {
case head :: tail =>
val highestBelowBlock = fallenBlocks
.filter(b => intersects_in_z_axis(head, b))
.reduce( (a,b) => if (a.end.z > b.end.z) a else b )
fallOn(head, highestBelowBlock)
blocksInAir = tail
fallenBlocks = fallenBlocks :+ head
}
}
}
def supportedBy(block: Block, supportedBy: Block): Boolean = {
intersects_in_z_axis(block, supportedBy) && onTop(block, supportedBy)
}
def supportedByBlocks(block: Block, blocks: List[Block]): List[Block] = {
blocks.filter(b => supportedBy(block, b))
}
def supportsBlocks(block: Block, blocks: List[Block]): List[Block] = {
blocks.filter(b => supportedBy(b, block))
}
def canDisintegrate(block: Block, blocks: List[Block]): Boolean = {
blocks
.filter(b => supportedBy(b, block)
&& supportedByBlocks(b, blocks).size == 1)
.size == 0
}
def print_xz_axis(blocks: List[Block]) {
val max_x = blocks.map { _.end.x }.reduce( (a,b) => if (a > b) a else b );
val max_z = blocks.map { _.end.z }.reduce( (a,b) => if (a > b) a else b );
val array = Array.fill(max_z + 1, max_x + 1)("|....|")
blocks.foreach(b => {
for (x <- b.start.x to b.end.x) {
for (z <- b.start.z to b.end.z) {
array(z)(x) = "|" + b.id + "|"
}
}
})
for (row <- array.reverse) {
for (char <- row) {
print(char)
}
println()
}
}
def print_yz_axis(blocks: List[Block]) {
val max_y = blocks.map { _.end.y }.reduce( (a,b) => if (a > b) a else b );
val max_z = blocks.map { _.end.z }.reduce( (a,b) => if (a > b) a else b );
val array = Array.fill(max_z + 1, max_y + 1)("|....|")
blocks.foreach(b => {
for (y <- b.start.y to b.end.y) {
for (z <- b.start.z to b.end.z) {
array(z)(y) = "|" + b.id + "|"
}
}
})
for (row <- array.reverse) {
for (char <- row) {
print(char)
}
println()
}
}
def solve(blocks: List[Block]) {
val groundBlock = Block("----", Point3D(0, 0, 0), Point3D(9, 9, 0))
simulateFalling(blocks)
val canDisintegrateBlocks = blocks.filter(b => canDisintegrate(b, blocks)).map( b => b.id)
val count = canDisintegrateBlocks.size
println(count)
}
def solve2(blocks: List[Block]) {
simulateFalling(blocks)
var sum = 0;
blocks.foreach(b => {
// need to deep copy
val otherBlocks = blocks.filter(c => c != b)
val otherBlocksCopy = otherBlocks.map( b => Block(b.id, Point3D(b.start.x, b.start.y, b.start.z), Point3D(b.end.x, b.end.y, b.end.z)))
simulateFalling(otherBlocksCopy)
val fallenBlocks = otherBlocksCopy.zip(otherBlocks).count { case (b1, b2) => b1.start.z != b2.start.z }
sum += fallenBlocks
})
println(sum)
}
def main(args: Array[String]): Unit = {
val blocks = parseFile("input")
solve(blocks)
solve2(blocks)
}
}