2015 - Day 7 (Some Assembly Required)

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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/env python3

#Advent of Code
#2015 Day 7, 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):
    def literal(x):
        l = True
        for c in x:
            if c not in "0123456789":
                l = False
                break
        return l

    f = open("puzzle_input.txt")
    puzzle_input = f.read().split("\n")
    f.close()

    wires = {}

    while len(puzzle_input) != 0:
        i = 0
        while i < len(puzzle_input):
            cur = puzzle_input[i]
            ins = cur.split("->")[0].rstrip().split(" ")
            out = cur.split("->")[1].lstrip()
            if len(ins) == 1:
                if literal(ins[0]):
                    wires[out] = int(ins[0])
                    puzzle_input.pop(i)
                elif ins[0] in wires:
                    wires[out] = wires[ins[0]]
                    puzzle_input.pop(i)
                else:
                    i += 1
            else:
                if ins[0] == "NOT":
                    if literal(ins[1]):
                        wires[out] = 65535 - int(ins[1])
                        puzzle_input.pop(i)
                    elif ins[1] in wires:
                        wires[out] = 65535 - wires[ins[1]]
                        puzzle_input.pop(i)
                    else:
                        i += 1
                else:
                    if literal(ins[0]):
                        val1 = int(ins[0])
                    elif ins[0] in wires:
                        val1 = wires[ins[0]]
                    else:
                        i += 1
                        continue
                    if literal(ins[2]):
                        val2 = int(ins[2])
                    elif ins[2] in wires:
                        val2 = wires[ins[2]]
                    else:
                        i += 1
                        continue

                    if ins[1] == "AND":
                        wires[out] = val1 & val2
                        puzzle_input.pop(i)
                    elif ins[1] == "OR":
                        wires[out] = val1 | val2
                        puzzle_input.pop(i)
                    elif ins[1] == "LSHIFT":
                        wires[out] = val1 << val2
                        puzzle_input.pop(i)
                    elif ins[1] == "RSHIFT":
                        wires[out] = val1 >> val2
                        puzzle_input.pop(i)
                    else:
                        i += 1

    return wires["a"]

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

    signal = solve(puzzle_input)

    print("The signal provided to wire a is " + str(signal) + "")

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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/usr/bin/env python3

#Advent of Code
#2015 Day 7, 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):
    def literal(x):
        l = True
        for c in x:
            if c not in "0123456789":
                l = False
                break
        return l

    f = open("puzzle_input.txt")
    puzzle_input = f.read().split("\n")
    puzzle_input_2 = puzzle_input[:]
    f.close()

    wires = {}

    while len(puzzle_input) != 0:
        i = 0
        while i < len(puzzle_input):
            cur = puzzle_input[i]
            ins = cur.split("->")[0].rstrip().split(" ")
            out = cur.split("->")[1].lstrip()
            if len(ins) == 1:
                if literal(ins[0]):
                    wires[out] = int(ins[0])
                    puzzle_input.pop(i)
                elif ins[0] in wires:
                    wires[out] = wires[ins[0]]
                    puzzle_input.pop(i)
                else:
                    i += 1
            else:
                if ins[0] == "NOT":
                    if literal(ins[1]):
                        wires[out] = 65535 - int(ins[1])
                        puzzle_input.pop(i)
                    elif ins[1] in wires:
                        wires[out] = 65535 - wires[ins[1]]
                        puzzle_input.pop(i)
                    else:
                        i += 1
                else:
                    if literal(ins[0]):
                        val1 = int(ins[0])
                    elif ins[0] in wires:
                        val1 = wires[ins[0]]
                    else:
                        i += 1
                        continue
                    if literal(ins[2]):
                        val2 = int(ins[2])
                    elif ins[2] in wires:
                        val2 = wires[ins[2]]
                    else:
                        i += 1
                        continue

                    if ins[1] == "AND":
                        wires[out] = val1 & val2
                        puzzle_input.pop(i)
                    elif ins[1] == "OR":
                        wires[out] = val1 | val2
                        puzzle_input.pop(i)
                    elif ins[1] == "LSHIFT":
                        wires[out] = val1 << val2
                        puzzle_input.pop(i)
                    elif ins[1] == "RSHIFT":
                        wires[out] = val1 >> val2
                        puzzle_input.pop(i)
                    else:
                        i += 1

    new_b = wires["a"]
    puzzle_input = puzzle_input_2[:]
    wires = {}

    while len(puzzle_input) != 0:
        i = 0
        while i < len(puzzle_input):
            cur = puzzle_input[i]
            ins = cur.split("->")[0].rstrip().split(" ")
            out = cur.split("->")[1].lstrip()
            if out == "b":
                wires["b"] = new_b
                puzzle_input.pop(i)
                continue
            if len(ins) == 1:
                if literal(ins[0]):
                    wires[out] = int(ins[0])
                    puzzle_input.pop(i)
                elif ins[0] in wires:
                    wires[out] = wires[ins[0]]
                    puzzle_input.pop(i)
                else:
                    i += 1
            else:
                if ins[0] == "NOT":
                    if literal(ins[1]):
                        wires[out] = 65535 - int(ins[1])
                        puzzle_input.pop(i)
                    elif ins[1] in wires:
                        wires[out] = 65535 - wires[ins[1]]
                        puzzle_input.pop(i)
                    else:
                        i += 1
                else:
                    if literal(ins[0]):
                        val1 = int(ins[0])
                    elif ins[0] in wires:
                        val1 = wires[ins[0]]
                    else:
                        i += 1
                        continue
                    if literal(ins[2]):
                        val2 = int(ins[2])
                    elif ins[2] in wires:
                        val2 = wires[ins[2]]
                    else:
                        i += 1
                        continue

                    if ins[1] == "AND":
                        wires[out] = val1 & val2
                        puzzle_input.pop(i)
                    elif ins[1] == "OR":
                        wires[out] = val1 | val2
                        puzzle_input.pop(i)
                    elif ins[1] == "LSHIFT":
                        wires[out] = val1 << val2
                        puzzle_input.pop(i)
                    elif ins[1] == "RSHIFT":
                        wires[out] = val1 >> val2
                        puzzle_input.pop(i)
                    else:
                        i += 1

    return wires["a"]

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

    signal = solve(puzzle_input)

    print("The signal provided to wire a is " + str(signal) + "")

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

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