Day 1 completed

This commit is contained in:
Fedaya 2024-12-01 15:02:34 +01:00
commit 5e782cf3d7
6 changed files with 59 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

11
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,11 @@
{
"[python]": {
"editor.formatOnSave": true,
"editor.defaultFormatter": "charliermarsh.ruff",
"editor.codeActionsOnSave": {
"source.organizeImports": "explicit"
}
},
"ruff.nativeServer": "on",
"ruff.lineLength": 119
}

11
day1/common.py Normal file
View File

@ -0,0 +1,11 @@
def parse(filename: str) -> list[list[int]]:
elves = []
elf = []
with open(filename, "r") as f:
while line := f.readline():
if line == "\n":
elves.append(elf)
elf = []
else:
elf.append(int(line[:-1]))
return elves

14
day1/part1.py Normal file
View File

@ -0,0 +1,14 @@
from common import parse
def solve(elves: list[list[int]]) -> int:
return (sorted(map(sum, elves), reverse=True))[0]
def main():
elves = parse("input")
print(solve(elves))
if __name__ == "__main__":
main()

14
day1/part2.py Normal file
View File

@ -0,0 +1,14 @@
from common import parse
def solve(elves: list[list[int]]) -> int:
return sum(list(sorted(map(sum, elves), reverse=True))[0:3])
def main():
elves = parse("input")
print(solve(elves))
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/2022/cookies.txt https://adventofcode.com/2022/day/$day/input