2017 - Day 1 (Inverse Captcha)

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
#!/usr/bin/env python3

#Advent of Code
#2017 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

def solve(puzzle_input):
    total = 0

    for i, x in enumerate(puzzle_input):
        if puzzle_input[i] == puzzle_input[(i+1) % len(puzzle_input)]:
            total += int(x)

    return total

def main():
    puzzle_input = util.read.as_string()

    total = solve(puzzle_input)

    print("The solution to the captcha is " + str(total) + ".")

class AOC_Tests(unittest.TestCase):
    def test_ex1(self):
        self.assertEqual(solve("1122"), 3)

    def test_ex2(self):
        self.assertEqual(solve("1111"), 4)

    def test_ex3(self):
        self.assertEqual(solve("1234"), 0)

    def test_ex4(self):
        self.assertEqual(solve("91212129"), 9)

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
#!/usr/bin/env python3

#Advent of Code
#2017 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

def solve(puzzle_input):
    total = 0
    half = int(len(puzzle_input) / 2)

    for i, x in enumerate(puzzle_input):
        if puzzle_input[i] == puzzle_input[(i+half) % len(puzzle_input)]:
            total += int(x)

    return total

def main():
    puzzle_input = util.read.as_string()

    total = solve(puzzle_input)

    print("The solution to the captcha is " + str(total) + ".")

class AOC_Tests(unittest.TestCase):
    def test_ex1(self):
        self.assertEqual(solve("1212"), 6)

    def test_ex2(self):
        self.assertEqual(solve("1221"), 0)

    def test_ex3(self):
        self.assertEqual(solve("123425"), 4)

    def test_ex4(self):
        self.assertEqual(solve("123123"), 12)

    def test_ex5(self):
        self.assertEqual(solve("12131415"), 4)

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