2017 - Day 14 (Disk Defragmentation)

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env python3

#Advent of Code
#2017 Day 14, 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 knot(puzzle_input):
    def rotate(l, n):
        t = [x for x in l]
        if len(t) <= 1:
            return t
        if len(t) == 2:
            return t[1] + t[0]
        for i in range(n):
            t = t[1:] + [t[0]]
        return t
    
    puzzle_input = [ord(x) for x in puzzle_input] + [17,31,73,47,23]
    l = [x for x in range(256)]
    skip_size = 0
    total_rotations = 0

    for j in range(64):
        for i in puzzle_input:
            if i <= 256:
                l = l[i-1::-1] + l[i:]
            else:
                l = l[::-1]
            l = rotate(l, i + skip_size)
            total_rotations += i + skip_size
            skip_size += 1

    l = rotate(l, 256 - (total_rotations % 256))

    output = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
    for i in range(256):
        if i % 16 == 0:
            output[int(i / 16)] = l[i]
        else:
            output[int(i / 16)] ^= l[i]
        i += 1
    return "".join(hex(x)[2:].zfill(2) for x in output)

def solve(puzzle_input):
    base = puzzle_input + "-"
    grid = []
    used = 0

    for i in range(128):
        grid.append(bin(int(knot(base + str(i)), 16))[2:])

    for row in grid:
        used += row.count("1")
    
    return used

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

    used = solve(puzzle_input)

    print("The number of squares used is " + str(used) + ".")

class AOC_Tests(unittest.TestCase):
    def test_ex1(self):
        self.assertEqual(solve("flqrgnkx"), 8108)

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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/env python3

#Advent of Code
#2017 Day 14, 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 knot(puzzle_input):
    def rotate(l, n):
        t = [x for x in l]
        if len(t) <= 1:
            return t
        if len(t) == 2:
            return t[1] + t[0]
        for i in range(n):
            t = t[1:] + [t[0]]
        return t

    puzzle_input = [ord(x) for x in puzzle_input] + [17,31,73,47,23]
    l = [x for x in range(256)]
    skip_size = 0
    total_rotations = 0

    for j in range(64):
        for i in puzzle_input:
            if i <= 256:
                l = l[i-1::-1] + l[i:]
            else:
                l = l[::-1]
            l = rotate(l, i + skip_size)
            total_rotations += i + skip_size
            skip_size += 1

    l = rotate(l, 256 - (total_rotations % 256))

    output = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
    for i in range(256):
        if i % 16 == 0:
            output[int(i / 16)] = l[i]
        else:
            output[int(i / 16)] ^= l[i]
        i += 1
    return "".join(hex(x)[2:].zfill(2) for x in output)

def day12_p2(puzzle_input):
    programs = {}

    def getProgramById(pName):
        if pName in programs:
            return programs[pName]
        else:
            return Program(pName)

    class Program:
        def __init__(self, pName, neighbours):
            self.pName = pName
            self.neighbours = neighbours
            self.group = None
            self.updating = False

            programs[pName] = self

        def updateNeighbours(self):
            self.updating = True

            for pName in self.neighbours:
                neighbour = getProgramById(pName)
                if not neighbour.updating and neighbour.group != self.group:
                    neighbour.group = self.group
                    neighbour.updateNeighbours()

            self.updating = False

    for p in puzzle_input:
        Program(p.split(" ")[0], " ".join(p.split(" ")[2:]).split(", "))

    i = 0
    for pName in programs:
        p = getProgramById(pName)
        if p.group == None:
            p.group = i
            p.updateNeighbours()
            i += 1

    return i

def solve(puzzle_input):
    base = puzzle_input + "-"
    grid = []
    d12_input = []

    for i in range(128):
        grid.append(bin(int(knot(base + str(i)), 16))[2:].zfill(128).replace("0", ".").replace("1", "#"))

    for y in range(128):
        for x in range(128):
            if grid[y][x] == "#":
                d12_input.append(f"[{x}|{y}] <-> ")
                if y != 0:
                    if grid[y-1][x] == "#":
                            d12_input[-1] += f"[{x}|{y-1}], "
                if x != 0:
                    if grid[y][x-1] == "#":
                        d12_input[-1] += f"[{x-1}|{y}], "
                d12_input[-1] += f"[{x}|{y}], "
                if x != 127:
                    if grid[y][x+1] == "#":
                        d12_input[-1] += f"[{x+1}|{y}], "
                if y != 127:
                    if grid[y+1][x] == "#":
                            d12_input[-1] += f"[{x}|{y+1}], "
                d12_input[-1] = d12_input[-1][:-2]

    return day12_p2(d12_input)

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

    regions = solve(puzzle_input)

    print("The number of regions in the grid is " + str(regions) + ".")

class AOC_Tests(unittest.TestCase):
    def test_ex1(self):
        self.assertEqual(solve("flqrgnkx"), 1242)

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