Skip to content

Commit 12e5f38

Browse files
committed
Day 25 initial commit
1 parent 1cf94e4 commit 12e5f38

File tree

1 file changed

+202
-0
lines changed

1 file changed

+202
-0
lines changed

src/day25/Main.java

Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
package day25;
2+
3+
import java.util.HashMap;
4+
5+
public class Main {
6+
static boolean[] tape = new boolean[1];
7+
static HashMap<Integer, State> states = new HashMap<>(); // A = 0,.., F = 5
8+
static State currentState;
9+
static int steps = 12261543;
10+
static int currentPos = 0;
11+
12+
public static void main(String[] args) {
13+
states.put(0, new A());
14+
states.put(1, new B());
15+
states.put(2, new C());
16+
states.put(3, new D());
17+
states.put(4, new E());
18+
states.put(5, new F());
19+
20+
currentState = states.get(0);
21+
22+
for (int i = 0; i < steps; i++) {
23+
currentState = states.get(currentState.move());
24+
}
25+
26+
int counter = 0;
27+
28+
for(boolean b : tape){
29+
if(b)
30+
counter++;
31+
}
32+
33+
System.out.println(counter);
34+
}
35+
36+
static void goLeft() {
37+
currentPos--;
38+
if (currentPos < 0) {
39+
boolean[] newTape = new boolean[tape.length + 1];
40+
System.arraycopy(tape, 0, newTape, 1, tape.length);
41+
tape = newTape;
42+
currentPos++;
43+
}
44+
}
45+
46+
static void goRight() {
47+
currentPos++;
48+
if (currentPos > tape.length - 1) {
49+
boolean[] newTape = new boolean[tape.length + 1];
50+
System.arraycopy(tape, 0, newTape, 0, tape.length);
51+
tape = newTape;
52+
}
53+
}
54+
}
55+
56+
interface State {
57+
int move();
58+
}
59+
60+
class A implements State {
61+
62+
// If the current value is 0:
63+
// - Write the value 1.
64+
// - Move one slot to the right.
65+
// - Continue with state B.
66+
// If the current value is 1:
67+
// - Write the value 0.
68+
// - Move one slot to the left.
69+
// - Continue with state C.
70+
71+
public int move() {
72+
if (!Main.tape[Main.currentPos]) {
73+
Main.tape[Main.currentPos] = true;
74+
Main.goRight();
75+
return 1;
76+
} else {
77+
Main.tape[Main.currentPos] = false;
78+
Main.goLeft();
79+
return 2;
80+
}
81+
}
82+
}
83+
84+
class B implements State {
85+
86+
// If the current value is 0:
87+
// - Write the value 1.
88+
// - Move one slot to the left.
89+
// - Continue with state A.
90+
// If the current value is 1:
91+
// - Write the value 1.
92+
// - Move one slot to the right.
93+
// - Continue with state C.
94+
95+
public int move() {
96+
if (!Main.tape[Main.currentPos]) {
97+
Main.tape[Main.currentPos] = true;
98+
Main.goLeft();
99+
return 0;
100+
} else {
101+
Main.tape[Main.currentPos] = true;
102+
Main.goRight();
103+
return 2;
104+
}
105+
}
106+
}
107+
108+
class C implements State {
109+
110+
// If the current value is 0:
111+
// - Write the value 1.
112+
// - Move one slot to the right.
113+
// - Continue with state A.
114+
// If the current value is 1:
115+
// - Write the value 0.
116+
// - Move one slot to the left.
117+
// - Continue with state D.
118+
119+
public int move() {
120+
if (!Main.tape[Main.currentPos]) {
121+
Main.tape[Main.currentPos] = true;
122+
Main.goRight();
123+
return 0;
124+
} else {
125+
Main.tape[Main.currentPos] = false;
126+
Main.goLeft();
127+
return 3;
128+
}
129+
}
130+
}
131+
132+
class D implements State {
133+
134+
// If the current value is 0:
135+
// - Write the value 1.
136+
// - Move one slot to the left.
137+
// - Continue with state E.
138+
// If the current value is 1:
139+
// - Write the value 1.
140+
// - Move one slot to the left.
141+
// - Continue with state C.
142+
143+
public int move() {
144+
if (!Main.tape[Main.currentPos]) {
145+
Main.tape[Main.currentPos] = true;
146+
Main.goLeft();
147+
return 4;
148+
} else {
149+
Main.tape[Main.currentPos] = true;
150+
Main.goLeft();
151+
return 2;
152+
}
153+
}
154+
}
155+
156+
class E implements State {
157+
158+
// If the current value is 0:
159+
// - Write the value 1.
160+
// - Move one slot to the right.
161+
// - Continue with state F.
162+
// If the current value is 1:
163+
// - Write the value 1.
164+
// - Move one slot to the right.
165+
// - Continue with state A.
166+
167+
public int move() {
168+
if (!Main.tape[Main.currentPos]) {
169+
Main.tape[Main.currentPos] = true;
170+
Main.goRight();
171+
return 5;
172+
} else {
173+
Main.tape[Main.currentPos] = true;
174+
Main.goRight();
175+
return 0;
176+
}
177+
}
178+
}
179+
180+
class F implements State {
181+
182+
// If the current value is 0:
183+
// - Write the value 1.
184+
// - Move one slot to the right.
185+
// - Continue with state A.
186+
// If the current value is 1:
187+
// - Write the value 1.
188+
// - Move one slot to the right.
189+
// - Continue with state E.
190+
191+
public int move() {
192+
if (!Main.tape[Main.currentPos]) {
193+
Main.tape[Main.currentPos] = true;
194+
Main.goRight();
195+
return 0;
196+
} else {
197+
Main.tape[Main.currentPos] = true;
198+
Main.goRight();
199+
return 4;
200+
}
201+
}
202+
}

0 commit comments

Comments
 (0)