day 1 part 2 bug, day12 part 1

main
Tom Weber 1 year ago
parent 000ad0f9ec
commit fb6b307e2e

@ -1,6 +1,5 @@
lines = open('inputs/day1.txt').readlines()
digit_names = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]
replaced_names = ['o1e', 't2o', 't3e', 'f4r', 'f5e', 's6x', 's7n', 'e8h', 'n9e']
lines = [x.strip("\n") for x in open('inputs/day1.txt').readlines()]
digit_names = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]
def digits(lines: list[str]) -> list[int]:
dgts = []
@ -22,20 +21,23 @@ def transform_digits(lines: list[str]) -> list[str]:
for line in lines:
indices = {}
for idx, c in enumerate(digit_names):
c_loc = line.find(c)
if c_loc != -1:
indices[str(c_loc)] = str(idx+1)
c_loc = [i for i in range(len(line)) if line.startswith(c, i)]
while c_loc:
loc = c_loc.pop(0)
indices[loc] = str(idx+1)
if len(indices) == 0:
newlines.append(line)
else:
first_index = sorted(indices)[0]
first_number = replaced_names[int(indices[first_index])-1]
first_digit_name = digit_names[int(indices[first_index])-1]
first_number = int(indices[first_index])
second_index = sorted(indices)[-1]
second_number = replaced_names[int(indices[second_index])-1]
second_digit_name = digit_names[int(indices[second_index])-1]
newlines.append(line.replace(first_digit_name, first_number).replace(second_digit_name, second_number))
second_number = int(indices[second_index])
newline = line[:first_index+1] + str(first_number) + line[first_index+1:]
newline = newline[:second_index+2] + str(second_number) + newline[second_index+2:]
newlines.append(newline)
return newlines
print(f'Part 1 Solution = {sum(digits(lines))}')
print(f'Part 2 Solution = {sum(digits(transform_digits(lines)))}')
print(f'Part 2 Solution = {sum(digits(transform_digits(lines)))}')

@ -0,0 +1,62 @@
import itertools
from functools import cache
lines = [line.strip("\n") for line in open("inputs/day12.txt").readlines()]
def encodings(lines: list[str]) -> tuple:
damaged = []
alt = []
for line in lines:
line = line.split(" ")
damaged.append(line[0])
alt.append([int(x) for x in line[1].split(",")])
return damaged, alt
def qm_idx(string: str) -> list[int]:
idx = [i for i, s in enumerate(string) if s == "?"]
return idx
def matches(string: str, enc: list[int]) -> bool:
groups = []
counter = 0
cont = False
for i, s in enumerate(string):
if s == "#":
counter += 1
cont = True
if i == len(string) - 1:
groups.append(counter)
else:
if cont:
cont = False
groups.append(counter)
counter = 0
return groups == enc
def combinations(string: str) -> list[str]:
ids = qm_idx(string)
combs = []
for comb in itertools.product("#.", repeat=len(ids)):
newstring = string
for i, c in zip(ids, comb):
newstring = newstring[:i] + c + newstring[i+1:]
combs.append(newstring)
return combs
@cache
def count_matches(string: str, alt: list[int]) -> int:
combs = combinations(string)
counter = 0
for comb in combs:
if matches(comb, alt):
counter += 1
return counter
def main():
dmgd, alts = encodings(lines)
counts = 0
for dmg, alt in zip(dmgd, alts):
counts += count_matches(dmg, alt)
print(f"Solution Part 1: {counts}")
# part 2 missing
main()

File diff suppressed because it is too large Load Diff

@ -0,0 +1,6 @@
???.### 1,1,3
.??..??...?##. 1,1,3
?#?#?#?#?#?#?#? 1,3,1,6
????.#...#... 4,1,1
????.######..#####. 1,6,5
?###???????? 3,2,1
Loading…
Cancel
Save