-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproblem189.ox
55 lines (49 loc) · 1.15 KB
/
problem189.ox
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
// -*- Rust -*- (Syntax is pretty close)
const ROW_COUNT = 8;
// Naive power function (good enough for our use case)
fn ipow(a: int, b: int) -> int {
let mut result = 1;
for _ in 0..b {
result *= a;
}
return result;
}
fn make_vec(length: int) -> vec<int> {
let mut v = vec<int>[];
for _ in 0..length {
v.push(0);
}
return v;
}
fn sum(v: vec<int>) -> int {
let mut s: int = 0;
for i in v {
s += i;
}
return s;
}
fn main() {
let mut downward_colors = vec[1, 1, 1];
for n in 2..=ROW_COUNT {
let mut new_colors = make_vec(ipow(3, n));
for i in 0..ipow(3, n - 1) {
for j in 0..ipow(3, n) {
let mut product = 1;
let mut c = i;
let mut d = j;
for _ in 1..n {
let mut valid_colors = vec[1, 1, 1];
valid_colors[c % 3] = 0;
valid_colors[d % 3] = 0;
valid_colors[(d / 3) % 3] = 0;
c /= 3;
d /= 3;
product *= (valid_colors[0] + valid_colors[1] + valid_colors[2]);
}
new_colors[j] += downward_colors[i] * product;
}
}
downward_colors = new_colors;
}
println(sum(downward_colors) as str);
}