Browse Source

day22 finished

main
Luna Lailatova 2 years ago
parent
commit
501f846a63
  1. 107
      day22-1.py
  2. 150
      day22-2.py
  3. 29
      in2.txt
  4. 202
      input22.txt

107
day22-1.py

@ -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())

150
day22-2.py

@ -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())

29
in2.txt

@ -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 10R5L5R10L4R5L5
hmdt: 32

202
input22.txt

File diff suppressed because one or more lines are too long
Loading…
Cancel
Save