From d84be96510c179e22d4af2d90d228052712e72c8 Mon Sep 17 00:00:00 2001 From: Peter Hudec Date: Mon, 15 Jan 2018 23:53:47 +0100 Subject: [PATCH] day 01 --- 01/input.puzzle01 | 1 + 01/input.sample01 | 1 + 01/input.sample02 | 1 + 01/input.sample03 | 1 + 01/input.sample04 | 1 + 01/part01.py | 46 ++++++++++++++++++++++++++++ 01/part02.py | 78 +++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 129 insertions(+) create mode 100644 01/input.puzzle01 create mode 100644 01/input.sample01 create mode 100644 01/input.sample02 create mode 100644 01/input.sample03 create mode 100644 01/input.sample04 create mode 100755 01/part01.py create mode 100755 01/part02.py diff --git a/01/input.puzzle01 b/01/input.puzzle01 new file mode 100644 index 0000000..4b9fef2 --- /dev/null +++ b/01/input.puzzle01 @@ -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 diff --git a/01/input.sample01 b/01/input.sample01 new file mode 100644 index 0000000..bf8e71a --- /dev/null +++ b/01/input.sample01 @@ -0,0 +1 @@ +R2, L3 diff --git a/01/input.sample02 b/01/input.sample02 new file mode 100644 index 0000000..214a0ca --- /dev/null +++ b/01/input.sample02 @@ -0,0 +1 @@ +R2, R2, R2 diff --git a/01/input.sample03 b/01/input.sample03 new file mode 100644 index 0000000..00c8c89 --- /dev/null +++ b/01/input.sample03 @@ -0,0 +1 @@ +R5, L5, R5, R3 diff --git a/01/input.sample04 b/01/input.sample04 new file mode 100644 index 0000000..1d6615f --- /dev/null +++ b/01/input.sample04 @@ -0,0 +1 @@ +R8, R4, R4, R8 diff --git a/01/part01.py b/01/part01.py new file mode 100755 index 0000000..063073c --- /dev/null +++ b/01/part01.py @@ -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) diff --git a/01/part02.py b/01/part02.py new file mode 100755 index 0000000..4a534a6 --- /dev/null +++ b/01/part02.py @@ -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)