Advent of Code 2022 - Day 9 - part 1
30 Nov 2023To get ready for Advent of Code 2023, I continued where I stopped last year: day 9.
Here’s me struggling for 1 hour and 40 minutes spread across two days, because I tried to be clever. For viewing pleasure it has been sped up.
What goes wrong here is that I assumed incorrectly that a negative number to the power of zero would be -1. Quick maths turned out not to be good maths. The following bit of experimentation led me to believe that.
result = -5 ** 0
assert result == -1
You may have already guessed that the order of operation fooled me.
The **
operator is executed before the -
operator is applied to the result.
The behaviour is different if I do the same using a variable:
x = -5
result = x ** 0
assert result == 1
The solution that eventually led to the correct answer is in the code block below. After submitting the correct answer, I did some cleanup:
- deleted some unreachable code
- linted it
- removed some debug lines
- and added some more comments
- deleted the
x ** 0
andy ** 0
since they are pointless now.
from advent_of_code import *
import requests_cache
requests_cache.install_cache()
test_input = """R 4
U 4
L 3
D 1
R 4
D 1
L 5
R 2"""
input_9 = fetch_input(9)
# input_9 = test_input
movements = [x.split(' ') for x in input_9.splitlines()]
movements = [(x[0], int(x[1])) for x in movements]
visited = set()
head = (0, 0)
tail = (0, 0)
directions = {
# What coordinates change for each movement
# x, y
"U": (1, 0),
"D": (-1, 0),
"L": (0, -1),
"R": (0, 1),
}
def modify_location(location, direction):
if isinstance(direction, str):
change = directions[direction]
else:
change = direction
return location[0] + change[0], location[1] + change[1]
def direction_to_move(head, tail):
x = head[0] - tail[0]
y = head[1] - tail[1]
# head and tail are at the same location, don't move
if x == 0 and y == 0:
return 0, 0
# head and tail are less than one square apart (including diagonally)
elif max(abs(x), abs(y)) == 1:
return 0, 0
# head and tail are too far apart, decide which direction to move the tail
else:
if x < 0:
x = x ** 0 * -1
else:
x = 0 if not x else 1
if y < 0:
y = -1
else:
y = 0 if not y else 1
return x, y
tail_visited = set()
for direction, length in movements:
for _ in range(length):
head = modify_location(head, direction)
tail = modify_location(tail, direction_to_move(head, tail))
# keep track of where the tail has been:
tail_visited.add(tail)
submit_answer(level=1, day=9, answer=len(tail_visited))
The lessons I learned: Don’t try to be clever and check your maths.