GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — AdventOfCode/languages/py/2023/12_HotSprings.py at main · wolflu05/AdventOfCode

Expand file tree

/

12_HotSprings.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
64 lines (45 loc) · 2 KB
/

12_HotSprings.py

Copy path

File metadata and controls

64 lines (45 loc) · 2 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import functools
from lib import *
inp = get_input()
def count_combinations(string, counts):
string += "." # append a . to the string, so that a last group will always be closed automatically
@functools.cache
def _count_combinations(i, curr_group, curr_group_len):
def new_group():
if curr_group_len == 0: # no group ended -> dont increment group, just continue with the next position
return _count_combinations(i+1, curr_group, 0)
# if there would be more groups then in counts defined, this is no valid solution
if curr_group >= len(counts):
return 0
# if the length of the current group doesn't match, starting a new one would produce no valid combination
if curr_group_len != counts[curr_group]:
return 0
# a group ended now -> start a new one
return _count_combinations(i+1, curr_group+1, 0)
if i >= len(string):
# all groups and their length are checked not, but there can be still less groups than required for counts
if curr_group != len(counts):
return 0
return 1
if string[i] == ".":
return new_group() # try to start a new group
if string[i] == "#":
return _count_combinations(i+1, curr_group, curr_group_len+1) # extend current group
if string[i] == "?":
return (
# replace ? with # -> continue group
_count_combinations(i+1, curr_group, curr_group_len+1) +
# replace ? with . -> end group here and start a new one
new_group()
)
return _count_combinations(0,0,0)
def solve(fold_count):
res = 0
for l in inp:
springs, counts = l.split()
springs = "?".join([springs] * fold_count)
counts=[int(c) for c in counts.split(",")] * fold_count
res += count_combinations(springs, counts)
print(res)
solve(1)
solve(5)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.