Compare commits

..

2 Commits

Author SHA1 Message Date
setop a59c3c697c day 15, trivial 2023-12-15 17:24:51 +01:00
setop 6be1a222b1 day 12, part 1 faster, but not fast enough for part 2 2023-12-13 14:23:41 +01:00
5 changed files with 146 additions and 0 deletions

40
d12/part1.block.py Normal file
View File

@ -0,0 +1,40 @@
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)

37
d12/part1.len.py Normal file
View File

@ -0,0 +1,37 @@
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)

31
d12/part1.list.py Normal file
View File

@ -0,0 +1,31 @@
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 Normal file
View File

@ -0,0 +1,12 @@
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 Normal file
View File

@ -0,0 +1,26 @@
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())))