day 01
This commit is contained in:
parent
2a49e27c19
commit
d84be96510
1
01/input.puzzle01
Normal file
1
01/input.puzzle01
Normal file
@ -0,0 +1 @@
|
|||||||
|
L4, R2, R4, L5, L3, L1, R4, R5, R1, R3, L3, L2, L2, R5, R1, L1, L2, R2, R2, L5, R5, R5, L2, R1, R2, L2, L4, L1, R5, R2, R1, R1, L2, L3, R2, L5, L186, L5, L3, R3, L5, R4, R2, L5, R1, R4, L1, L3, R3, R1, L1, R4, R2, L1, L4, R5, L1, R50, L4, R3, R78, R4, R2, L4, R3, L4, R4, L1, R5, L4, R1, L2, R3, L2, R5, R5, L4, L1, L2, R185, L5, R2, R1, L3, R4, L5, R2, R4, L3, R4, L2, L5, R1, R2, L2, L1, L2, R2, L2, R1, L5, L3, L4, L3, L4, L2, L5, L5, R2, L3, L4, R4, R4, R5, L4, L2, R4, L5, R3, R1, L1, R3, L2, R2, R1, R5, L4, R5, L3, R2, R3, R1, R4, L4, R1, R3, L5, L1, L3, R2, R1, R4, L4, R3, L3, R3, R2, L3, L3, R4, L2, R4, L3, L4, R5, R1, L1, R5, R3, R1, R3, R4, L1, R4, R3, R1, L5, L5, L4, R4, R3, L2, R1, R5, L3, R4, R5, L4, L5, R2
|
1
01/input.sample01
Normal file
1
01/input.sample01
Normal file
@ -0,0 +1 @@
|
|||||||
|
R2, L3
|
1
01/input.sample02
Normal file
1
01/input.sample02
Normal file
@ -0,0 +1 @@
|
|||||||
|
R2, R2, R2
|
1
01/input.sample03
Normal file
1
01/input.sample03
Normal file
@ -0,0 +1 @@
|
|||||||
|
R5, L5, R5, R3
|
1
01/input.sample04
Normal file
1
01/input.sample04
Normal file
@ -0,0 +1 @@
|
|||||||
|
R8, R4, R4, R8
|
46
01/part01.py
Executable file
46
01/part01.py
Executable file
@ -0,0 +1,46 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
import re
|
||||||
|
import argparse
|
||||||
|
|
||||||
|
DIRECTIONS = [
|
||||||
|
{'dir': 'N', 'V': 1, "H": 0 },
|
||||||
|
{'dir': 'W', 'V': 0, "H": -1 },
|
||||||
|
{'dir': 'S', 'V': -1, "H": 0 },
|
||||||
|
{'dir': 'E', 'V': 0, "H": 1 }
|
||||||
|
]
|
||||||
|
DIRECTION_CHANGE = {
|
||||||
|
'L': -1,
|
||||||
|
'R': 1
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def load_file(filename):
|
||||||
|
with open(filename) as f:
|
||||||
|
data = f.read()
|
||||||
|
return data
|
||||||
|
|
||||||
|
def main(args):
|
||||||
|
DIRECTION_INDEX = 0
|
||||||
|
MAP_POSITION = [0, 0]
|
||||||
|
|
||||||
|
for INDICE in load_file(args.input).split(','):
|
||||||
|
INDICE = INDICE.strip()
|
||||||
|
TURN = INDICE[0]
|
||||||
|
STEP = int(INDICE[1:].strip())
|
||||||
|
DIRECTION_INDEX = DIRECTION_INDEX + DIRECTION_CHANGE[TURN]
|
||||||
|
if DIRECTION_INDEX < 0:
|
||||||
|
DIRECTION_INDEX = len(DIRECTIONS) - 1
|
||||||
|
if DIRECTION_INDEX >= len(DIRECTIONS):
|
||||||
|
DIRECTION_INDEX = 0
|
||||||
|
MAP_POSITION[0] = MAP_POSITION[0] + DIRECTIONS[DIRECTION_INDEX]['V']*STEP
|
||||||
|
MAP_POSITION[1] = MAP_POSITION[1] + DIRECTIONS[DIRECTION_INDEX]['H']*STEP
|
||||||
|
|
||||||
|
print("%d" % (abs(MAP_POSITION[0]) + abs(MAP_POSITION[1])))
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
parser = argparse.ArgumentParser(description='AdventOfCode 2016 Day 01')
|
||||||
|
parser.add_argument('--input', '-i', action='store', required=True, help='input file')
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
main(args)
|
78
01/part02.py
Executable file
78
01/part02.py
Executable file
@ -0,0 +1,78 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
import re
|
||||||
|
import argparse
|
||||||
|
import copy
|
||||||
|
|
||||||
|
DIRECTIONS = [
|
||||||
|
{'dir': 'N', 'V': 1, "H": 0 },
|
||||||
|
{'dir': 'W', 'V': 0, "H": -1 },
|
||||||
|
{'dir': 'S', 'V': -1, "H": 0 },
|
||||||
|
{'dir': 'E', 'V': 0, "H": 1 }
|
||||||
|
]
|
||||||
|
DIRECTION_CHANGE = {
|
||||||
|
'L': -1,
|
||||||
|
'R': 1
|
||||||
|
}
|
||||||
|
|
||||||
|
MAPS_VISITED = []
|
||||||
|
|
||||||
|
def load_file(filename):
|
||||||
|
with open(filename) as f:
|
||||||
|
data = f.read()
|
||||||
|
return data
|
||||||
|
|
||||||
|
def main(args):
|
||||||
|
DIRECTION_INDEX = 0
|
||||||
|
MAP_POSITION_B = [0, 0]
|
||||||
|
MAP_POSITION_A = None
|
||||||
|
TWICE = None
|
||||||
|
|
||||||
|
MAPS_VISITED = {0: [0]}
|
||||||
|
|
||||||
|
for INDICE in load_file(args.input).split(','):
|
||||||
|
INDICE = INDICE.strip()
|
||||||
|
TURN = INDICE[0]
|
||||||
|
STEP = int(INDICE[1:].strip())
|
||||||
|
DIRECTION_INDEX = DIRECTION_INDEX + DIRECTION_CHANGE[TURN]
|
||||||
|
if DIRECTION_INDEX < 0:
|
||||||
|
DIRECTION_INDEX = len(DIRECTIONS) - 1
|
||||||
|
if DIRECTION_INDEX >= len(DIRECTIONS):
|
||||||
|
DIRECTION_INDEX = 0
|
||||||
|
MAP_POSITION_A = copy.copy(MAP_POSITION_B)
|
||||||
|
MAP_POSITION_B[0] = MAP_POSITION_A[0] + DIRECTIONS[DIRECTION_INDEX]['V']*STEP
|
||||||
|
MAP_POSITION_B[1] = MAP_POSITION_A[1] + DIRECTIONS[DIRECTION_INDEX]['H']*STEP
|
||||||
|
|
||||||
|
if bool(DIRECTIONS[DIRECTION_INDEX]['V']):
|
||||||
|
step = DIRECTIONS[DIRECTION_INDEX]['V']
|
||||||
|
for x in range(MAP_POSITION_A[0]+step, MAP_POSITION_B[0]+step, step):
|
||||||
|
if x not in MAPS_VISITED:
|
||||||
|
MAPS_VISITED[x] = []
|
||||||
|
if MAP_POSITION_A[1] in MAPS_VISITED[x]:
|
||||||
|
TWICE = [x, MAP_POSITION_A[1]]
|
||||||
|
break
|
||||||
|
MAPS_VISITED[x].append(MAP_POSITION_A[1])
|
||||||
|
if TWICE:
|
||||||
|
break
|
||||||
|
|
||||||
|
if bool(DIRECTIONS[DIRECTION_INDEX]['H']):
|
||||||
|
step = DIRECTIONS[DIRECTION_INDEX]['H']
|
||||||
|
for y in range(MAP_POSITION_A[1]+step, MAP_POSITION_B[1]+step, step):
|
||||||
|
if MAP_POSITION_A[0] not in MAPS_VISITED:
|
||||||
|
MAPS_VISITED[MAP_POSITION_A[0]] = []
|
||||||
|
if y in MAPS_VISITED[MAP_POSITION_A[0]]:
|
||||||
|
TWICE = [MAP_POSITION_A[0], y]
|
||||||
|
break
|
||||||
|
MAPS_VISITED[MAP_POSITION_A[0]].append(y)
|
||||||
|
if TWICE:
|
||||||
|
break
|
||||||
|
|
||||||
|
if TWICE:
|
||||||
|
print("%d" % (abs(TWICE[0]) + abs(TWICE[1])))
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
parser = argparse.ArgumentParser(description='AdventOfCode 2016 Day 01')
|
||||||
|
parser.add_argument('--input', '-i', action='store', required=True, help='input file')
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
main(args)
|
Loading…
x
Reference in New Issue
Block a user