2017 - Day 23 (Coprocessor Conflagration)

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

#Advent of Code
#2017 Day 23, 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

import string

def solve(puzzle_input):
    i = 0
    mul_count = 0
    registers = [0 for x in range(8)]

    while 0 <= i < len(puzzle_input):
        inst        = puzzle_input[i].split()
        op          = inst[0]

        try:
            reg     = int(inst[1])
            reg_num = True
        except:
            reg     = ord(inst[1]) - 97
            reg_num = False

        if len(inst) == 3:
            if inst[2] in string.ascii_lowercase:
                val = registers[ord(inst[2]) - 97]
            else:
                val = int(inst[2])
        else:
            val = None
        
        
        if op == "set":
            registers[reg]  = val
        elif op == "sub":
            registers[reg] -= val
        elif op == "mul":
            registers[reg] *= val
            mul_count += 1
        elif op == "jnz":
            if reg_num:
                if reg != 0:
                    i += val
                    continue
            else:
                if registers[reg] != 0:
                    i += val
                    continue
        i += 1

    return mul_count

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

    muls = solve(puzzle_input)

    print("The mul instruction is invoked " + str(muls) + " times.")

class AOC_Tests(unittest.TestCase):
    def test_ex1(self):
        self.assertEqual(solve(["mul a 1",
                                "mul a 1",
                                "mul a 1"]), 3)

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

#Advent of Code
#2017 Day 23, 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):
    h = 0

    start = (int(puzzle_input[0].split()[2]) * 100) + 100000
    end = start + 17000
    step = -int(puzzle_input[-2].split()[2])

    for i in range(start, end + 1, step):
        prime = True
        for j in range(2, int(i**0.5)):
            if i % j == 0:
                prime = False
                break
        h += not prime
    
    return h

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

    h = solve(puzzle_input)

    print("The value of register h is " + str(h) + ".")

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

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