Springdroid Adventure
Tags: adventofcode, algorithms, computerscience, programming
Published: 2022-05-23 17:01:29

## [Advent of Code 2019 Day 21](https://adventofcode.com/2019/day/21)

## [Try the simulator using your puzzle input and instructions!](https://aocspringdroidadventure.rmion.repl.co/)
![Simulator showing found solution to puzzle](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yb96v5k2kp77w26qiu5b.gif)

## Task: Solve for X where...
### Part 1
```
X = the amount of hull damage reported by the droid after successfully traversing the hull
```

## No example input provided
Instead, some example instructions and hull diagrams are provided
```
NOT D J
WALK
```
It represents:
- Two commands for the droid
- The first encompasses three operators: 1) an instruction, 2 and 3) boolean values
- The second instructs the droid to move one tile forward

## Part 1
1. Intcode computer: Round 10!
2. All of the character codes, I hope
3. Composing a few input instructions using the examples
4. Running the program with an example input
5. Enabling direct instruction input
6. Trying to understand `AND`, `NOT`, `OR`, `T` and `J`
7. Confused by what I see
8. Giving up. Cheating. Feeling real bummed.

### Intcode computer: Round 10!
- Another ASCII-based droid-controlling Intcode computer puzzle
- This time, the input uses boolean values and logical operators instead of purely directional values
- I'm glad I made the simulator for Day 17. I should be able to repurpose it to gain more visibility into this puzzle's output

### All of the character codes, I hope
```
 Newline
10

 Space
32

 N  O  T
78 79 84

 A  N  D
65 78 68

 O  R
79 82

 A  B  C  D  T  J
65 66 67 68 84 74

 W  A  L  K
87 65 76 75
```

### Composing a few input instructions using the examples
> Jump if the tile immediately in front is not ground.
```
NOT A J
WALK

78 79 84 32 65 32 74 10
87 65 76 75 10
```

> Set J to true if and only if there is no ground four tiles away. In other words, jump into any hole found.
```
NOT D J
WALK

78 79 84 32 68 32 74 10
87 65 76 75 10
```

> Jump if a three-tile-wide hole (with ground on the other side of the hole) is detected.
```
NOT A J
NOT B T
AND T J
NOT C T
AND T J
AND D J

78 79 84 32 65 32 74 10
78 79 84 32 66 32 84 10
65 78 68 32 84 32 74 10
78 79 84 32 67 32 84 10
65 78 68 32 84 32 74 10
65 78 68 32 68 32 74 10
87 65 76 75 10
```

### Running the program with an example input
Let's start with `NOT A J, WALK`
- Success: It runs!

Here's a GIF of what I saw:
![Failed attempt at traversing the hull](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gdxdhie4dlcnboozn5uh.gif)

Let's try again with `NOT D J, WALK`
- Success: It runs!
- It shows the exact same series of ASCII artwork as in the puzzle instructions


Let's try again with the lengthiest example
- Success: It runs!
- The droid never jumps, and falls straight in the hole

```
.................
.................
@................
#####.###########

.................
.................
.@...............
#####.###########

.................
.................
..@..............
#####.###########

.................
.................
...@.............
#####.###########

.................
.................
....@............
#####.###########

.................
.................
.................
#####@###########
```

### Enabling direct instruction input
- I am already tired of manually converting letters into character codes and re-building my array of character codes
- I want to use the simulator for what it's best at: acting like a droid who can translate english into character codes

Thankfully, this was a relatively easy task:
```
Split the string entered as text into a textarea at each character into an array of single characters, newlines and space characters included
Change each value into its equivalent character code
Append to the end of the new list of character codes six additional codes that represent two newline characters at each end and the word WALK in the middle
```

