Compare commits

...

2 Commits

Author SHA1 Message Date
9e21d8d9c8 retyped day2, completed days 3 and 4, modified template 2024-12-01 16:19:37 +01:00
42bab0cb9e Day 2 completed 2024-12-01 15:22:59 +01:00
14 changed files with 222 additions and 0 deletions

3
day2/common.py Normal file
View File

@ -0,0 +1,3 @@
def parse(filename: str) -> list[tuple[str, str]]:
with open(filename) as f:
return [tuple(a[:-1].split()) for a in f.readlines()]

37
day2/part1.py Normal file
View File

@ -0,0 +1,37 @@
from common import parse
def round(game: tuple[str, str]) -> int:
if game[0] == "A":
if game[1] == "X":
return 1 + 3
elif game[1] == "Y":
return 2 + 6
else:
return 3 + 0
elif game[0] == "B":
if game[1] == "X":
return 1 + 0
elif game[1] == "Y":
return 2 + 3
else:
return 3 + 6
else:
if game[1] == "X":
return 1 + 6
elif game[1] == "Y":
return 2 + 0
else:
return 3 + 3
def solve(strategy: list[tuple[str, str]]) -> int:
return sum(map(round, strategy))
def main():
print(solve(parse("input")))
if __name__ == "__main__":
main()

37
day2/part2.py Normal file
View File

@ -0,0 +1,37 @@
from common import parse
def round(game: tuple[str, str]) -> int:
if game[0] == "A":
if game[1] == "X":
return 3
elif game[1] == "Y":
return 3 + 1
else:
return 6 + 2
elif game[0] == "B":
if game[1] == "X":
return 1
elif game[1] == "Y":
return 3 + 2
else:
return 6 + 3
else:
if game[1] == "X":
return 2
elif game[1] == "Y":
return 3 + 3
else:
return 6 + 1
def solve(strategy: list[tuple[str, str]]) -> int:
return sum(map(round, strategy))
def main():
print(solve(parse("input")))
if __name__ == "__main__":
main()

1
day3/common.py Normal file
View File

@ -0,0 +1 @@
# Nothing in common

22
day3/part1.py Normal file
View 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
View 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
View File

@ -0,0 +1,6 @@
vJrwpWtwJgWrhcsFMMfFFhFp
jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL
PmmdzqPrVvPwwTWBwg
wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn
ttgJtRGJQctTZtZT
CrZsJsPPZsGzwwsLwLmpwMDw

29
day4/common.py Normal file
View 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
View 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
View 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
View 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

View File

@ -1,4 +1,5 @@
#!/bin/bash
day=$1
mkdir day${day}
cp template/* day${day}
curl -o day${day}/input --cookie ~/AdventOfCode/2022/cookies.txt https://adventofcode.com/2022/day/$day/input

3
template/common.py Normal file
View File

@ -0,0 +1,3 @@
def parse(filename: str):
with open(filename) as f:
return [line[:-1] for line in f.readlines()]

13
template/part1.py Normal file
View File

@ -0,0 +1,13 @@
from common import parse
def solve(input):
pass
def main():
print(solve(parse("input")))
if __name__ == "__main__":
main()