-
Notifications
You must be signed in to change notification settings - Fork 0
/
TreeOfLife.scala
191 lines (173 loc) · 5.04 KB
/
TreeOfLife.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
//
// Problem: https://www.hackerrank.com/challenges/the-tree-of-life/problem
//
object TreeOfLife {
def main(args: Array[String]) {
val rule = collection.immutable.BitSet.fromBitMask(Array(readInt))
val tree = readLine.filter(_ != ' ').toUpperCase
val numQueries = readInt
var queries = List[String]()
for (i <- 1 to numQueries) queries :+= readLine
var current = 0
var currTree = deserialize(tree)
val trees = collection.mutable.ListBuffer[String]()
trees += serialize(currTree)
queries.foreach(query => {
val tmp = query.split(" ")
val iterations = tmp(0).toInt
val path = tmp(1)
//println("Query: iterations = %d, path = %s".format(iterations, path))
if (iterations == 0) {
println(getCellData(currTree, path))
} else if (iterations < 0) {
current += iterations
currTree = deserialize(trees(current))
println(getCellData(currTree, path))
}
if (iterations > 0) {
if (current + iterations < trees.size) {
current += iterations
currTree = deserialize(trees(current))
println(getCellData(currTree, path))
} else {
currTree = deserialize(trees(trees.size - 1))
for (i <- 1 to iterations) {
current += 1
if (current >= trees.size) {
evolve(currTree, rule)
trees += serialize(currTree)
}
}
println(getCellData(currTree, path))
}
}
})
}
sealed trait BTree
case object Empty extends BTree
case class Node(var data: Boolean,
var left: BTree,
var right: BTree,
var parent: BTree)
extends BTree {
var ndata = false
override def toString: String = {
val buf = new StringBuffer("Node(")
buf.append(data).append(",")
buf
.append(left match {
case Empty => Empty
case x: Node => x
})
.append(",")
.append(right match {
case Empty => Empty
case x: Node => x
})
buf.append(")")
buf.toString
}
}
def deserialize(tree: String): BTree = {
val stack = collection.mutable.Stack[Any]()
for (token <- tree) {
token match {
case '(' => stack.push('(')
case '.' => stack.push(Node(false, Empty, Empty, Empty))
case 'X' => stack.push(Node(true, Empty, Empty, Empty))
case ')' => {
val right = stack.pop.asInstanceOf[Node]
val parent = stack.pop.asInstanceOf[Node]
val left = stack.pop.asInstanceOf[Node]
val tmp = stack.pop
if (tmp.asInstanceOf[Char] != '(')
throw new Exception("Invalid Tree!")
parent.left = left
parent.right = right
left.parent = parent
right.parent = parent
stack.push(parent)
}
}
}
if (stack.size != 1) throw new Exception("Invalid Tree!!")
stack.pop.asInstanceOf[Node]
}
def serialize(btree: BTree): String = {
btree match {
case x: Node => {
val ldata = serialize(x.left)
val rdata = serialize(x.right)
val buf = new StringBuffer("")
buf
.append(if (ldata.isEmpty) ldata else "(" + ldata)
.append(if (x.data) "X" else ".")
.append(if (rdata.isEmpty) rdata else rdata + ")")
buf.toString
}
case Empty => ""
}
}
def cellNeighbourhoodValue(node: BTree): Int = {
def cellValue(cell: BTree): Int = {
cell match {
case Empty => 0
case x: Node => if (x.data) 1 else 0
}
}
node match {
case cell: Node => {
val cellCurr = cellValue(cell)
val cellParent = cellValue(cell.parent)
val cellLeft = cellValue(cell.left)
val cellRight = cellValue(cell.right)
Integer.parseInt(
"%d%d%d%d".format(cellParent, cellLeft, cellCurr, cellRight),
2)
}
case Empty => 0
}
}
def getNode(node: BTree, path: String) = {
var curr = node.asInstanceOf[Node]
path.foreach(x =>
x match {
case '<' => curr = curr.left.asInstanceOf[Node]
case '>' => curr = curr.right.asInstanceOf[Node]
case _ => curr
})
curr
}
def getCellData(node: BTree, path: String) = {
val tmp = getNode(node, path)
if (tmp.data) "X" else "."
}
def nextCellValue(node: BTree, rule: collection.immutable.BitSet): Boolean = {
val currValue = cellNeighbourhoodValue(node)
rule(currValue)
}
def evolve(tree: BTree, rule: collection.immutable.BitSet) {
def evolveNode(tree: BTree) {
tree match {
case x: Node => {
x.ndata = nextCellValue(x, rule)
evolveNode(x.left)
evolveNode(x.right)
}
case Empty => ()
}
}
def commit(tree: BTree) {
tree match {
case x: Node => {
x.data = x.ndata
commit(x.left)
commit(x.right)
}
case Empty => ()
}
}
evolveNode(tree)
commit(tree)
}
}