2017 - Day 15 (Dueling Generators)

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

#Advent of Code
#2017 Day 15, 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):
    gena = puzzle_input[0]
    genb = puzzle_input[1]
    count = 0

    for i in range(40000000):
        gena = (gena * 16807) % 2147483647
        genb = (genb * 48271) % 2147483647

        if bin(gena)[2:].zfill(32)[16:] == bin(genb)[2:].zfill(32)[16:]:
            count += 1
    
    return count

def main():
    puzzle_input = [int(x.split(" ")[-1]) for x in util.read.as_lines()]

    count = solve(puzzle_input)

    print("The judge's final count is " + str(count) + ".")

class AOC_Tests(unittest.TestCase):
    def test_ex1(self):
        self.assertEqual(solve([65, 8921]), 588)

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

#Advent of Code
#2017 Day 15, 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):
    gena = puzzle_input[0]
    genb = puzzle_input[1]
    count = 0

    for i in range(5000000):
        loop = True
        while loop:
            gena = (gena * 16807) % 2147483647
            loop = gena % 4
        loop = True
        while loop:
            genb = (genb * 48271) % 2147483647
            loop = genb % 8
        
        if bin(gena)[2:].zfill(32)[16:] == bin(genb)[2:].zfill(32)[16:]:
            count += 1
    
    return count

def main():
    f = open("puzzle_input.txt")
    puzzle_input = [int(x.split(" ")[-1]) for x in util.read.as_lines()]
    f.close()

    count = solve(puzzle_input)

    print("The judge's final count is " + str(count) + ".")

class AOC_Tests(unittest.TestCase):
    def test_ex1(self):
        self.assertEqual(solve([65, 8921]), 309)

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