Day1 completed

This commit is contained in:
Fedaya 2024-12-01 14:15:11 +01:00
commit 47cab6a2c7
5 changed files with 48 additions and 0 deletions

5
.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
# Created by venv; see https://docs.python.org/3/library/venv.html
cookies.txt
**/input
__pycache__
.venv

17
day1/common.py Normal file
View File

@ -0,0 +1,17 @@
def parse(filename: str) -> list:
with open(filename, 'r') as f:
return [l[:-1] for l in f.readlines()]
def split(parsed: list[str]) -> tuple[list[int], list[int]]:
l1 = []
l2 = []
for line in parsed:
c1, c2 = line.split(" ")
l1.append(int(c1))
l2.append(int(c2))
return l1, l2
def split_and_sort(parsed: list[str]) -> tuple[list[int], list[int]]:
l1, l2 = split(parsed)
return sorted(l1), sorted(l2)

12
day1/part1.py Normal file
View File

@ -0,0 +1,12 @@
from common import parse, split_and_sort
def main() -> None:
l1, l2 = split_and_sort(parse('input'))
assert ( len(l1) == len(l2))
print(sum(map(lambda i: abs(l1[i] - l2[i]), range(len(l1)))))
if __name__ == '__main__':
main()

10
day1/part2.py Normal file
View File

@ -0,0 +1,10 @@
from common import parse, split
def main():
l1, l2 = split(parse('input'))
print(sum(map(lambda x: x * len(list(filter(lambda y: y == x, l2))), l1)))
if __name__ == '__main__':
main()

4
get_input.sh Executable file
View File

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