[Try the simulator using your puzzle input and instructions!](https://aocspringdroidadventure.rmion.repl.co/)
![Simulator with custom input text entry](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/39wtjty0lcoiz8xojkh2.png)

### Trying to understand `AND`, `NOT`, `OR`, `T` and `J`
Here are the rules as I understand them:
- I must submit instructions once, at the beginning
- I must submit as many or fewer than 15 lines
- The last instruction must be `WALK`

My instructions must follow this format:
```
OPERATOR X Y
```
- Where `OPERATOR` must be one of `AND`, `NOT`, `OR`
- And `X` must be one of `A`,`B`,`C`,`D`,`T`,`J`
- And `Y` must be one of `T`,`J`

`T` and `J`
- Appear to act as two variables, each containing a boolean - both starting as `false`
- The one that matters most is `J` because it dictates whether the droid will jump
- `T` acts as a handy way to track additional state and eventually help determine what `J` should be

`AND`, `NOT` and `OR`
- `NOT` is like `opposite`
- `OR` is like `either`
- `AND` is like `both`

Stepping through the long example:
> jumps if a three-tile-wide hole (with ground on the other side of the hole) is detected
```
NOT A J
NOT B T
AND T J
NOT C T
AND T J
AND D J

T = false
J = false

NOT A J
  Is A ground?
    Yes? A is ground. Ground is true.
      J = false
    No? A is hole. Hole is false.
      J = true

T = false
J = true

NOT B T
  Is B ground?
    No? B is hole. Hole is false.
      T = true

T = true
J = true

AND T J
  Is T true?
    Yes.
  Is J true?
    Yes.
      J = true

T = true
J = true

NOT C T
  Is C ground?
    No? C is hole. Hole is false.
      T = true

T = true
J = true

AND T J
  Is T true?
    Yes.
  Is J true?
    Yes.
      J = true

T = true
J = true

AND D J
  Is D ground?
    Yes. D is ground. Ground is true.
      D is true.
  Is J true?
    Yes.
     J = true

J = true

@.......
#...####
 ABCD
 FFFT

JUMP

....@...
#...####
```

### Confused by what I see
I tried a few single `NOT` instructions:
![Example outputs of failed attempts](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hk26odh69rw5hsr049wp.jpg)
- The ground keeps changing
- Maybe because the droid reports ground for any tile it doesn't see?
- There is a three-tile-wide hole for `B` and `C`, but not `A` or `D`. Wait. What?

### Giving up. Cheating. Feeling real bummed.
I didn't understand the logical operators enough to piece this puzzle together.

So I gave up and went to the sub-reddit Solution Megathread for today's puzzle.

Thankfully, [one commenter pasted their solutions to both parts](https://www.reddit.com/r/adventofcode/comments/edll5a/comment/fc9y38n/?utm_source=share&utm_medium=web2x&context=3) with a fantastic and understandable explanation.
```
notes:  
--------------------------------------------------------------
  1. droid jumps 4 steps at a time 
  2. always check to see that tile D (4th tile) is solid (for landing) 
  3. if you want to land on an island (###.##..####), jump 2 tiles before the first 
     hole: so basically jump whenever C (3rd tile ahead) is a hole.  
  4. else jump if there's a hole right in front of you (A)

--------------------------------------------------------------

NOT C J 
AND D J 
NOT A T 
OR T J

WALK
```

- Only four commands?! That seems too easy.
- What do they do?

```
NOT C J 
  If the tile three ahead is a hole, get ready to jump
AND D J 
  If the tile three ahead is a hole
  And the tile four ahead is ground, get ready to jump
NOT A T 
  Remember if the tile one ahead is a hole as:
    True = hole, False = ground
OR T J
  JUMP if either:
    The tile one ahead is a hole, or
    The tile three ahead is a hole and four ahead is ground
```

Sure enough, running it in my simulator outputs this:
```
Input instructions:

    Walking...

         䮭
```

- That last symbol is proof that it worked
- I could have used my console to see the large integer value last within the output list and entered it as the correct answer
- But I didn't solve this puzzle myself
- Nor am I confident I could re-write those instructions
- So, sadly, this puzzle will remain unsolved for me

## Celebrating my accomplishments
- I figured out how to convert a string into character codes
- I built another Intcode computer simulator...one that also accepts instructions!
- I've now built 9 simulators!

Bummers:
- I've now only solved 9/12 Intcode computer puzzles: 75%
- I really wanted to figure this one out. Oh well, at least I understand how the solution works.