Skip to content

Commit

Permalink
2023 day 12 part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
lancelote committed Dec 27, 2023
1 parent 04468b1 commit 93d7672
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
- 2020 - ★★★★★ ★★★★★ ★★★★★ ☆
- 2021 - ★★★★★ ★★★★★ ★★★★★ ★★★
- 2022 - ★★★★★ ★★★★★ ★★★★★ ★☆
- 2023 - ★★★★☆ ★☆★★☆ ★
- 2023 - ★★★★☆ ★☆★★☆ ★

## How to use

Expand Down
58 changes: 58 additions & 0 deletions src/year2023/day12a.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
"""2023 - Day 12 Part 1: Hot Springs"""
from dataclasses import dataclass
from typing import Self


@dataclass
class Row:
springs: str
segments: list[int]

@property
def arrangements(self) -> int:
n = len(self.segments)

def dfs(i: int, j: int, filled: int) -> int:
if i == len(self.springs):
if (j == n and filled == 0) or (
j == n - 1 and filled == self.segments[j]
):
return 1
else:
return 0

count = 0

if self.springs[i] in {".", "?"}:
if filled == 0:
count += dfs(i + 1, j, filled)
elif filled == self.segments[j]:
count += dfs(i + 1, j + 1, 0)

if self.springs[i] in {"#", "?"}:
if j < n and filled < self.segments[j]:
count += dfs(i + 1, j, filled + 1)

return count

return dfs(0, 0, 0)

@classmethod
def from_line(cls, line: str) -> Self:
springs, segments_part = line.split()
segments = [int(x) for x in segments_part.split(",")]
return cls(springs, segments)


@dataclass
class Field:
rows: list[Row]

@classmethod
def from_task(cls, task: str) -> Self:
return cls([Row.from_line(line) for line in task.splitlines()])


def solve(task: str) -> int:
field = Field.from_task(task)
return sum(r.arrangements for r in field.rows)
36 changes: 36 additions & 0 deletions tests/src/year2023/test_day12a.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"""2023 - Day 12 Part 1: Hot Springs"""
from textwrap import dedent

import pytest

from src.year2023.day12a import Row
from src.year2023.day12a import solve


@pytest.mark.parametrize(
"line,expected",
(
("???.### 1,1,3", 1),
(".??..??...?##. 1,1,3", 4),
("?#?#?#?#?#?#?#? 1,3,1,6", 1),
("????.#...#... 4,1,1", 1),
("????.######..#####. 1,6,5", 4),
("?###???????? 3,2,1", 10),
),
)
def test_row(line, expected):
assert Row.from_line(line).arrangements == expected


def test_solve():
task = dedent(
"""
???.### 1,1,3
.??..??...?##. 1,1,3
?#?#?#?#?#?#?#? 1,3,1,6
????.#...#... 4,1,1
????.######..#####. 1,6,5
?###???????? 3,2,1
"""
).strip()
assert solve(task) == 21

0 comments on commit 93d7672

Please sign in to comment.