Skip to content

Commit 131dca9

Browse files
committed
Add 2017/25
1 parent 00f87af commit 131dca9

File tree

3 files changed

+144
-0
lines changed

3 files changed

+144
-0
lines changed

year2017/day25/input.txt

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
Begin in state A.
2+
Perform a diagnostic checksum after 12656374 steps.
3+
4+
In state A:
5+
If the current value is 0:
6+
- Write the value 1.
7+
- Move one slot to the right.
8+
- Continue with state B.
9+
If the current value is 1:
10+
- Write the value 0.
11+
- Move one slot to the left.
12+
- Continue with state C.
13+
14+
In state B:
15+
If the current value is 0:
16+
- Write the value 1.
17+
- Move one slot to the left.
18+
- Continue with state A.
19+
If the current value is 1:
20+
- Write the value 1.
21+
- Move one slot to the left.
22+
- Continue with state D.
23+
24+
In state C:
25+
If the current value is 0:
26+
- Write the value 1.
27+
- Move one slot to the right.
28+
- Continue with state D.
29+
If the current value is 1:
30+
- Write the value 0.
31+
- Move one slot to the right.
32+
- Continue with state C.
33+
34+
In state D:
35+
If the current value is 0:
36+
- Write the value 0.
37+
- Move one slot to the left.
38+
- Continue with state B.
39+
If the current value is 1:
40+
- Write the value 0.
41+
- Move one slot to the right.
42+
- Continue with state E.
43+
44+
In state E:
45+
If the current value is 0:
46+
- Write the value 1.
47+
- Move one slot to the right.
48+
- Continue with state C.
49+
If the current value is 1:
50+
- Write the value 1.
51+
- Move one slot to the left.
52+
- Continue with state F.
53+
54+
In state F:
55+
If the current value is 0:
56+
- Write the value 1.
57+
- Move one slot to the left.
58+
- Continue with state E.
59+
If the current value is 1:
60+
- Write the value 1.
61+
- Move one slot to the right.
62+
- Continue with state A.

year2017/day25/part1.awk

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/usr/bin/awk -f
2+
3+
function extract_word(input, i, words) {
4+
patsplit(input, words, /[A-Za-z0-9]+/);
5+
return words[i];
6+
}
7+
8+
function direction_to_index_delta(direction) {
9+
if (direction == "right") return 1;
10+
if (direction == "left") return -1;
11+
12+
print "Error: invalid direction", direction;
13+
exit 1;
14+
}
15+
16+
BEGIN{RS="\n\n"}
17+
18+
/^In state/ {
19+
split($0, rows, "\n");
20+
21+
in_state = extract_word(rows[1], 3);
22+
23+
next_value_map[in_state, 0] = extract_word(rows[3], 4);
24+
next_index_delta_map[in_state, 0] = direction_to_index_delta(extract_word(rows[4], 6));
25+
next_state_map[in_state, 0] = extract_word(rows[5], 4);
26+
27+
next_value_map[in_state, 1] = extract_word(rows[7], 4);
28+
next_index_delta_map[in_state, 1] = direction_to_index_delta(extract_word(rows[8], 6));
29+
next_state_map[in_state, 1] = extract_word(rows[9], 4);
30+
31+
next;
32+
}
33+
34+
/^Begin in state/{
35+
split($0, rows, "\n");
36+
current_state = extract_word(rows[1], 4);
37+
38+
total_steps = extract_word(rows[2], 6);
39+
}
40+
41+
END{
42+
current_index = 0;
43+
44+
while (total_steps--) {
45+
current_value = tape[current_index] || 0;
46+
next_state = next_state_map[current_state, current_value];
47+
next_value = next_value_map[current_state, current_value];
48+
next_index_delta = next_index_delta_map[current_state, current_value];
49+
50+
tape[current_index] = next_value;
51+
current_state = next_state;
52+
current_index += next_index_delta;
53+
}
54+
55+
diagnostic_checksum = 0;
56+
for (i in tape) {
57+
diagnostic_checksum += tape[i];
58+
}
59+
print diagnostic_checksum;
60+
}

year2017/day25/sample.txt

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Begin in state A.
2+
Perform a diagnostic checksum after 6 steps.
3+
4+
In state A:
5+
If the current value is 0:
6+
- Write the value 1.
7+
- Move one slot to the right.
8+
- Continue with state B.
9+
If the current value is 1:
10+
- Write the value 0.
11+
- Move one slot to the left.
12+
- Continue with state B.
13+
14+
In state B:
15+
If the current value is 0:
16+
- Write the value 1.
17+
- Move one slot to the left.
18+
- Continue with state A.
19+
If the current value is 1:
20+
- Write the value 1.
21+
- Move one slot to the right.
22+
- Continue with state A.

0 commit comments

Comments
 (0)