Luna Lailatova
2 years ago
4 changed files with 473 additions and 15 deletions
@ -0,0 +1,107 @@
@@ -0,0 +1,107 @@
|
||||
import re |
||||
|
||||
#create a class to store the map |
||||
class plan: |
||||
def __init__(self): |
||||
self.grid = [] |
||||
self.goTo = [(0, 1), (1, 0), (0, -1), (-1, 0)] |
||||
|
||||
def addLine(self, inputLine): |
||||
inputLine = inputLine.replace(' ', '+') |
||||
self.grid.append(list(inputLine)) |
||||
|
||||
def testPrint(self): |
||||
for line in self.grid: |
||||
for point in line: |
||||
print(point, end = ' ') |
||||
print('\n') |
||||
|
||||
def setStart(self): |
||||
self.direction = 0 |
||||
self.column = self.grid[0].index('.') |
||||
self.row = 0 |
||||
maxL = 0 |
||||
for line in self.grid: |
||||
if len(line) > maxL: |
||||
maxL = len(line) |
||||
for i in range(len(self.grid)): |
||||
while len(self.grid[i]) < maxL: |
||||
self.grid[i].append('+') |
||||
|
||||
def getInstruction(self, instruction): |
||||
if instruction.isnumeric(): |
||||
self.move(int(instruction)) |
||||
if instruction.isalpha(): |
||||
self.turn(instruction) |
||||
|
||||
def turn(self, direction): |
||||
if direction == 'L': |
||||
self.direction -= 1 |
||||
if self.direction < 0: |
||||
self.direction = 3 |
||||
elif direction == 'R': |
||||
self.direction = (self.direction + 1) % 4 |
||||
|
||||
def move(self, distance): |
||||
for _ in range(distance): |
||||
nextRow = self.row + self.goTo[self.direction][0] |
||||
nextCol = self.column + self.goTo[self.direction][1] |
||||
if nextRow < 0 or nextCol < 0 or nextRow >= len(self.grid) or nextCol >= len(self.grid[nextRow]) or self.grid[nextRow][nextCol] == '+': |
||||
nextRow, nextCol = self.wrapAround(nextRow, nextCol) |
||||
if self.grid[nextRow][nextCol] == '#': |
||||
return |
||||
self.column = nextCol |
||||
self.row = nextRow |
||||
|
||||
def wrapAround(self, nextRow, nextCol): |
||||
if self.direction == 0: |
||||
for i in range(len(self.grid[nextRow])): |
||||
if self.grid[nextRow][i] in ['.', '#']: |
||||
nextCol = i |
||||
break |
||||
elif self.direction == 1: |
||||
for i in range(len(self.grid)): |
||||
if self.grid[i][nextCol] in ['.', '#']: |
||||
nextRow = i |
||||
break |
||||
elif self.direction == 2: |
||||
for i in range(len(self.grid[nextRow])-1, 0, -1): |
||||
if self.grid[nextRow][i] in ['.', '#']: |
||||
nextCol = i |
||||
break |
||||
elif self.direction == 3: |
||||
for i in range(len(self.grid)-1, 0, -1): |
||||
if self.grid[i][nextCol] in ['.', '#']: |
||||
nextRow = i |
||||
break |
||||
return nextRow, nextCol |
||||
|
||||
def getPassword(self): |
||||
return (self.column + 1)*4 + (self.row + 1)*1000 + self.direction |
||||
|
||||
|
||||
|
||||
#create an object of the map |
||||
path = plan() |
||||
|
||||
# reading input and inserting it into the map |
||||
with open('input22.txt','r') as f: |
||||
inp = f.read().splitlines(keepends=False) |
||||
|
||||
for line in inp: |
||||
if line == '': |
||||
break |
||||
path.addLine(line) |
||||
|
||||
instructionSet = inp[-1] |
||||
instructionSet = re.split('(\d+)', instructionSet) |
||||
|
||||
path.setStart() |
||||
|
||||
#go through the instructions |
||||
for instruction in instructionSet: |
||||
path.getInstruction(instruction) |
||||
|
||||
#path.testPrint() |
||||
print(path.column + 1, path.row + 1, path.direction, sep = '---') |
||||
print(path.getPassword()) |
@ -0,0 +1,150 @@
@@ -0,0 +1,150 @@
|
||||
import re |
||||
|
||||
#create a class to store the map |
||||
class plan: |
||||
def __init__(self): |
||||
self.grid = [] |
||||
self.goTo = [(0, 1), (1, 0), (0, -1), (-1, 0)] |
||||
|
||||
def addLine(self, inputLine): |
||||
inputLine = inputLine.replace(' ', '+') |
||||
self.grid.append(list(inputLine)) |
||||
|
||||
def testPrint(self): |
||||
for line in self.grid: |
||||
for point in line: |
||||
print(point, end = ' ') |
||||
print('\n') |
||||
|
||||
def setStart(self): |
||||
self.direction = 0 |
||||
self.column = self.grid[0].index('.') |
||||
self.row = 0 |
||||
maxL = 0 |
||||
for line in self.grid: |
||||
if len(line) > maxL: |
||||
maxL = len(line) |
||||
for i in range(len(self.grid)): |
||||
while len(self.grid[i]) < maxL: |
||||
self.grid[i].append('+') |
||||
|
||||
def getInstruction(self, instruction): |
||||
if instruction.isnumeric(): |
||||
self.move(int(instruction)) |
||||
if instruction.isalpha(): |
||||
self.turn(instruction) |
||||
|
||||
def turn(self, direction): |
||||
if direction == 'L': |
||||
self.direction -= 1 |
||||
if self.direction < 0: |
||||
self.direction = 3 |
||||
elif direction == 'R': |
||||
self.direction = (self.direction + 1) % 4 |
||||
|
||||
def move(self, distance): |
||||
for _ in range(distance): |
||||
directionChange = self.direction |
||||
nextRow = self.row + self.goTo[self.direction][0] |
||||
nextCol = self.column + self.goTo[self.direction][1] |
||||
if nextRow < 0 or nextCol < 0 or nextRow >= len(self.grid) or nextCol >= len(self.grid[nextRow]) or self.grid[nextRow][nextCol] == '+': |
||||
nextRow, nextCol, directionChange = self.wrapAround(self.row, self.column) |
||||
if self.grid[nextRow][nextCol] == '#': |
||||
return |
||||
self.direction = directionChange |
||||
self.column = nextCol |
||||
self.row = nextRow |
||||
|
||||
def wrapAround(self, nextRow, nextCol): |
||||
if self.direction == 0: |
||||
if nextRow < 50: |
||||
nextCol = 99 |
||||
nextRow = 149 - nextRow |
||||
directionChange = 2 |
||||
elif nextRow < 100: |
||||
nextCol = 50 + nextRow |
||||
nextRow = 49 |
||||
directionChange = 3 |
||||
elif nextRow < 150: |
||||
nextRow = 149 - nextRow |
||||
nextCol = 149 |
||||
directionChange = 2 |
||||
else: |
||||
nextCol = nextRow - 100 |
||||
nextRow = 149 |
||||
directionChange = 3 |
||||
elif self.direction == 1: |
||||
if nextCol < 50: |
||||
nextCol += 100 |
||||
nextRow = 0 |
||||
directionChange = 1 |
||||
elif nextCol < 100: |
||||
nextRow = 100 + nextCol |
||||
nextCol = 49 |
||||
directionChange = 2 |
||||
else: |
||||
nextRow = nextCol - 50 |
||||
nextCol = 99 |
||||
directionChange = 2 |
||||
elif self.direction == 2: |
||||
if nextRow < 50: |
||||
nextCol = 0 |
||||
nextRow = 149 - nextRow |
||||
directionChange = 0 |
||||
elif nextRow < 100: |
||||
nextCol = nextRow - 50 |
||||
nextRow = 100 |
||||
directionChange = 1 |
||||
elif nextRow < 150: |
||||
nextRow = 49 - (nextRow - 100) |
||||
nextCol = 50 |
||||
directionChange = 0 |
||||
else: |
||||
nextCol = nextRow - 100 |
||||
nextRow = 0 |
||||
directionChange = 1 |
||||
elif self.direction == 3: |
||||
if nextCol < 50: |
||||
nextRow = nextCol + 50 |
||||
nextCol = 50 |
||||
directionChange = 0 |
||||
elif nextCol < 100: |
||||
nextRow = nextCol + 100 |
||||
nextCol = 0 |
||||
directionChange = 0 |
||||
else: |
||||
nextCol = nextCol - 100 |
||||
nextRow = 199 |
||||
directionChange = 3 |
||||
return nextRow, nextCol, directionChange |
||||
|
||||
def getPassword(self): |
||||
return (self.column + 1)*4 + (self.row + 1)*1000 + self.direction |
||||
|
||||
|
||||
|
||||
#create an object of the map |
||||
path = plan() |
||||
|
||||
# reading input and inserting it into the map |
||||
with open('input22.txt','r') as f: |
||||
inp = f.read().splitlines(keepends=False) |
||||
|
||||
for line in inp: |
||||
if line == '': |
||||
break |
||||
path.addLine(line) |
||||
|
||||
instructionSet = inp[-1] |
||||
instructionSet = re.split('(\d+)', instructionSet) |
||||
|
||||
path.setStart() |
||||
|
||||
#go through the instructions |
||||
for instruction in instructionSet: |
||||
path.getInstruction(instruction) |
||||
|
||||
|
||||
# print solution |
||||
print(path.column + 1, path.row + 1, path.direction, sep = '---') |
||||
print(path.getPassword()) |
@ -1,15 +1,14 @@
@@ -1,15 +1,14 @@
|
||||
root: pppw + sjmn |
||||
dbpl: 5 |
||||
cczh: sllz + lgvd |
||||
zczc: 2 |
||||
ptdq: humn - dvpt |
||||
dvpt: 3 |
||||
lfqf: 4 |
||||
humn: 5 |
||||
ljgn: 2 |
||||
sjmn: drzm * dbpl |
||||
sllz: 4 |
||||
pppw: cczh / lfqf |
||||
lgvd: ljgn * ptdq |
||||
drzm: hmdt - zczc |
||||
hmdt: 32 |
||||
...# |
||||
.#.. |
||||
#... |
||||
.... |
||||
...#.......# |
||||
........#... |
||||
..#....#.... |
||||
..........#. |
||||
...#.... |
||||
.....#.. |
||||
.#...... |
||||
......#. |
||||
|
||||
10R5L5R10L4R5L5 |
File diff suppressed because one or more lines are too long
Loading…
Reference in new issue