Compare commits
No commits in common. "a59c3c697c3baead456aba7c58d6703dfcc9d1ab" and "a6c027a1af91fc02c5157817250619ba017aa1cc" have entirely different histories.
a59c3c697c
...
a6c027a1af
|
@ -1,40 +0,0 @@
|
|||
import sys
|
||||
|
||||
def strset(s,i,c):
|
||||
return s[:i]+c+s[i+1:]
|
||||
|
||||
def next(pat, nums, patc=''): # pat must terminiate with '.'
|
||||
#print(f"next {pat=} {nums=} ({patc=}), {len(pat)+len(patc)}")
|
||||
if len(nums) == 0:
|
||||
if pat.find('#')<0:
|
||||
#print("valid", patc)
|
||||
return 1
|
||||
return 0
|
||||
# find all ways to build first block
|
||||
l = nums[0]
|
||||
block = '#'*l
|
||||
rest = sum(nums[1:])
|
||||
n = 0
|
||||
m = len(pat)-l-rest
|
||||
for i in range(m):
|
||||
# if can build block:
|
||||
z = pat[i:i+l].replace('?','#')
|
||||
#print(f"{i=}/{m=} {z=}, {pat[i+l]}")
|
||||
if z == block:
|
||||
##print("march, and ", pat[i+l])
|
||||
if pat[:i].find('#')<0 and not pat[i+l] == '#': # '.' or '?'
|
||||
#pat = strset(pat, i+l, '.')
|
||||
# recurse
|
||||
r = next(pat[i+l+1:], nums[1:], patc+pat[:i].replace('?','.')+z+'.')
|
||||
if r > 0:
|
||||
n += r
|
||||
return n
|
||||
|
||||
S= 0
|
||||
for i,l in enumerate(sys.stdin.read().splitlines()):
|
||||
pattern, snums = l.split(' ')
|
||||
nums = list(map(int,snums.split(',')))
|
||||
n = next(pattern+'.', nums)
|
||||
print(i,l,n)
|
||||
S += n
|
||||
print(S)
|
|
@ -1,37 +0,0 @@
|
|||
import sys
|
||||
|
||||
def valid(pat:str, nums:list[int]):
|
||||
pat += '.'
|
||||
c = 0
|
||||
for i,p in enumerate(pat):
|
||||
if p == '#':
|
||||
c += 1
|
||||
else:
|
||||
if c>0:
|
||||
if c == nums[0]:
|
||||
c = 0
|
||||
nums = nums[1:]
|
||||
if len(nums) == 0:
|
||||
return 1 if pat[i:].find('#')<0 else 0
|
||||
else:
|
||||
return 0
|
||||
return 0
|
||||
|
||||
def next(pattern, nums):
|
||||
i = pattern.find('?')
|
||||
if i < 0:
|
||||
v = valid(pattern, nums)
|
||||
if v == 1:
|
||||
print("valid", pattern)
|
||||
return v
|
||||
else:
|
||||
return next(pattern[:i]+'.'+pattern[i+1:], nums)+next(pattern[:i]+'#'+pattern[i+1:], nums)
|
||||
|
||||
S= 0
|
||||
for i,l in enumerate(sys.stdin.read().splitlines()):
|
||||
pattern, snums = l.split(' ')
|
||||
nums = list(map(int,snums.split(',')))
|
||||
n = next(pattern, nums)
|
||||
print(i,l,n)
|
||||
S += n
|
||||
print(S)
|
|
@ -1,31 +0,0 @@
|
|||
import sys
|
||||
|
||||
def valid(pattern, nums):
|
||||
l = []
|
||||
c = ''
|
||||
for p in pattern:
|
||||
if p == '#':
|
||||
c+=p
|
||||
else:
|
||||
if len(c):
|
||||
l.append(c)
|
||||
c = ''
|
||||
if len(c):
|
||||
l.append(c)
|
||||
c = ''
|
||||
l = [len(m) for m in l]
|
||||
return 1 if l == nums else 0
|
||||
|
||||
def next(pattern, nums):
|
||||
i = pattern.find('?')
|
||||
if i < 0:
|
||||
return valid(pattern, nums)
|
||||
else:
|
||||
return next(pattern[:i]+'.'+pattern[i+1:], nums)+next(pattern[:i]+'#'+pattern[i+1:], nums)
|
||||
|
||||
S= 0
|
||||
for l in sys.stdin.read().splitlines():
|
||||
pattern, snums = l.split(' ')
|
||||
nums = list(map(int,snums.split(',')))
|
||||
S += next(pattern, nums)
|
||||
print(S)
|
12
d15/part1.py
12
d15/part1.py
|
@ -1,12 +0,0 @@
|
|||
import sys
|
||||
|
||||
S=0
|
||||
for l in sys.stdin.read().split(','):
|
||||
c = 0
|
||||
for w in l:
|
||||
c += ord(w)
|
||||
c *= 17
|
||||
c = c % 256
|
||||
print(l, c)
|
||||
S += c
|
||||
print(S)
|
26
d15/part2.py
26
d15/part2.py
|
@ -1,26 +0,0 @@
|
|||
import sys
|
||||
|
||||
def hash(l):
|
||||
c = 0
|
||||
for w in l:
|
||||
c += ord(w)
|
||||
c *= 17
|
||||
c = c % 256
|
||||
return c
|
||||
|
||||
B = [ dict() for _ in range(256) ]
|
||||
|
||||
for l in sys.stdin.read().split(','):
|
||||
e = l.find('=')
|
||||
d = l.find('-')
|
||||
i = max(e,d)
|
||||
lbl = l[:i]
|
||||
box = hash(lbl)
|
||||
if e > 0: # set
|
||||
v = int(l[i+1:])
|
||||
B[box][lbl] = v
|
||||
elif d > 0: # remove
|
||||
if lbl in B[box]:
|
||||
del B[box][lbl]
|
||||
|
||||
print(sum((i+1)*(j+1)*v for i, b in enumerate(B) for j,(k,v) in enumerate(b.items())))
|
Loading…
Reference in New Issue