Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- direction_name = {0 : "right", 1 : "down", 2 : "left", 3 : "up"}
- direction_code = {"right" : 0, "down": 1, "left" : 2, "up" : 3}
- zone_link = {(1,0):(2,0), (1,1):(4,1), (1,2):(6,0), (1,3):(9,0), \
- (2,0):(7,2), (2,1):(4,2), (2,2):(1,2), (2,3):(9,3), \
- (4,0):(2,3), (4,1):(7,1), (4,2):(6,1), (4,3):(1,3), \
- (6,0):(7,0), (6,1):(9,1), (6,2):(1,0), (6,3):(4,0), \
- (7,0):(2,2), (7,1):(9,2), (7,2):(6,2), (7,3):(4,3), \
- (9,0):(7,3), (9,1):(2,1), (9,2):(1,1), (9,3):(6,3)}
- direction_coord_change = {0 : (1,0), 1 : (0,1), 2 : (-1,0), 3 : (0,-1)}
- lines = puzzle_input.split("\n")
- field_strings = lines[:-2]
- instruction_line = lines[-1]
- complete_field = np.zeros((len(max(field_strings,key=len)),len(field_strings)))
- for y_pos,line in enumerate(field_strings):
- for x_pos in range(len(line)):
- if line[x_pos] == ".":
- complete_field[x_pos,y_pos] = 1
- elif line[x_pos] == "#":
- complete_field[x_pos,y_pos] = 2
- # split field in zones
- zone_field = np.zeros((12,50,50))
- for zone_y in range(4):
- for zone_x in range(3):
- zone_id = zone_x+zone_y*3
- zone_field[zone_id,:,:] = complete_field[zone_x*50:(zone_x+1)*50,zone_y*50:(zone_y+1)*50]
- # Initialize location
- zone = 1
- x_pos = 0
- y_pos = 0
- direction = 0
- # Process instructions
- instructions = []
- index = 0
- while index < len(instruction_line):
- if instruction_line[index] == "R":
- instructions.append("R")
- index += 1
- elif instruction_line[index] == "L":
- instructions.append("L")
- index += 1
- else:
- length = 0
- eol = False
- while not eol:
- length += 1
- if index+length >= len(instruction_line):
- eol = True
- elif (instruction_line[index+length] == "R") or (instruction_line[index+length] == "L"):
- eol = True
- instructions.append(int(instruction_line[index:index+length]))
- index += length
- print("Start position: (",x_pos,",",y_pos,")")
- route = []
- # Execute instructions:
- for instruction in instructions:
- if instruction == "R":
- direction = (direction + 1) % 4
- print("Turning right, now facing",direction_name[direction])
- elif instruction == "L":
- direction = (direction - 1) % 4
- print("Turning left, now facing",direction_name[direction])
- else:
- print("Moving",instruction,"steps forward")
- for step in range(instruction):
- target_x_pos = x_pos + direction_coord_change[direction][0]
- target_y_pos = y_pos + direction_coord_change[direction][1]
- target_zone = zone
- target_direction = direction
- # Wraparound!
- if target_y_pos >= zone_field[zone].shape[1]:
- target_zone,target_direction = zone_link[(zone,direction)]
- if target_direction == 0: # right
- target_y_pos = 49 - target_x_pos
- target_x_pos = 0
- if target_direction == 1: # down
- target_y_pos = 0
- if target_direction == 2: # left
- target_y_pos = target_x_pos
- target_x_pos = 49
- if target_direction == 3: # up
- target_x_pos = 49 - target_pos_x
- target_y_pos = 49
- elif target_y_pos < 0:
- target_zone,target_direction = zone_link[(zone,direction)]
- if target_direction == 0: # right
- target_y_pos = target_x_pos
- target_x_pos = 0
- if target_direction == 1: # down
- target_x_pos = 49 - target_pos_x
- target_y_pos = 0
- if target_direction == 2: # left
- target_y_pos = 49 - target_x_pos
- target_x_pos = 49
- if target_direction == 3: # up
- target_y_pos = 49
- elif target_x_pos >= zone_field[zone].shape[0]:
- target_zone,target_direction = zone_link[(zone,direction)]
- if target_direction == 0: # right
- target_x_pos = 0
- if target_direction == 1: # down
- target_x_pos = 49 - target_y_pos
- target_y_pos = 0
- if target_direction == 2: # left
- target_x_pos = 49
- target_y_pos = 49 - target_y_pos
- if target_direction == 3: # up
- target_x_pos = target_y_pos
- target_y_pos = 49
- elif target_x_pos < 0:
- target_zone,target_direction = zone_link[(zone,direction)]
- if target_direction == 0: # right
- target_x_pos = 0
- target_y_pos = 49 - target_y_pos
- if target_direction == 1: # down
- target_x_pos = target_y_pos
- target_y_pos = 0
- if target_direction == 2: # left
- target_x_pos = 49
- if target_direction == 3: # up
- target_x_pos = 49 - target_y_pos
- target_y_pos = 49
- if zone_field[target_zone][target_x_pos][target_y_pos] == 2:
- print("Hit a wall, stopping!")
- break
- else:
- x_pos = target_x_pos
- y_pos = target_y_pos
- direction = target_direction
- zone = target_zone
- route.append((zone,x_pos,y_pos))
- print("Now at position (",x_pos,",",y_pos,")")
- # Correct coordinates (not just on face of cube but on folded out map:
- zone_x = zone % 3
- zone_y = (zone - zone_x) // 3
- x_pos += zone_x * 50
- y_pos += zone_y * 50
- print("Final coordinate: (",x_pos+1,",",y_pos+1,")")
- print("Final direction:",direction_name[direction])
- solution = (y_pos + 1) * 1000 + (x_pos + 1) * 4 + direction
- print("Final password:",solution)
Advertisement
Add Comment
Please, Sign In to add comment