Solution to problem 3 in Python

This commit is contained in:
David Doblas Jiménez 2022-02-28 15:03:57 +01:00
parent d8be81fb92
commit bb777a3e12

View File

@ -58,5 +58,58 @@ def part_1() -> None:
print(f"{len(set(pos))} houses receive at least one present")
# --- Part Two ---
# The next year, to speed up the process, Santa creates a robot version of
# himself, Robo-Santa, to deliver presents with him.
# Santa and Robo-Santa start at the same location (delivering two presents to
# the same starting house), then take turns moving based on instructions from
# the elf, who is eggnoggedly reading from the same script as the previous
# year.
# This year, how many houses receive at least one present?
# For example:
# ^v delivers presents to 3 houses, because Santa goes north, and then
# Robo-Santa goes south.
# ^>v< now delivers presents to 3 houses, and Santa and Robo-Santa end up
# back where they started.
# ^v^v^v^v^v now delivers presents to 11 houses, with Santa going one
# direction and Robo-Santa going the other.
def part_2():
santa = [(0, 0)]
robo_santa = [(0, 0)]
for idx, move in enumerate(moves):
if move == "^":
if idx % 2 == 0:
santa.append(move_up(santa[-1]))
else:
robo_santa.append(move_up(robo_santa[-1]))
elif move == "v":
if idx % 2 == 0:
santa.append(move_down(santa[-1]))
else:
robo_santa.append(move_down(robo_santa[-1]))
elif move == "<":
if idx % 2 == 0:
santa.append(move_left(santa[-1]))
else:
robo_santa.append(move_left(robo_santa[-1]))
elif move == ">":
if idx % 2 == 0:
santa.append(move_right(santa[-1]))
else:
robo_santa.append(move_right(robo_santa[-1]))
print(
f"{len(set(santa + robo_santa))} houses receive at least one present"
)
if __name__ == "__main__":
part_1()
part_2()