From 16d40ab16324e7013ef5fe8191a12d9ed656cc5b Mon Sep 17 00:00:00 2001 From: Stegallo Date: Thu, 2 Nov 2023 20:50:46 -0700 Subject: [PATCH] 2015 d1 --- tests/y_2015/test_2015_day1.py | 52 +++++++++--- tests/y_2015/test_2015_day2.py | 11 --- tests/y_2015/test_2015_day3.py | 13 --- tests/y_2015/test_2015_day4.py | 13 --- tests/y_2015/test_2015_day5.py | 30 ------- tests/y_2015/test_2015_day6.py | 43 ---------- tests/y_2015/test_2015_day7.py | 143 --------------------------------- y_2015/day1.py | 26 +++--- 8 files changed, 56 insertions(+), 275 deletions(-) delete mode 100644 tests/y_2015/test_2015_day2.py delete mode 100644 tests/y_2015/test_2015_day3.py delete mode 100644 tests/y_2015/test_2015_day4.py delete mode 100644 tests/y_2015/test_2015_day5.py delete mode 100644 tests/y_2015/test_2015_day6.py delete mode 100644 tests/y_2015/test_2015_day7.py diff --git a/tests/y_2015/test_2015_day1.py b/tests/y_2015/test_2015_day1.py index 6abbf82..cbafecf 100644 --- a/tests/y_2015/test_2015_day1.py +++ b/tests/y_2015/test_2015_day1.py @@ -1,19 +1,45 @@ -from y_2015.day1 import calculate_1, calculate_2 +from __future__ import annotations + +from unittest.mock import mock_open, patch + +from y_2015.day1 import Day + +with patch("builtins.open", mock_open(read_data="0")): + day = Day() def test_calculate_1(): - assert calculate_1("(())") == 0 - assert calculate_1("()()") == 0 - assert calculate_1("(((") == 3 - assert calculate_1("(()(()(") == 3 - assert calculate_1("))(((((") == 3 - assert calculate_1("())") == -1 - assert calculate_1("))(") == -1 - assert calculate_1(")))") == -3 - assert calculate_1(")())())") == -3 + day._Day__input_data = "(())" + assert day._calculate_1() == 0 + + day._Day__input_data = "(((" + assert day._calculate_1() == 3 + + day._Day__input_data = "(()(()(" + assert day._calculate_1() == 3 + + day._Day__input_data = "))(((((" + assert day._calculate_1() == 3 + + day._Day__input_data = "())" + assert day._calculate_1() == -1 + + day._Day__input_data = "))(" + assert day._calculate_1() == -1 + + day._Day__input_data = ")))" + assert day._calculate_1() == -3 + + day._Day__input_data = ")())())" + assert day._calculate_1() == -3 def test_calculate_2(): - assert calculate_2(")") == 1 - assert calculate_2("()())") == 5 - assert calculate_2("(") == -1 + day._Day__input_data = ")" + assert day._calculate_2() == 1 + + day._Day__input_data = "()())" + assert day._calculate_2() == 5 + + day._Day__input_data = "(" + assert day._calculate_2() == -1 diff --git a/tests/y_2015/test_2015_day2.py b/tests/y_2015/test_2015_day2.py deleted file mode 100644 index 11d847c..0000000 --- a/tests/y_2015/test_2015_day2.py +++ /dev/null @@ -1,11 +0,0 @@ -from y_2015.day2 import calculate_1, calculate_2 - - -def test_calculate_1(): - assert calculate_1("2x3x4") == 58 - assert calculate_1("1x1x10") == 43 - - -def test_calculate_2(): - assert calculate_2("2x3x4") == 34 - assert calculate_2("1x1x10") == 14 diff --git a/tests/y_2015/test_2015_day3.py b/tests/y_2015/test_2015_day3.py deleted file mode 100644 index df2abfc..0000000 --- a/tests/y_2015/test_2015_day3.py +++ /dev/null @@ -1,13 +0,0 @@ -from y_2015.day3 import calculate_1, calculate_2 - - -def test_calculate_1(): - assert calculate_1(">") == 2 - assert calculate_1("^>v<") == 4 - assert calculate_1("^v^v^v^v^v") == 2 - - -def test_calculate_2(): - assert calculate_2("^v") == 3 - assert calculate_2("^>v<") == 3 - assert calculate_2("^v^v^v^v^v") == 11 diff --git a/tests/y_2015/test_2015_day4.py b/tests/y_2015/test_2015_day4.py deleted file mode 100644 index a7bce6f..0000000 --- a/tests/y_2015/test_2015_day4.py +++ /dev/null @@ -1,13 +0,0 @@ -from y_2015.day4 import calculate_1, calculate_2 - - -def test_calculate_1(): - return # sourcery skip: remove-unreachable-code - assert calculate_1("abcdef") == 609043 - assert calculate_1("pqrstuv") == 1048970 - - -def test_calculate_2(): - return # sourcery skip: remove-unreachable-code - assert calculate_2("abcdef") == 6742839 - assert calculate_2("pqrstuv") == 5714438 diff --git a/tests/y_2015/test_2015_day5.py b/tests/y_2015/test_2015_day5.py deleted file mode 100644 index 44164e2..0000000 --- a/tests/y_2015/test_2015_day5.py +++ /dev/null @@ -1,30 +0,0 @@ -from y_2015.day5 import calculate_1, calculate_2, repeated, twiced - - -def test_calculate_1(): - assert calculate_1("ugknbfddgicrmopn") == 1 - assert calculate_1("aaa") == 1 - assert calculate_1("jchzalrnumimnmhp") == 0 - assert calculate_1("haegwjzuvuyypxyu") == 0 - assert calculate_1("dvszwmarrgswjxmb") == 0 - - -def test_twiced(): - assert twiced("xyxy") is True - assert twiced("aabcdefgaa") is True - assert twiced("aaa") is False - assert twiced("aaaa") is True - - -def test_repeated(): - assert repeated("xyx") is True - assert repeated("abcdefeghi") is True - assert repeated("aaa") is True - - -def test_calculate_2(): - assert calculate_2("qjhvhtzxzqqjkmpb") == 1 - assert calculate_2("xxyxx") == 1 - assert calculate_2("aaaa") == 1 - assert calculate_2("uurcxstgmygtbstg") == 0 - assert calculate_2("ieodomkazucvgmuy") == 0 diff --git a/tests/y_2015/test_2015_day6.py b/tests/y_2015/test_2015_day6.py deleted file mode 100644 index 6ee62f6..0000000 --- a/tests/y_2015/test_2015_day6.py +++ /dev/null @@ -1,43 +0,0 @@ -from unittest.mock import patch - -from y_2015.day6 import calculate_1, calculate_2, defaultdict, parse - - -def test_parse(): - assert parse("turn on 0,0 through 999,999") == ( - "turn on", - (0, 0), - "through", - (999, 999), - ) - assert parse("toggle 0,0 through 999,0") == ("toggle", (0, 0), "through", (999, 0)) - - -def test_calculate_1_all_off_turn_on(): - with patch("y_2015.day6.GRID", defaultdict(int)): - assert calculate_1(["turn on 0,0 through 999,999"]) == 1000000 - - -def test_calculate_1_all_on_toggle(): - with patch("y_2015.day6.GRID", defaultdict(int)): - assert calculate_1(["toggle 0,0 through 999,0"]) == 1000 - - -def test_calculate_1_all_on_turn_off(): - with patch("y_2015.day6.GRID", defaultdict(int)): - assert calculate_1(["turn off 499,499 through 500,500"]) == 0 - - -def test_calculate_2_first(): - with patch("y_2015.day6.GRID", defaultdict(int)): - assert calculate_2(["turn on 0,0 through 0,0"]) == 1 - - -def test_calculate_2_second(): - with patch("y_2015.day6.GRID", defaultdict(int)): - assert calculate_2(["toggle 0,0 through 999,999"]) == 2000000 - - -def test_calculate_2_all_on_turn_off(): - with patch("y_2015.day6.GRID", defaultdict(int)): - assert calculate_2(["turn off 499,499 through 500,500"]) == 0 diff --git a/tests/y_2015/test_2015_day7.py b/tests/y_2015/test_2015_day7.py deleted file mode 100644 index 0fe1099..0000000 --- a/tests/y_2015/test_2015_day7.py +++ /dev/null @@ -1,143 +0,0 @@ -from unittest.mock import patch - -from y_2015.day7 import Container, calculate_1, calculate_2, defaultdict, inner_1, parse - - -def test_parse(): - assert parse("123 -> x") == ["x", "123"] - assert parse("x AND y -> z") == ["z", "AND", "x", "y"] - assert parse("p LSHIFT 2 -> q") == ["q", "LSHIFT", "p", "2"] - assert parse("NOT e -> f") == ["f", "NOT", "e"] - assert parse("x OR y -> z") == ["z", "OR", "x", "y"] - assert parse("p RSHIFT 2 -> q") == ["q", "RSHIFT", "p", "2"] - - -def test_inner_1_value(): - with patch("y_2015.day7.WIRING", defaultdict(Container)): - assert inner_1( - [ - "123 -> x", - "456 -> y", - ] - ) == {"x": 123, "y": 456} - - -def test_inner_1_direct(): - with patch("y_2015.day7.WIRING", defaultdict(Container)): - assert inner_1(["123 -> x", "x -> y"]) == {"x": 123, "y": 123} - - -def test_inner_1_binary(): - with patch("y_2015.day7.WIRING", defaultdict(Container)): - assert inner_1(["123 AND 456 -> d"]) == {"d": 72} - assert inner_1(["123 OR 456 -> d"]) == {"d": 507} - assert inner_1(["123 LSHIFT 2 -> d"]) == {"d": 492} - assert inner_1(["456 RSHIFT 2 -> d"]) == {"d": 114} - - -def test_inner_1_unary(): - with patch("y_2015.day7.WIRING", defaultdict(Container)): - assert inner_1(["NOT 123 -> h"]) == {"h": 65412} - - -def test_inner_1(): - with patch("y_2015.day7.WIRING", defaultdict(Container)): - assert inner_1( - [ - "123 -> x", - "456 -> y", - "x AND y -> d", - "x OR y -> e", - "x LSHIFT 2 -> f", - "y RSHIFT 2 -> g", - "NOT x -> h", - "NOT y -> i", - ] - ) == { - "d": 72, - "e": 507, - "f": 492, - "g": 114, - "h": 65412, - "i": 65079, - "x": 123, - "y": 456, - } - - -def test_inner_1_bis(): - with patch("y_2015.day7.WIRING", defaultdict(Container)): - assert inner_1( - [ - "123 -> x", - "456 -> y", - "x AND y -> d", - "x OR y -> e", - "x LSHIFT 2 -> f", - "y RSHIFT 2 -> g", - "NOT x -> h", - "NOT y -> i", - "x AND 456 -> j", - ] - ) == { - "d": 72, - "e": 507, - "f": 492, - "g": 114, - "h": 65412, - "i": 65079, - "x": 123, - "y": 456, - "j": 72, - } - - -def test_inner_1_not_existing(): - with patch("y_2015.day7.WIRING", defaultdict(Container)): - assert inner_1( - [ - "x -> y", - "123 -> x", - ] - ) == {"x": 123, "y": 123} - - -def test_calculate_1(): - with patch("y_2015.day7.WIRING", defaultdict(Container)): - assert ( - calculate_1( - [ - "123 -> x", - "456 -> y", - "x AND y -> d", - "x OR y -> e", - "x LSHIFT 2 -> f", - "y RSHIFT 2 -> g", - "NOT x -> h", - "NOT y -> i", - "1 -> a", - ] - ) - == 1 - ) - - -def test_calculate_2(): - with patch("y_2015.day7.WIRING", defaultdict(Container)): - assert ( - calculate_2( - [ - "123 -> x", - "456 -> y", - "x AND y -> d", - "x OR y -> e", - "x LSHIFT 2 -> f", - "y RSHIFT 2 -> g", - "NOT x -> h", - "NOT y -> i", - "1 -> a", - "0 -> b", - ] - ) - == 1 - ) diff --git a/y_2015/day1.py b/y_2015/day1.py index a33fb76..797a5fe 100644 --- a/y_2015/day1.py +++ b/y_2015/day1.py @@ -1,14 +1,22 @@ +from common.aoc import AoCDay + OPERATIONS = {"(": 1, ")": -1} -def calculate_1(i: str) -> int: - return sum(OPERATIONS[x] for x in i) +class Day(AoCDay): + def __init__(self, test=0): + super().__init__(__name__, test) + + def _preprocess_input(self): + self.__input_data = self._input_data[0][0] + def _calculate_1(self): + return sum(OPERATIONS[x] for x in self.__input_data) -def calculate_2(i: str) -> int: - result = 0 - for j, x in enumerate(i, start=1): - result += OPERATIONS[x] - if result == -1: - return j - return -1 + def _calculate_2(self): + result = 0 + for j, x in enumerate(self.__input_data, start=1): + result += OPERATIONS[x] + if result == -1: + return j + return -1