From b20971a5d094a572e0b0e94fe5a6636a2eb37798 Mon Sep 17 00:00:00 2001 From: Stegallo Date: Fri, 8 Dec 2023 20:33:10 -0800 Subject: [PATCH 1/5] day3 --- README.md | 4 +- y_2023/day3.py | 118 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 120 insertions(+), 2 deletions(-) create mode 100644 y_2023/day3.py diff --git a/README.md b/README.md index 7737567..426f92a 100644 --- a/README.md +++ b/README.md @@ -11,8 +11,8 @@ for solutions in rust, refer to [rust repo][rustrepo] | | 2015 | 2016 | 2017 | 2018 | 2019 | 2020 | 2021 | 2022 | 2023 | | - | - | - | - | - | - | - | - | - | - | | 01 | [puzzle][201501p]
[✓][201501] | | | | | | | [puzzle][202201p]
[✓][202201] | [puzzle][202301p]
[✓][202301] | -| 02 | [puzzle][201502p]
[✓][201502] | | | | | | | | | -| 03 | [puzzle][201503p]
[✓][201503] | | | | | | | | | +| 02 | [puzzle][201502p]
[✓][201502] | | | | | | | | [puzzle][202302p]
[✓][202302] | +| 03 | [puzzle][201503p]
[✓][201503] | | | | | | | | [puzzle][202303p]
[✓][202303] | | 04 | [puzzle][201504p]
[✓][201504] | | | | | | | | | | 05 | [puzzle][201505p]
[✓][201505] | | | | | | | | | | 06 | [puzzle][201506p]
[✓][201506] | | | | | | | | | diff --git a/y_2023/day3.py b/y_2023/day3.py new file mode 100644 index 0000000..3ce4fa0 --- /dev/null +++ b/y_2023/day3.py @@ -0,0 +1,118 @@ +from common.aoc import AoCDay +from collections import defaultdict + + +class Day(AoCDay): + def __init__(self, test=0): + super().__init__(__name__, test) + + def _preprocess_input(self): + self.__input_data = self._input_data[0] + self.__simbols = {} + for c, x in enumerate(self.__input_data): + for d, y in enumerate(x): + if y != "." and not y.isnumeric(): + self.__simbols[(c, d)] = y + + def _calculate_1(self) -> int: # 553825 + result = 0 + print(self.__simbols) + for c, x in enumerate(self.__input_data): + splits = x.split(".") + numbers = [] + for i in splits: + if i.isnumeric(): + numbers.append(i) + elif i[:-1].isnumeric(): + numbers.append(i[:-1]) + elif i[1:].isnumeric(): + numbers.append(i[1:]) + elif len(i) > 1: + # print(i) + j = i.split("*") + numbers.extend(j) + # 1/0 + # print(f'{numbers=}') + # continue + num_ind = 0 + start = 0 + end = 0 + while x: + # print(x, x[0], numbers, num_ind, start, end) + if num_ind >= len(numbers): + break + if x[0] == "." or not x[0].isnumeric(): + x = x[1:] + start += 1 + if x.startswith(str(numbers[num_ind])): + end = start + len(str(numbers[num_ind])) - 1 + # print(numbers[num_ind], start, end) + for k in range(c - 1, c + 2): + for m in range(start - 1, end + 2): + # print((k,l), simbols.get((k,l))) + if self.__simbols.get((k, m)): + # print(int(numbers[num_ind])) + result += int(numbers[num_ind]) + start = end + 1 + x = x[len(str(numbers[num_ind])) :] + num_ind += 1 + + # break + + return result + + def _calculate_2(self) -> int: # 93994191 + result = 0 + new_simbols = defaultdict(list) + + for c, x in enumerate(self.__input_data): + splits = x.split(".") + numbers = [] + for i in splits: + if i.isnumeric(): + numbers.append(i) + elif i[:-1].isnumeric(): + numbers.append(i[:-1]) + elif i[1:].isnumeric(): + numbers.append(i[1:]) + elif len(i) > 1: + # print(i) + j = i.split("*") + numbers.extend(j) + # 1/0 + # print(f'{numbers=}') + # continue + num_ind = 0 + start = 0 + end = 0 + while x: + # print(x, x[0], numbers, num_ind, start, end) + if num_ind >= len(numbers): + break + if x[0] == "." or not x[0].isnumeric(): + x = x[1:] + start += 1 + if x.startswith(str(numbers[num_ind])): + end = start + len(str(numbers[num_ind])) - 1 + # print(numbers[num_ind], start, end) + for k in range(c - 1, c + 2): + for m in range(start - 1, end + 2): + # print((k,l), simbols.get((k,l))) + if self.__simbols.get((k, m)): + # print(int(numbers[num_ind])) + # result+=int(numbers[num_ind]) + new_simbols[k, m].append(int(numbers[num_ind])) + start = end + 1 + x = x[len(str(numbers[num_ind])) :] + num_ind += 1 + + # break + # print(new_simbols) + for i in self.__simbols: + if self.__simbols[i][0] == "*" and len(new_simbols[i]) == 2: + # print(simbols[i]) + # print(new_simbols[i]) + # print( new_simbols[i][0]*new_simbols[i][1] ) + result += new_simbols[i][0] * new_simbols[i][1] + + return result From 4884d45990497e351018242b5ed0f12c72d84ad4 Mon Sep 17 00:00:00 2001 From: Stegallo Date: Sat, 9 Dec 2023 11:22:52 -0800 Subject: [PATCH 2/5] day3 --- README.md | 2 + y_2023/day3.py | 147 +++++++++++++++++-------------------------------- 2 files changed, 54 insertions(+), 95 deletions(-) diff --git a/README.md b/README.md index 426f92a..ebc4440 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,8 @@ for solutions in rust, refer to [rust repo][rustrepo] [202301p]: https://adventofcode.com/2023/day/1 [202302]: https://github.com/Stegallo/adventofcode/blob/master/y_2023/day2.py [202302p]: https://adventofcode.com/2023/day/2 +[202303]: https://github.com/Stegallo/adventofcode/blob/master/y_2023/day3.py +[202303p]: https://adventofcode.com/2023/day/3 [rustrepo]: https://github.com/Stegallo/adventofcodeinrust diff --git a/y_2023/day3.py b/y_2023/day3.py index 3ce4fa0..b147b05 100644 --- a/y_2023/day3.py +++ b/y_2023/day3.py @@ -1,5 +1,43 @@ from common.aoc import AoCDay +from typing import List, Tuple from collections import defaultdict +from pydantic.dataclasses import dataclass + + +@dataclass +class Number: + value: int + row: int + start: int + length: int + + @property + def border(self) -> List[Tuple[int, int]]: + result = [] + for y in range(3): + for x in range(self.length + 2): + result.append((self.row + y - 1, self.start + x - 1)) + return result + + +def extract_numbers(input, row) -> List[Number]: + start = 0 + length = 0 + result = [] + for c, i in enumerate(input): + if i.isnumeric(): + length += 1 + else: + if length > 0: + result.append( + Number(int(input[start : start + length]), row, start, length), + ) + start = c + 1 + length = 0 + if length > 0: + result.append(Number(int(input[start : start + length]), row, start, length)) + + return result class Day(AoCDay): @@ -9,110 +47,29 @@ def __init__(self, test=0): def _preprocess_input(self): self.__input_data = self._input_data[0] self.__simbols = {} + self.__numbers = [] for c, x in enumerate(self.__input_data): for d, y in enumerate(x): if y != "." and not y.isnumeric(): self.__simbols[(c, d)] = y + self.__numbers.extend(extract_numbers(x, c)) - def _calculate_1(self) -> int: # 553825 + def _calculate_1(self) -> int: result = 0 - print(self.__simbols) - for c, x in enumerate(self.__input_data): - splits = x.split(".") - numbers = [] - for i in splits: - if i.isnumeric(): - numbers.append(i) - elif i[:-1].isnumeric(): - numbers.append(i[:-1]) - elif i[1:].isnumeric(): - numbers.append(i[1:]) - elif len(i) > 1: - # print(i) - j = i.split("*") - numbers.extend(j) - # 1/0 - # print(f'{numbers=}') - # continue - num_ind = 0 - start = 0 - end = 0 - while x: - # print(x, x[0], numbers, num_ind, start, end) - if num_ind >= len(numbers): - break - if x[0] == "." or not x[0].isnumeric(): - x = x[1:] - start += 1 - if x.startswith(str(numbers[num_ind])): - end = start + len(str(numbers[num_ind])) - 1 - # print(numbers[num_ind], start, end) - for k in range(c - 1, c + 2): - for m in range(start - 1, end + 2): - # print((k,l), simbols.get((k,l))) - if self.__simbols.get((k, m)): - # print(int(numbers[num_ind])) - result += int(numbers[num_ind]) - start = end + 1 - x = x[len(str(numbers[num_ind])) :] - num_ind += 1 - - # break + for i in self.__numbers: + for j in i.border: + if j in self.__simbols: + result += i.value return result - def _calculate_2(self) -> int: # 93994191 + def _calculate_2(self) -> int: result = 0 - new_simbols = defaultdict(list) - - for c, x in enumerate(self.__input_data): - splits = x.split(".") - numbers = [] - for i in splits: - if i.isnumeric(): - numbers.append(i) - elif i[:-1].isnumeric(): - numbers.append(i[:-1]) - elif i[1:].isnumeric(): - numbers.append(i[1:]) - elif len(i) > 1: - # print(i) - j = i.split("*") - numbers.extend(j) - # 1/0 - # print(f'{numbers=}') - # continue - num_ind = 0 - start = 0 - end = 0 - while x: - # print(x, x[0], numbers, num_ind, start, end) - if num_ind >= len(numbers): - break - if x[0] == "." or not x[0].isnumeric(): - x = x[1:] - start += 1 - if x.startswith(str(numbers[num_ind])): - end = start + len(str(numbers[num_ind])) - 1 - # print(numbers[num_ind], start, end) - for k in range(c - 1, c + 2): - for m in range(start - 1, end + 2): - # print((k,l), simbols.get((k,l))) - if self.__simbols.get((k, m)): - # print(int(numbers[num_ind])) - # result+=int(numbers[num_ind]) - new_simbols[k, m].append(int(numbers[num_ind])) - start = end + 1 - x = x[len(str(numbers[num_ind])) :] - num_ind += 1 - - # break - # print(new_simbols) - for i in self.__simbols: - if self.__simbols[i][0] == "*" and len(new_simbols[i]) == 2: - # print(simbols[i]) - # print(new_simbols[i]) - # print( new_simbols[i][0]*new_simbols[i][1] ) - result += new_simbols[i][0] * new_simbols[i][1] + star_adjacents = defaultdict(list) + for i in self.__numbers: + for j in i.border: + if j in self.__simbols and self.__simbols[j] == "*": + star_adjacents[j].append(i.value) + result = sum((v[0] * v[1]) for v in star_adjacents.values() if len(v) == 2) return result From 29d8e06d455259e209a5f42af66920950f0c99f4 Mon Sep 17 00:00:00 2001 From: Sourcery AI <> Date: Sat, 9 Dec 2023 19:23:22 +0000 Subject: [PATCH 3/5] 'Refactored by Sourcery' --- y_2023/day3.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/y_2023/day3.py b/y_2023/day3.py index b147b05..642dd31 100644 --- a/y_2023/day3.py +++ b/y_2023/day3.py @@ -15,8 +15,10 @@ class Number: def border(self) -> List[Tuple[int, int]]: result = [] for y in range(3): - for x in range(self.length + 2): - result.append((self.row + y - 1, self.start + x - 1)) + result.extend( + (self.row + y - 1, self.start + x - 1) + for x in range(self.length + 2) + ) return result @@ -71,5 +73,4 @@ def _calculate_2(self) -> int: for j in i.border: if j in self.__simbols and self.__simbols[j] == "*": star_adjacents[j].append(i.value) - result = sum((v[0] * v[1]) for v in star_adjacents.values() if len(v) == 2) - return result + return sum((v[0] * v[1]) for v in star_adjacents.values() if len(v) == 2) From 467bfb995bd927b6927fda40579aeb217e461d05 Mon Sep 17 00:00:00 2001 From: Stegallo Date: Sat, 9 Dec 2023 11:28:40 -0800 Subject: [PATCH 4/5] day3 --- y_2023/day3.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/y_2023/day3.py b/y_2023/day3.py index 642dd31..e88501a 100644 --- a/y_2023/day3.py +++ b/y_2023/day3.py @@ -13,12 +13,11 @@ class Number: @property def border(self) -> List[Tuple[int, int]]: - result = [] - for y in range(3): - result.extend( - (self.row + y - 1, self.start + x - 1) - for x in range(self.length + 2) - ) + result = [ + (self.row + y - 1, self.start + x - 1) + for x in range(self.length + 2) + for y in range(3) + ] return result @@ -66,7 +65,6 @@ def _calculate_1(self) -> int: return result def _calculate_2(self) -> int: - result = 0 star_adjacents = defaultdict(list) for i in self.__numbers: From aaa043a02e25d231e7ca59d10c37783e4d91d6af Mon Sep 17 00:00:00 2001 From: Sourcery AI <> Date: Sat, 9 Dec 2023 19:28:53 +0000 Subject: [PATCH 5/5] 'Refactored by Sourcery' --- y_2023/day3.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/y_2023/day3.py b/y_2023/day3.py index e88501a..19552b2 100644 --- a/y_2023/day3.py +++ b/y_2023/day3.py @@ -13,12 +13,11 @@ class Number: @property def border(self) -> List[Tuple[int, int]]: - result = [ + return [ (self.row + y - 1, self.start + x - 1) for x in range(self.length + 2) for y in range(3) ] - return result def extract_numbers(input, row) -> List[Number]: