2016 - Day 8 (Two-Factor Authentication)

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

#Advent of Code
#2016 Day 8, 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):
    d = [["." for i in range(50)] for x in range(6)]

    def rect(s, d):
        w, h = s.split("x")
        w = int(w)
        h = int(h)
        for x in range(w):
            for y in range(h):
                d[y][x] = "#"
        return d

    def rotate(roc, n, a, d):
        if roc == "row":
            cur = "".join(d[int(n)]) + "".join(d[int(n)])
            if int(a) > 0:
                cur = cur[50-int(a):100-int(a)]
            else:
                cur = cur[int(a):50+int(a)]
            d[int(n)] = list(cur)
        else:
            cur = ""
            for row in d:
                cur += row[int(n)]
            cur += cur
            if int(a) > 0:
                cur = cur[6-int(a):12-int(a)]
            else:
                cur = cur[int(a):6+int(a)]
            for p in range(len(cur)):
                d[p][int(n)] = cur[p]
        return d

    def printd(d):
        for row in d:
            print("".join(row))

    for inst in puzzle_input:
        if inst.split(" ")[0] == "rect":
            d = rect(inst.split(" ")[1], d)
        elif inst.split(" ")[0] == "rotate":
            d = rotate(inst.split(" ")[1], inst.split(" ")[2].split("=")[1], inst.split(" ")[4], d)

    lit = 0
    for i in d:
        for x in i:
            if x == "#":
                lit += 1

    return lit

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

    lit = solve(puzzle_input)

    print("The number of lit pixels is " + str(lit) + ".")

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

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

#Advent of Code
#2016 Day 8, 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):
    d = [["." for i in range(50)] for x in range(6)]

    def rect(s, d):
        w, h = s.split("x")
        w = int(w)
        h = int(h)
        for x in range(w):
            for y in range(h):
                d[y][x] = "#"
        return d

    def rotate(roc, n, a, d):
        if roc == "row":
            cur = "".join(d[int(n)]) + "".join(d[int(n)])
            if int(a) > 0:
                cur = cur[50-int(a):100-int(a)]
            else:
                cur = cur[int(a):50+int(a)]
            d[int(n)] = list(cur)
        else:
            cur = ""
            for row in d:
                cur += row[int(n)]
            cur += cur
            if int(a) > 0:
                cur = cur[6-int(a):12-int(a)]
            else:
                cur = cur[int(a):6+int(a)]
            for p in range(len(cur)):
                d[p][int(n)] = cur[p]
        return d

    def printd(d):
        for row in d:
            print("".join(row))

    for inst in puzzle_input:
        if inst.split(" ")[0] == "rect":
            d = rect(inst.split(" ")[1], d)
        elif inst.split(" ")[0] == "rotate":
            d = rotate(inst.split(" ")[1], inst.split(" ")[2].split("=")[1], inst.split(" ")[4], d)

    printd(d)

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

    solve(puzzle_input)

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

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