2017 - Day 9 šŸŽ‚ (Stream Processing)

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

#Advent of Code
#2017 Day 9, 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):
    depth = 0
    total = 0

    i = 0
    while i < len(puzzle_input):
        if puzzle_input[i] == "!":
            puzzle_input = puzzle_input[:i] + puzzle_input[i+2:]
        else:
            i += 1        

    i = 0
    in_garbage = False
    while i < len(puzzle_input):
        if in_garbage:
            if puzzle_input[i] == ">":
                in_garbage = False
            puzzle_input = puzzle_input[:i] + puzzle_input[i+1:]
        elif puzzle_input[i] == "<":
            in_garbage = True
            puzzle_input = puzzle_input[:i] + puzzle_input[i+1:]
        else:
            i += 1

    for i in puzzle_input:
        if i == "{":
            depth += 1
            total += depth
        elif i == "}":
            if depth > 0:
                depth -= 1

    return total

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

    score = solve(puzzle_input)

    print("The total score for all groups in the input is " + str(score) + ".")

class AOC_Tests(unittest.TestCase):
    def test_ex1(self):
        self.assertEqual(solve("{}"), 1)

    def test_ex2(self):
        self.assertEqual(solve("{{{}}}"), 6)

    def test_ex3(self):
        self.assertEqual(solve("{{},{}}"), 5)

    def test_ex4(self):
        self.assertEqual(solve("{{{},{},{{}}}}"), 16)

    def test_ex5(self):
        self.assertEqual(solve("{<a>,<a>,<a>,<a>}"), 1)

    def test_ex6(self):
        self.assertEqual(solve("{{<ab>},{<ab>},{<ab>},{<ab>}}"), 9)

    def test_ex7(self):
        self.assertEqual(solve("{{<!!>},{<!!>},{<!!>},{<!!>}}"), 9)

    def test_ex8(self):
        self.assertEqual(solve("{{<a!>},{<a!>},{<a!>},{<ab>}}"), 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python3

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

    i = 0
    while i < len(puzzle_input):
        if puzzle_input[i] == "!":
            puzzle_input = puzzle_input[:i] + puzzle_input[i+2:]
        else:
            i += 1        

    i = 0
    in_garbage = False
    while i < len(puzzle_input):
        if in_garbage:
            if puzzle_input[i] == ">":
                in_garbage = False
            else:
                count += 1
        elif puzzle_input[i] == "<":
            in_garbage = True
        i += 1

    return count

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

    count = solve(puzzle_input)

    print("The number of non-cancelled charcters whithin the garbage is " + str(count) + ".")

class AOC_Tests(unittest.TestCase):
    def test_ex1(self):
        self.assertEqual(solve("<>"), 0)

    def test_ex2(self):
        self.assertEqual(solve("<random characters>"), 17)

    def test_ex3(self):
        self.assertEqual(solve("<<<<>"), 3)

    def test_ex4(self):
        self.assertEqual(solve("<{!>}>"), 2)

    def test_ex5(self):
        self.assertEqual(solve("<!!>"), 0)

    def test_ex6(self):
        self.assertEqual(solve("<!!!>>"), 0)

    def test_ex7(self):
        self.assertEqual(solve("<{o\"i!a,<{i<a>"), 10)

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