day 12 part 1 completed
This commit is contained in:
parent
f234fc62bf
commit
a9d7c6a101
@ -1,7 +1,7 @@
|
|||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from typing import NamedTuple, NewType
|
from typing import NamedTuple, NewType
|
||||||
import re
|
import re
|
||||||
|
import logging
|
||||||
|
|
||||||
def im_replace(line: str, mask: str) -> str:
|
def im_replace(line: str, mask: str) -> str:
|
||||||
idx = 0
|
idx = 0
|
||||||
@ -21,31 +21,27 @@ class SpringLine(NamedTuple):
|
|||||||
|
|
||||||
def arrangments(self) -> list[str]:
|
def arrangments(self) -> list[str]:
|
||||||
count_im = self.line.count("?")
|
count_im = self.line.count("?")
|
||||||
print(count_im)
|
logging.debug(count_im)
|
||||||
possibles = []
|
possibles = []
|
||||||
for i in range(pow(2, count_im)):
|
for i in range(pow(2, count_im)):
|
||||||
|
debug_str = ""
|
||||||
rep_str = f"{i:b}".rjust(count_im, "0").replace("0", ".").replace("1", "#")
|
rep_str = f"{i:b}".rjust(count_im, "0").replace("0", ".").replace("1", "#")
|
||||||
print(rep_str, end=" / ")
|
debug_str += rep_str + " / "
|
||||||
tentative = im_replace(self.line, rep_str)
|
tentative = im_replace(self.line, rep_str)
|
||||||
possible = SpringLine(tentative, self.groups)
|
possible = SpringLine(tentative, self.groups)
|
||||||
print(possible, end=": ")
|
debug_str += str(possible)+": "
|
||||||
if possible.is_valid():
|
if possible.is_valid():
|
||||||
possibles.append(im_replace(self.line, rep_str))
|
possibles.append(im_replace(self.line, rep_str))
|
||||||
print("valid")
|
debug_str += "valid"
|
||||||
else:
|
else:
|
||||||
print("invalid")
|
debug_str += "invalid"
|
||||||
|
logging.debug(debug_str)
|
||||||
return possibles
|
return possibles
|
||||||
|
|
||||||
def is_valid(self) -> bool:
|
def is_valid(self) -> bool:
|
||||||
# TODO: correct this (re.match with generated pattern based en groups)
|
pattern = "^\.*" + "\.+".join(["#" * i for i in self.groups]) + "\.*$"
|
||||||
line_copy = self.line
|
# logging.debug(pattern)
|
||||||
|
return re.match(pattern, self.line)
|
||||||
for group in self.groups:
|
|
||||||
idx = line_copy.find("#" * group)
|
|
||||||
if idx == -1:
|
|
||||||
return False
|
|
||||||
line_copy = line_copy[idx + group :]
|
|
||||||
return True
|
|
||||||
|
|
||||||
|
|
||||||
Spring = NewType("Spring", list[SpringLine])
|
Spring = NewType("Spring", list[SpringLine])
|
||||||
@ -56,7 +52,7 @@ def parse(input_file: str) -> Spring:
|
|||||||
with open(input_file, "r", encoding="utf-8") as inputfd:
|
with open(input_file, "r", encoding="utf-8") as inputfd:
|
||||||
while line := inputfd.readline():
|
while line := inputfd.readline():
|
||||||
if match := re.match("([?#.]+)\s([0-9,]+)", line):
|
if match := re.match("([?#.]+)\s([0-9,]+)", line):
|
||||||
# print(match.group(0))
|
# logging.debug(match.group(0))
|
||||||
spring_lines.append(
|
spring_lines.append(
|
||||||
SpringLine(
|
SpringLine(
|
||||||
match.group(1),
|
match.group(1),
|
||||||
|
@ -5,9 +5,9 @@ from common import *
|
|||||||
|
|
||||||
def part1(input_file: str) -> int:
|
def part1(input_file: str) -> int:
|
||||||
spring = parse(input_file)
|
spring = parse(input_file)
|
||||||
print(spring)
|
#interm = list(map(lambda s: s.arrangments(), spring))
|
||||||
interm = list(map(lambda s: s.arrangments(), spring))
|
#interm = list(map(len, interm))
|
||||||
print(interm)
|
return sum(list(map(len, map(lambda s: s.arrangments(), spring))))
|
||||||
interm = list(map(len, interm))
|
|
||||||
print(interm)
|
if __name__=="__main__":
|
||||||
return sum(interm)
|
print(part1("input.txt"))
|
@ -1,3 +1,7 @@
|
|||||||
#!/usr/bin/env python3.11
|
#!/usr/bin/env python3.11
|
||||||
|
|
||||||
from common import *
|
from common import *
|
||||||
|
|
||||||
|
|
||||||
|
def part2(input_file:str) -> int:
|
||||||
|
return 0
|
@ -23,7 +23,7 @@ class DayXTests(TestCase):
|
|||||||
self.assertEqual(part1("example_input.txt"), 21)
|
self.assertEqual(part1("example_input.txt"), 21)
|
||||||
|
|
||||||
def test_part2(self):
|
def test_part2(self):
|
||||||
pass
|
self.assertEqual(part2("example_input.txt"), 525152)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
Loading…
x
Reference in New Issue
Block a user