commit 47cab6a2c7ad433c066181edb129c5a82bd09a39 Author: Fedaya Date: Sun Dec 1 14:15:11 2024 +0100 Day1 completed diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a40797e --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +# Created by venv; see https://docs.python.org/3/library/venv.html +cookies.txt +**/input +__pycache__ +.venv diff --git a/day1/common.py b/day1/common.py new file mode 100644 index 0000000..ef1615e --- /dev/null +++ b/day1/common.py @@ -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) \ No newline at end of file diff --git a/day1/part1.py b/day1/part1.py new file mode 100644 index 0000000..62e80bc --- /dev/null +++ b/day1/part1.py @@ -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() \ No newline at end of file diff --git a/day1/part2.py b/day1/part2.py new file mode 100644 index 0000000..abd910f --- /dev/null +++ b/day1/part2.py @@ -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() \ No newline at end of file diff --git a/get_input.sh b/get_input.sh new file mode 100755 index 0000000..d417e06 --- /dev/null +++ b/get_input.sh @@ -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