-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day25.js
96 lines (83 loc) · 1.26 KB
/
Day25.js
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
let state = 'A';
let index = 0;
let tape = [0];
let count = 0;
function incTape() {
index++;
if(index === tape.length) tape.push(0);
}
function decTape() {
index--;
if(index < 0) {
tape.unshift(0);
index = 0;
}
}
while(count < 12399302) {
switch(state) {
case 'A':
if(tape[index] === 0) {
tape[index] = 1;
state = "B";
}
else {
tape[index] = 0;
state = "C";
}
incTape();
break;
case 'B':
if(tape[index] === 0) {
tape[index] = 0;
state = "A";
decTape();
}
else {
tape[index] = 0;
state = "D";
incTape();
}
break;
case 'C':
if(tape[index] === 0) state = 'D';
else state = 'A';
tape[index] = 1;
incTape();
break;
case 'D':
if(tape[index] === 0) {
tape[index] = 1;
state = 'E';
}
else {
tape[index] = 0;
state = 'D';
}
decTape();
break;
case 'E':
if(tape[index] === 0) {
tape[index] = 1;
state = 'F';
incTape();
}
else {
tape[index] = 1;
state = 'B';
decTape();
}
break;
case 'F':
if(tape[index] === 0) state = 'A';
else state = 'E';
tape[index] = 1;
incTape();
break;
}
count++;
}
let ones = 0;
tape.forEach(function(val) {
if(val === 1) ones++;
});
console.log("Answer 1: " + ones);