Solve 2015:12 "JSAbacusFramework.io"
This commit is contained in:
parent
761d00b552
commit
6a1b231552
2 changed files with 77 additions and 0 deletions
38
2015-python/solutions/day_12.py
Normal file
38
2015-python/solutions/day_12.py
Normal file
|
|
@ -0,0 +1,38 @@
|
||||||
|
import json
|
||||||
|
import re
|
||||||
|
from solutions import BaseSolution
|
||||||
|
|
||||||
|
|
||||||
|
class Solution(BaseSolution):
|
||||||
|
input_file = "12.txt"
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return "Day 12: JSAbacusFramework.io"
|
||||||
|
|
||||||
|
def parse_input(self, data):
|
||||||
|
return data.strip()
|
||||||
|
|
||||||
|
def solve(self, puzzle_input):
|
||||||
|
digits = re.compile(r"-?\d+")
|
||||||
|
return sum(map(int, re.findall(digits, puzzle_input)))
|
||||||
|
|
||||||
|
def solve_again(self, puzzle_input):
|
||||||
|
def nodesum(node):
|
||||||
|
if isinstance(node, int):
|
||||||
|
return node
|
||||||
|
if isinstance(node, str):
|
||||||
|
return 0
|
||||||
|
if isinstance(node, list):
|
||||||
|
return sum(nodesum(i) for i in node)
|
||||||
|
if isinstance(node, object):
|
||||||
|
if "red" in node.values():
|
||||||
|
return 0
|
||||||
|
return sum(nodesum(i) for i in node.values())
|
||||||
|
|
||||||
|
nodes = json.loads(puzzle_input)
|
||||||
|
return nodesum(nodes)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
solution = Solution()
|
||||||
|
solution.show_results()
|
||||||
39
2015-python/tests/test_day_12.py
Normal file
39
2015-python/tests/test_day_12.py
Normal file
|
|
@ -0,0 +1,39 @@
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
from solutions.day_12 import Solution
|
||||||
|
|
||||||
|
|
||||||
|
class Day12TestCase(unittest.TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
self.solution = Solution()
|
||||||
|
self.puzzle_input = self.solution.parse_input(
|
||||||
|
"""
|
||||||
|
<REPLACE ME>
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_parse_puzzle_input(self):
|
||||||
|
data = """
|
||||||
|
<REPLACE ME>
|
||||||
|
""".strip()
|
||||||
|
assert self.solution.parse_input(data) == "<REPLACE ME>"
|
||||||
|
|
||||||
|
def test_solve_first_part(self):
|
||||||
|
assert self.solution.solve("[1,2,3]") == 6
|
||||||
|
assert self.solution.solve('{"a":2,"b":4}') == 6
|
||||||
|
assert self.solution.solve("[[[3]]]") == 3
|
||||||
|
assert self.solution.solve('{"a":{"b":4},"c":-1}') == 3
|
||||||
|
assert self.solution.solve('{"a":[-1,1]}') == 0
|
||||||
|
assert self.solution.solve('-1,{"a":1}]') == 0
|
||||||
|
assert self.solution.solve("{}") == 0
|
||||||
|
assert self.solution.solve("[]") == 0
|
||||||
|
|
||||||
|
def test_solve_second_part(self):
|
||||||
|
assert self.solution.solve_again("[1,2,3]") == 6
|
||||||
|
assert self.solution.solve_again('[1,{"c":"red","b":2},3]') == 4
|
||||||
|
assert self.solution.solve_again('{"d":"red","e":[1,2,3,4],"f":5}') == 0
|
||||||
|
assert self.solution.solve_again('[1,"red",5]') == 6
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
unittest.main()
|
||||||
Loading…
Add table
Reference in a new issue