Skip to content
This repository was archived by the owner on Mar 24, 2022. It is now read-only.

Commit 36a2e1c

Browse files
committed
Add Challenge #213[Easy]
1 parent cb46649 commit 36a2e1c

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

Java/Easy/CARule90.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Created by /u/jorgegil96 on Reddit
2+
// Challenge #213 [Easy] Cellular Automata: Rule 90
3+
// https://www.reddit.com/r/dailyprogrammer/comments/3jz8tt/20150907_challenge_213_easy_cellular_automata/
4+
5+
public class CARule90 {
6+
public static void states(String input, StringBuilder sb) {
7+
int before, after;
8+
for (int i = 0; i < input.length(); i++) {
9+
before = 0;
10+
after = 0;
11+
if (i == 0) {
12+
after = Character.getNumericValue(input.charAt(i + 1));
13+
}
14+
else if (i == input.length() - 1) {
15+
before = Character.getNumericValue(input.charAt(i - 1));
16+
}
17+
else {
18+
before = Character.getNumericValue(input.charAt(i - 1));
19+
after = Character.getNumericValue(input.charAt(i + 1));
20+
}
21+
if ((before + after) % 2 == 0) {
22+
System.out.print(" ");
23+
sb.setCharAt(i, '0');
24+
}
25+
else{
26+
System.out.print("X");
27+
sb.setCharAt(i, '1');
28+
}
29+
}
30+
}
31+
32+
public static void main(String[] args) {
33+
String input = "00000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000";
34+
StringBuilder sb = new StringBuilder(input);
35+
36+
int n = 25;
37+
38+
for (int i = 0; i < input.length(); i++)
39+
if (input.charAt(i) == '0')
40+
System.out.print(' ');
41+
else
42+
System.out.print('X');
43+
44+
System.out.println("");
45+
46+
for (int i = 0; i < n; i++){
47+
states(sb.toString(), sb);
48+
if (!sb.toString().contains("1")){
49+
break;
50+
}
51+
System.out.println("");
52+
}
53+
54+
}
55+
}

0 commit comments

Comments
 (0)