2016 - Day 4 (Security Through Obscurity)

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

#Advent of Code
#2016 Day 4, 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 collections import Counter

def solve(puzzle_input):
    sector_ids = 0

    def getchecksum(s):
        c = Counter(s)
        d = {}
        for l in c:
            if c[l] in d:
                d[c[l]] += l
            else:
                d[c[l]] = l
        f = ""
        for i in sorted(d, reverse=True):
            f += "".join(sorted(list(d[i])))
        return f[:5]

    for room in puzzle_input:
        if room == "": continue
        working = "".join(sorted(room.split("-")[:-1]))
        if getchecksum(working) == room.split("[")[1][:-1]:
            sector_ids += int(room.split("-")[-1][:3])

    return sector_ids

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

    sector_ids = solve(puzzle_input)

    print("The sum of the sector IDs is " + str(sector_ids) + ".")

class AOC_Tests(unittest.TestCase):
    def test_ex1(self):
        self.assertEqual(solve(["aaaaa-bbb-z-y-x-123[abxyz]",
                                "a-b-c-d-e-f-g-h-987[abcde]",
                                "not-a-real-room-404[oarel]",
                                "totally-real-room-200[decoy]"]), 1514)

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
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python3

#Advent of Code
#2016 Day 4, 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 collections import Counter

def solve(puzzle_input):
    valid = []

    def getchecksum(s):
        c = Counter(s)
        d = {}
        for l in c:
            if c[l] in d:
                d[c[l]] += l
            else:
                d[c[l]] = l
        f = ""
        for i in sorted(d, reverse=True):
            f += "".join(sorted(list(d[i])))
        return f[:5]

    def shift(s, k):
        k %= 26
        alpha = "abcdefghijklmnopqrstuvwxyz" * 2
        alpha += alpha.upper()
        def get_i():
            for i in range(26):
                yield i
            for i in range(53, 78):
                yield i
        ROT = {alpha[i]: alpha[i + k] for i in get_i()}
        return "".join(ROT.get(i, i) for i in s)

    for room in puzzle_input:
        if room == "": continue
        working = "".join(sorted(room.split("-")[:-1]))
        if getchecksum(working) == room.split("[")[1][:-1]:
            valid.append(room)

    for room in valid:
        cur = shift("-".join(sorted(room.split("-")[:-1])), int(room.split("-")[-1][:3]))
        if "north" in cur or "pole" in cur:
            return int(room.split("-")[3].split("[")[0])

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

    room = solve(puzzle_input)

    print("The room with the North Pole objects is " + str(room) + ".")

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

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