Browse Source

Day 1

master
Chris Smith 7 years ago
commit
5011f99970
2 changed files with 34 additions and 0 deletions
  1. 33
    0
      01.py
  2. 1
    0
      01.txt

+ 33
- 0
01.py View File

@@ -0,0 +1,33 @@
1
+#!/usr/bin/python
2
+
3
+from enum import IntEnum
4
+import math
5
+import operator
6
+
7
+def steps(start, delta, count):
8
+    sign = int(math.copysign(1, delta))
9
+    return [start] * count if delta == 0 else range(start + sign, start + delta + sign, sign)
10
+
11
+
12
+with open('01.txt', 'r') as file:
13
+    input = file.read()
14
+
15
+movement_lookup = {'L': -1, 'R': +1}
16
+direction_lookup = {0: (0, -1), 1:  (1,  0), 2: (0,  1), 3:  (-1, 0)}
17
+
18
+moves = [(movement_lookup[move[0]], int(move[1:])) for move in input.split(', ')]
19
+
20
+heading = 0
21
+history = [(0, 0)]
22
+for move in moves:
23
+    heading = (heading + move[0] + 4) % 4
24
+    delta = map(operator.mul, direction_lookup[heading], (move[1], move[1]))
25
+    delta_mag = max(abs(delta[0]), abs(delta[1]))
26
+    history += zip(steps(history[-1][0], delta[0], delta_mag),
27
+                   steps(history[-1][1], delta[1], delta_mag))
28
+
29
+duplicates = (x for n, x in enumerate(history) if history[:n].count(x) > 0)
30
+overlap = next(duplicates)
31
+
32
+print("Part 1: %s ==> %s" % (history[-1], abs(history[-1][0]) + abs(history[-1][1])))
33
+print("Part 2: %s ==> %s" % (overlap, abs(overlap[0]) + abs(overlap[1])))

+ 1
- 0
01.txt View File

@@ -0,0 +1 @@
1
+L5, R1, L5, L1, R5, R1, R1, L4, L1, L3, R2, R4, L4, L1, L1, R2, R4, R3, L1, R4, L4, L5, L4, R4, L5, R1, R5, L2, R1, R3, L2, L4, L4, R1, L192, R5, R1, R4, L5, L4, R5, L1, L1, R48, R5, R5, L2, R4, R4, R1, R3, L1, L4, L5, R1, L4, L2, L5, R5, L2, R74, R4, L1, R188, R5, L4, L2, R5, R2, L4, R4, R3, R3, R2, R1, L3, L2, L5, L5, L2, L1, R1, R5, R4, L3, R5, L1, L3, R4, L1, L3, L2, R1, R3, R2, R5, L3, L1, L1, R5, L4, L5, R5, R2, L5, R2, L1, L5, L3, L5, L5, L1, R1, L4, L3, L1, R2, R5, L1, L3, R4, R5, L4, L1, R5, L1, R5, R5, R5, R2, R1, R2, L5, L5, L5, R4, L5, L4, L4, R5, L2, R1, R5, L1, L5, R4, L3, R4, L2, R3, R3, R3, L2, L2, L2, L1, L4, R3, L4, L2, R2, R5, L1, R2

Loading…
Cancel
Save