retyped day2, completed days 3 and 4, modified template
This commit is contained in:
parent
42bab0cb9e
commit
9e21d8d9c8
@ -1,6 +1,3 @@
|
|||||||
from typing import Any
|
def parse(filename: str) -> list[tuple[str, str]]:
|
||||||
|
|
||||||
|
|
||||||
def parse(filename: str) -> Any | None:
|
|
||||||
with open(filename) as f:
|
with open(filename) as f:
|
||||||
return [tuple(a[:-1].split()) for a in f.readlines()]
|
return [tuple(a[:-1].split()) for a in f.readlines()]
|
||||||
|
1
day3/common.py
Normal file
1
day3/common.py
Normal file
@ -0,0 +1 @@
|
|||||||
|
# Nothing in common
|
22
day3/part1.py
Normal file
22
day3/part1.py
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
from string import ascii_lowercase
|
||||||
|
|
||||||
|
|
||||||
|
def parse(filename: str) -> list[tuple[str, str]]:
|
||||||
|
with open(filename) as f:
|
||||||
|
return list(map(lambda x: (x[0 : int((len(x) - 1) / 2)], x[int((len(x) - 1) / 2) : -1]), f.readlines()))
|
||||||
|
|
||||||
|
|
||||||
|
def convert(char: str) -> int:
|
||||||
|
return ord(char) - 96 if char in ascii_lowercase else ord(char) - 38
|
||||||
|
|
||||||
|
|
||||||
|
def solve(bagpacks: list[tuple[str, str]]) -> int:
|
||||||
|
return sum(map(convert, [(filter(lambda x: x in bagpack[1], bagpack[0])).__next__() for bagpack in bagpacks]))
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
print(solve(parse("input")))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
38
day3/part2.py
Normal file
38
day3/part2.py
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
from string import ascii_lowercase
|
||||||
|
|
||||||
|
|
||||||
|
def parse(filename: str) -> list[tuple[str, str, str]]:
|
||||||
|
groups = []
|
||||||
|
i = 0
|
||||||
|
group = []
|
||||||
|
with open(filename) as f:
|
||||||
|
for line in f.readlines():
|
||||||
|
if i % 3 == 0 and i != 0:
|
||||||
|
groups.append(group)
|
||||||
|
group = []
|
||||||
|
group.append(line[:-1])
|
||||||
|
i += 1
|
||||||
|
groups.append(group)
|
||||||
|
return tuple(groups)
|
||||||
|
|
||||||
|
|
||||||
|
def convert(char: str) -> int:
|
||||||
|
return ord(char) - 96 if char in ascii_lowercase else ord(char) - 38
|
||||||
|
|
||||||
|
|
||||||
|
def common_character(group: tuple[str, str, str]) -> str:
|
||||||
|
assert len(group) == 3
|
||||||
|
b1, b2, b3 = group
|
||||||
|
return filter(lambda c: c in b2 and c in b3, b1).__next__()
|
||||||
|
|
||||||
|
|
||||||
|
def solve(groups: list[tuple[str, str, str]]) -> int:
|
||||||
|
return sum(map(convert, map(common_character, groups)))
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
print(solve(parse("input")))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
6
day3/toy_input
Normal file
6
day3/toy_input
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
vJrwpWtwJgWrhcsFMMfFFhFp
|
||||||
|
jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL
|
||||||
|
PmmdzqPrVvPwwTWBwg
|
||||||
|
wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn
|
||||||
|
ttgJtRGJQctTZtZT
|
||||||
|
CrZsJsPPZsGzwwsLwLmpwMDw
|
29
day4/common.py
Normal file
29
day4/common.py
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import re
|
||||||
|
from typing import NamedTuple
|
||||||
|
|
||||||
|
|
||||||
|
class Range(NamedTuple):
|
||||||
|
first: int
|
||||||
|
last: int
|
||||||
|
|
||||||
|
def __contains__(self, other: Range) -> bool:
|
||||||
|
return self.first <= other.first and self.last >= other.last
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return f"{self.first}-{self.last}"
|
||||||
|
|
||||||
|
def overlaps_with(self, other: Range) -> bool:
|
||||||
|
return Range(other.first, other.first) in self or Range(other.last, other.last) in self
|
||||||
|
|
||||||
|
|
||||||
|
def parse(filename: str) -> list[tuple[Range, Range]]:
|
||||||
|
pairs = []
|
||||||
|
with open(filename) as f:
|
||||||
|
while line := f.readline():
|
||||||
|
if match := re.match(r"(\d+)-(\d+),(\d+)-(\d+)", line):
|
||||||
|
pairs.append(
|
||||||
|
(Range(int(match.group(1)), int(match.group(2))), Range(int(match.group(3)), int(match.group(4))))
|
||||||
|
)
|
||||||
|
return pairs
|
13
day4/part1.py
Normal file
13
day4/part1.py
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
from common import Range, parse
|
||||||
|
|
||||||
|
|
||||||
|
def solve(input: list[tuple[Range, Range]]) -> int:
|
||||||
|
return len(list(filter(lambda x: x[0] in x[1] or x[1] in x[0], input)))
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
print(solve(parse("input")))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
13
day4/part2.py
Normal file
13
day4/part2.py
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
from common import Range, parse
|
||||||
|
|
||||||
|
|
||||||
|
def solve(input: list[tuple[Range, Range]]) -> int:
|
||||||
|
return len(list(filter(lambda x: x[0].overlaps_with(x[1]) or x[1].overlaps_with(x[0]), input)))
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
print(solve(parse("input")))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
6
day4/toy_input
Normal file
6
day4/toy_input
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
2-4,6-8
|
||||||
|
2-3,4-5
|
||||||
|
5-7,7-9
|
||||||
|
2-8,3-7
|
||||||
|
6-6,4-6
|
||||||
|
2-6,4-8
|
@ -1,5 +1,3 @@
|
|||||||
from typing import Any
|
def parse(filename: str):
|
||||||
|
with open(filename) as f:
|
||||||
|
return [line[:-1] for line in f.readlines()]
|
||||||
def parse(filename: str) -> Any | None:
|
|
||||||
pass
|
|
||||||
|
@ -1,13 +1,12 @@
|
|||||||
from common import parse
|
from common import parse
|
||||||
|
|
||||||
|
|
||||||
def solve():
|
def solve(input):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
r = solve()
|
print(solve(parse("input")))
|
||||||
print(r)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
@ -1,14 +0,0 @@
|
|||||||
from common import parse
|
|
||||||
|
|
||||||
|
|
||||||
def solve():
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
|
||||||
r = solve()
|
|
||||||
print(r)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
main()
|
|
Loading…
x
Reference in New Issue
Block a user