Added template + day12 in progress
This commit is contained in:
parent
ed55a50ec7
commit
f234fc62bf
66
day12/common.py
Normal file
66
day12/common.py
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
from dataclasses import dataclass
|
||||||
|
from typing import NamedTuple, NewType
|
||||||
|
import re
|
||||||
|
|
||||||
|
|
||||||
|
def im_replace(line: str, mask: str) -> str:
|
||||||
|
idx = 0
|
||||||
|
new_line = ""
|
||||||
|
for c in line:
|
||||||
|
if c != "?":
|
||||||
|
new_line += c
|
||||||
|
else:
|
||||||
|
new_line += mask[idx]
|
||||||
|
idx += 1
|
||||||
|
return new_line
|
||||||
|
|
||||||
|
|
||||||
|
class SpringLine(NamedTuple):
|
||||||
|
line: str
|
||||||
|
groups: tuple[int, ...]
|
||||||
|
|
||||||
|
def arrangments(self) -> list[str]:
|
||||||
|
count_im = self.line.count("?")
|
||||||
|
print(count_im)
|
||||||
|
possibles = []
|
||||||
|
for i in range(pow(2, count_im)):
|
||||||
|
rep_str = f"{i:b}".rjust(count_im, "0").replace("0", ".").replace("1", "#")
|
||||||
|
print(rep_str, end=" / ")
|
||||||
|
tentative = im_replace(self.line, rep_str)
|
||||||
|
possible = SpringLine(tentative, self.groups)
|
||||||
|
print(possible, end=": ")
|
||||||
|
if possible.is_valid():
|
||||||
|
possibles.append(im_replace(self.line, rep_str))
|
||||||
|
print("valid")
|
||||||
|
else:
|
||||||
|
print("invalid")
|
||||||
|
return possibles
|
||||||
|
|
||||||
|
def is_valid(self) -> bool:
|
||||||
|
# TODO: correct this (re.match with generated pattern based en groups)
|
||||||
|
line_copy = 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])
|
||||||
|
|
||||||
|
|
||||||
|
def parse(input_file: str) -> Spring:
|
||||||
|
spring_lines = []
|
||||||
|
with open(input_file, "r", encoding="utf-8") as inputfd:
|
||||||
|
while line := inputfd.readline():
|
||||||
|
if match := re.match("([?#.]+)\s([0-9,]+)", line):
|
||||||
|
# print(match.group(0))
|
||||||
|
spring_lines.append(
|
||||||
|
SpringLine(
|
||||||
|
match.group(1),
|
||||||
|
tuple(map(lambda c: int(c), match.group(2).split(","))),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
return Spring(spring_lines)
|
6
day12/example_input.txt
Normal file
6
day12/example_input.txt
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
???.### 1,1,3
|
||||||
|
.??..??...?##. 1,1,3
|
||||||
|
?#?#?#?#?#?#?#? 1,3,1,6
|
||||||
|
????.#...#... 4,1,1
|
||||||
|
????.######..#####. 1,6,5
|
||||||
|
?###???????? 3,2,1
|
13
day12/part1.py
Executable file
13
day12/part1.py
Executable file
@ -0,0 +1,13 @@
|
|||||||
|
#!/usr/bin/env python3.11
|
||||||
|
|
||||||
|
from common import *
|
||||||
|
|
||||||
|
|
||||||
|
def part1(input_file: str) -> int:
|
||||||
|
spring = parse(input_file)
|
||||||
|
print(spring)
|
||||||
|
interm = list(map(lambda s: s.arrangments(), spring))
|
||||||
|
print(interm)
|
||||||
|
interm = list(map(len, interm))
|
||||||
|
print(interm)
|
||||||
|
return sum(interm)
|
3
day12/part2.py
Executable file
3
day12/part2.py
Executable file
@ -0,0 +1,3 @@
|
|||||||
|
#!/usr/bin/env python3.11
|
||||||
|
|
||||||
|
from common import *
|
31
day12/test.py
Executable file
31
day12/test.py
Executable file
@ -0,0 +1,31 @@
|
|||||||
|
#!/usr/bin/env python3.11
|
||||||
|
|
||||||
|
import logging
|
||||||
|
from unittest import TestCase, main
|
||||||
|
from common import *
|
||||||
|
from part1 import part1
|
||||||
|
|
||||||
|
|
||||||
|
class DayXTests(TestCase):
|
||||||
|
def test_parsing(self):
|
||||||
|
parsed_spring = parse("example_input.txt")
|
||||||
|
spring: Spring = [
|
||||||
|
SpringLine("???.###", (1, 1, 3)),
|
||||||
|
SpringLine(".??..??...?##.", (1, 1, 3)),
|
||||||
|
SpringLine("?#?#?#?#?#?#?#?", (1, 3, 1, 6)),
|
||||||
|
SpringLine("????.#...#...", (4, 1, 1)),
|
||||||
|
SpringLine("????.######..#####.", (1, 6, 5)),
|
||||||
|
SpringLine("?###????????", (3, 2, 1)),
|
||||||
|
]
|
||||||
|
self.assertEqual(parsed_spring, spring)
|
||||||
|
|
||||||
|
def test_part1(self):
|
||||||
|
self.assertEqual(part1("example_input.txt"), 21)
|
||||||
|
|
||||||
|
def test_part2(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
logging.basicConfig(level=logging.DEBUG)
|
||||||
|
main(verbosity=2)
|
2
template/common.py
Normal file
2
template/common.py
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
def parse(input_file: str) -> T:
|
||||||
|
pass
|
0
template/example_input.txt
Normal file
0
template/example_input.txt
Normal file
3
template/part1.py
Executable file
3
template/part1.py
Executable file
@ -0,0 +1,3 @@
|
|||||||
|
#!/usr/bin/env python3.11
|
||||||
|
|
||||||
|
from common import *
|
3
template/part2.py
Executable file
3
template/part2.py
Executable file
@ -0,0 +1,3 @@
|
|||||||
|
#!/usr/bin/env python3.11
|
||||||
|
|
||||||
|
from common import *
|
21
template/test.py
Executable file
21
template/test.py
Executable file
@ -0,0 +1,21 @@
|
|||||||
|
#!/usr/bin/env python3.11
|
||||||
|
|
||||||
|
from unittest import TestCase, main
|
||||||
|
import logging
|
||||||
|
from common import *
|
||||||
|
|
||||||
|
|
||||||
|
class DayXTests(TestCase):
|
||||||
|
def test_parsing(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def test_part1(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def test_part2(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
logging.basicConfig(level=logging.DEBUG)
|
||||||
|
main(verbosity=2)
|
Loading…
x
Reference in New Issue
Block a user