2016 - Day 16 (Dragon Checksum)

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

#Advent of Code
#2016 Day 16, 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, required_length=272):
    state = puzzle_input

    def step(a):
        return a + "0" + a[::-1].replace("1", "-").replace("0", "1").replace("-", "0")

    def checksum(a):
        o = ""
        for i in range(0, len(a)-1, 2):
            if a[i] == a[i+1]:
                o += "1"
            else:
                o += "0"
        if len(o) % 2 == 0:
            return checksum(o)
        return o

    while len(state) < required_length:
        state = step(state)

    return checksum(state[:required_length])

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

    checksum = solve(puzzle_input)

    print("The correct checksum is " + str(checksum) + ".")

class AOC_Tests(unittest.TestCase):
    def test_ex1(self):
        self.assertEqual(solve("10000", 20), "01100")

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

#Advent of Code
#2016 Day 16, 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, required_length=35651584):
    state = puzzle_input

    def step(a):
        return a + "0" + a[::-1].replace("1", "-").replace("0", "1").replace("-", "0")

    def checksum(a):
        o = ""
        for i in range(0, len(a)-1, 2):
            if a[i] == a[i+1]:
                o += "1"
            else:
                o += "0"
        if len(o) % 2 == 0:
            return checksum(o)
        return o

    while len(state) < required_length:
        state = step(state)

    return checksum(state[:required_length])

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

    checksum = solve(puzzle_input)

    print("The correct checksum is " + str(checksum) + ".")

class AOC_Tests(unittest.TestCase):
    @unittest.skip("No test cases")
    def test_ex1(self):
        pass

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