Last active
December 2, 2016 03:09
-
-
Save cjauvin/b791a79e9e3688a3b32a29439217645e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# http://adventofcode.com/2016/day/1 | |
import re | |
import numpy as np | |
def taxi_dist(S): | |
p = [0, 0] | |
i = 0 | |
d = 1 | |
for x in S: | |
if (i == 0 and x[0] == 'L') or (i == 1 and x[0] == 'R'): | |
d *= -1 | |
p[i] += int(x[1:]) * d | |
i += 1 | |
i %= 2 | |
return np.sum(np.abs(p)) | |
def visit_twice(S): | |
p = [0, 0] | |
q = [0, 0] | |
i = 0 | |
d = 1 | |
Q = {tuple(q)} | |
for x in S: | |
if (i == 0 and x[0] == 'L') or (i == 1 and x[0] == 'R'): | |
d *= -1 | |
e = int(x[1:]) | |
j = i + 1 | |
j %= 2 | |
for k in range(e): | |
p[i] += d | |
q[j] += d | |
if tuple(q) in Q: | |
return np.sum(np.abs(p)) | |
Q.add(tuple(q)) | |
i += 1 | |
i %= 2 | |
s = "R3, L5, R1, R2, L5, R2, R3, L2, L5, R5, L4, L3, R5, L1, R3, R4, R1, L3, R3, L2, L5, L2, R4, R5, R5, L4, L3, L3, R4, R4, R5, L5, L3, R2, R2, L3, L4, L5, R1, R3, L3, R2, L3, R5, L194, L2, L5, R2, R1, R1, L1, L5, L4, R4, R2, R2, L4, L1, R2, R53, R3, L5, R72, R2, L5, R3, L4, R187, L4, L5, L2, R1, R3, R5, L4, L4, R2, R5, L5, L4, L3, R5, L2, R1, R1, R4, L1, R2, L3, R5, L4, R2, L3, R1, L4, R4, L1, L2, R3, L1, L1, R4, R3, L4, R2, R5, L2, L3, L3, L1, R3, R5, R2, R3, R1, R2, L1, L4, L5, L2, R4, R5, L2, R4, R4, L3, R2, R1, L4, R3, L3, L4, L3, L1, R3, L2, R2, L4, L4, L5, R3, R5, R3, L2, R5, L2, L1, L5, L1, R2, R4, L5, R2, L4, L5, L4, L5, L2, L5, L4, R5, R3, R2, R2, L3, R3, L2, L5" | |
#s = "R2, L3" | |
# s = "R2 R2 R2" | |
# s = "R5, L5, R5, R3" | |
# s = "R8, R4, R4, R8" | |
S = re.split('\W+', s) | |
print(taxi_dist(S)) | |
print(visit_twice(S)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment