Skip to content

Commit

Permalink
Merge pull request #222 from Stegallo/2023
Browse files Browse the repository at this point in the history
2023
  • Loading branch information
Stegallo authored Dec 9, 2023
2 parents ce4360b + 0d87adb commit 481cc1b
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 2 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]</br>[][201501] | | | | | | | [puzzle][202201p]</br>[][202201] | [puzzle][202301p]</br>[][202301] |
| 02 | [puzzle][201502p]</br>[][201502] | | | | | | | | |
| 03 | [puzzle][201503p]</br>[][201503] | | | | | | | | |
| 02 | [puzzle][201502p]</br>[][201502] | | | | | | | | [puzzle][202302p]</br>[][202302] |
| 03 | [puzzle][201503p]</br>[][201503] | | | | | | | | [puzzle][202303p]</br>[][202303] |
| 04 | [puzzle][201504p]</br>[][201504] | | | | | | | | |
| 05 | [puzzle][201505p]</br>[][201505] | | | | | | | | |
| 06 | [puzzle][201506p]</br>[][201506] | | | | | | | | |
Expand Down Expand Up @@ -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

Expand Down
73 changes: 73 additions & 0 deletions y_2023/day3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
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]]:
return [
(self.row + y - 1, self.start + x - 1)
for x in range(self.length + 2)
for y in range(3)
]


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):
def __init__(self, test=0):
super().__init__(__name__, test)

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:
result = 0

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:
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)
return sum((v[0] * v[1]) for v in star_adjacents.values() if len(v) == 2)

0 comments on commit 481cc1b

Please sign in to comment.