Skip to content

Commit d14a6c0

Browse files
committed
Year 2017 Day 25
1 parent 45a2f91 commit d14a6c0

File tree

4 files changed

+130
-0
lines changed

4 files changed

+130
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -172,3 +172,4 @@ The minimal SBT project provides:
172172
| 22 | [Sporifica Virus](https://adventofcode.com/2017/day/22) | [Source](src/main/scala/AdventOfCode2017/Day22.scala) |
173173
| 23 | [Coprocessor Conflagration](https://adventofcode.com/2017/day/23) | [Source](src/main/scala/AdventOfCode2017/Day23.scala) |
174174
| 24 | [Electromagnetic Moat](https://adventofcode.com/2017/day/24) | [Source](src/main/scala/AdventOfCode2017/Day24.scala) |
175+
| 25 | [The Halting Problem](https://adventofcode.com/2017/day/25) | [Source](src/main/scala/AdventOfCode2017/Day25.scala) |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
Begin in state A.
2+
Perform a diagnostic checksum after 12667664 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 right.
22+
- Continue with state D.
23+
24+
In state C:
25+
If the current value is 0:
26+
- Write the value 0.
27+
- Move one slot to the left.
28+
- Continue with state B.
29+
If the current value is 1:
30+
- Write the value 0.
31+
- Move one slot to the left.
32+
- Continue with state E.
33+
34+
In state D:
35+
If the current value is 0:
36+
- Write the value 1.
37+
- Move one slot to the right.
38+
- Continue with state A.
39+
If the current value is 1:
40+
- Write the value 0.
41+
- Move one slot to the right.
42+
- Continue with state B.
43+
44+
In state E:
45+
If the current value is 0:
46+
- Write the value 1.
47+
- Move one slot to the left.
48+
- Continue with state F.
49+
If the current value is 1:
50+
- Write the value 1.
51+
- Move one slot to the left.
52+
- Continue with state C.
53+
54+
In state F:
55+
If the current value is 0:
56+
- Write the value 1.
57+
- Move one slot to the right.
58+
- Continue with state D.
59+
If the current value is 1:
60+
- Write the value 1.
61+
- Move one slot to the right.
62+
- Continue with state A.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package AdventOfCode2017
2+
3+
object Day25:
4+
case class Rule(write: Boolean, step: Int, next: String)
5+
case class Turing(tape: Set[Int], position: Int, state: String)
6+
7+
def parse(input: Seq[String]): (String, Int, Map[(String, Boolean), Rule]) =
8+
val trimmed = input.map(_.init.trim.split(" "))
9+
val start = trimmed(0)(3)
10+
val total = trimmed(1)(5).toInt
11+
val rules = trimmed.drop(2).grouped(10).map { group =>
12+
val state = group(1)(2)
13+
Seq((state, false) -> parseRule(group.drop(3)), (state, true) -> parseRule(group.drop(7)))
14+
}
15+
(start, total, rules.flatten.toMap)
16+
17+
def parseRule(block: Seq[Array[String]]): Rule =
18+
val write = block(0)(4) == "1"
19+
val step = if block(1)(6) == "right" then 1 else -1
20+
val next = block(2)(4)
21+
Rule(write, step, next)
22+
23+
def step(rules: Map[(String, Boolean), Rule])(turing: Turing): Turing =
24+
val rule = rules(turing.state -> turing.tape.contains(turing.position))
25+
val nextTape = if rule.write then turing.tape + turing.position else turing.tape - turing.position
26+
Turing(nextTape, turing.position + rule.step, rule.next)
27+
28+
def part1(input: Seq[String]): Int =
29+
val (start, total, rules) = parse(input)
30+
val initial = Turing(Set(), 0, start)
31+
Iterator.iterate(initial)(step(rules)).drop(total).next().tape.size
32+
33+
def main(args: Array[String]): Unit =
34+
val data = io.Source.fromResource("AdventOfCode2017/Day25.txt").getLines().toSeq
35+
println(part1(data))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package AdventOfCode2017
2+
3+
import org.scalatest.funsuite.AnyFunSuite
4+
5+
class Day25Suite extends AnyFunSuite:
6+
val sample = Seq(
7+
"Begin in state A.",
8+
"Perform a diagnostic checksum after 6 steps.",
9+
"",
10+
"In state A:",
11+
" If the current value is 0:",
12+
" - Write the value 1.",
13+
" - Move one slot to the right.",
14+
" - Continue with state B.",
15+
" If the current value is 1:",
16+
" - Write the value 0.",
17+
" - Move one slot to the left.",
18+
" - Continue with state B.",
19+
"",
20+
"In state B:",
21+
" If the current value is 0:",
22+
" - Write the value 1.",
23+
" - Move one slot to the left.",
24+
" - Continue with state A.",
25+
"If the current value is 1:",
26+
" - Write the value 1.",
27+
" - Move one slot to the right.",
28+
" - Continue with state A.")
29+
30+
test("Part 1 should handle sample input correctly") {
31+
assert(Day25.part1(sample) == 3)
32+
}

0 commit comments

Comments
 (0)