2016 - Day 1 (No Time for a Taxicab)

View puzzle description on AdventOfCode.com
Part 1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env python3

#Advent of Code
#2016 Day 1, Part 1
#Solution by James C. (https://github.com/JamesMCo)

import os, sys
sys.path.append(os.path.abspath("../.."))
import unittest, util.read
from util.tests import run

from math import fabs

def solve(puzzle_input):
    origin = (0, 0)
    here   = [0, 0]
    facing = 0 #N=0, E=1, S=2, W=3

    for i in puzzle_input:
        if i[0] == "L":
            facing -= 1
            if facing == -1: facing = 3
        else:
            facing += 1
            if facing == 4: facing = 0
        if facing == 0:
            here[1] += int(i[1:])
        elif facing == 1:
            here[0] += int(i[1:])
        elif facing == 2:
            here[1] -= int(i[1:])
        else:
            here[0] -= int(i[1:])

    dx = fabs(origin[0] - here[0])
    dy = fabs(origin[1] - here[1])
    return int(dx + dy)

def main():
    puzzle_input = util.read.as_string_list(", ")

    distance = solve(puzzle_input)

    print("The Easter Bunny HQ is " + str(distance) + " blocks away.")

class AOC_Tests(unittest.TestCase):
    def test_ex1(self):
        self.assertEqual(solve(["R2", "L3"]), 5)

    def test_ex2(self):
        self.assertEqual(solve(["R2", "R2", "R2"]), 2)

    def test_ex3(self):
        self.assertEqual(solve(["R5", "L5", "R5", "R3"]), 12)

if __name__ == "__main__":
    run(main)
Part 2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env python3

#Advent of Code
#2016 Day 1, Part 2
#Solution by James C. (https://github.com/JamesMCo)

import os, sys
sys.path.append(os.path.abspath("../.."))
import unittest, util.read
from util.tests import run

from math import fabs

def solve(puzzle_input):
    origin = (0, 0)
    here   = [0, 0]
    visit  = []
    facing = 0 #N=0, E=1, S=2, W=3

    for i in puzzle_input:
        if i[0] == "L":
            facing -= 1
            if facing == -1: facing = 3
        else:
            facing += 1
            if facing == 4: facing = 0
        moves = int(i[1:])
        while moves != 0:
            if   facing == 0: here[1] += 1
            elif facing == 1: here[0] += 1
            elif facing == 2: here[1] -= 1
            else:             here[0] -= 1
            moves -= 1
            if (here[0], here[1]) not in visit:
                visit.append((here[0], here[1]))
            else:
                dx = fabs(origin[0] - here[0])
                dy = fabs(origin[1] - here[1])
                return int(dx + dy)

def main():
    puzzle_input = util.read.as_string_list(", ")

    distance = solve(puzzle_input)

    print("The Easter Bunny HQ is at " + str(distance) + " blocks away.")

class AOC_Tests(unittest.TestCase):
    def test_ex1(self):
        self.assertEqual(solve(["R8", "R4", "R4", "R8"]), 4)

if __name__ == "__main__":
    run(main)
Return to solution index