🎉」 init(2025): day 1

This commit is contained in:
2025-12-01 11:25:06 +01:00
commit 6306ab23d1
52 changed files with 2012 additions and 0 deletions

1
2024/day09/.envrc Normal file
View File

@@ -0,0 +1 @@
use flake

27
2024/day09/flake.lock generated Normal file
View File

@@ -0,0 +1,27 @@
{
"nodes": {
"nixpkgs": {
"flake": false,
"locked": {
"lastModified": 1733412085,
"narHash": "sha256-FillH0qdWDt/nlO6ED7h4cmN+G9uXwGjwmCnHs0QVYM=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "4dc2fc4e62dbf62b84132fe526356fbac7b03541",
"type": "github"
},
"original": {
"id": "nixpkgs",
"ref": "nixos-24.11",
"type": "indirect"
}
},
"root": {
"inputs": {
"nixpkgs": "nixpkgs"
}
}
},
"root": "root",
"version": 7
}

28
2024/day09/flake.nix Normal file
View File

@@ -0,0 +1,28 @@
{
description = "a test development environment for python";
inputs = {
nixpkgs = {
url = "nixpkgs/nixos-24.11";
flake = false;
};
};
outputs = inputs@{ nixpkgs, ... }:
{
devShell = {
x86_64-linux = let
pkgs = import nixpkgs { system = "x86_64-linux"; };
in pkgs.mkShell {
packages = with pkgs; [
gcc python3
];
shellHook = ''
echo -e "\x1B[0;33mentering python test development environment...\x1B[0m"
'';
};
};
};
}

39
2024/day09/part1.py Normal file
View File

@@ -0,0 +1,39 @@
import os
import os.path
def checksum(hdd):
res = 0
for i in range(len(hdd)):
if hdd[i] != '.':
res += i * int(hdd[i])
return (res)
def main():
file_path = "./input.txt"
if (os.path.isfile(file_path)):
content = open(file_path, "r").read()
else:
content = "2333133121414131402"
hdd = []
for i in range(len(content)):
if i % 2 == 0:
hdd.extend([i // 2 for x in range(int(content[i]))])
else:
hdd.extend(['.' for x in range(int(content[i]))])
print(hdd)
for i in range(len(hdd) - 1, -1, -1):
for j in range(len(hdd[:i])):
if (hdd[j] == '.'):
hdd[i], hdd[j] = hdd[j], hdd[i]
break
print(checksum(hdd))
if __name__ == '__main__':
main()

56
2024/day09/part2.py Normal file
View File

@@ -0,0 +1,56 @@
import os
import os.path
def checksum(hdd):
res = 0
for i in range(len(hdd)):
if hdd[i] != '.':
res += i * int(hdd[i])
return (res)
def has_free_size(hdd, j, size):
block_size = 0
while hdd[j] == '.':
block_size += 1
if block_size == size:
return True
j += 1
return False
def main():
file_path = "./input.txt"
if os.path.isfile(file_path):
content = open(file_path, "r").read()
else:
content = "2333133121414131402"
hdd = []
for i in range(len(content)):
if i % 2 == 0:
hdd.extend([i // 2 for x in range(int(content[i]))])
else:
hdd.extend(['.' for x in range(int(content[i]))])
print(hdd)
block_len = 1
for i in range(len(hdd) - 1, -1, -1):
if '.' not in [hdd[i], hdd[i - 1]] and int(hdd[i - 1]) == int(hdd[i]):
block_len += 1
continue
for j in range(len(hdd[:i])):
if has_free_size(hdd, j, block_len):
for k in range(block_len):
hdd[i + k], hdd[j + k] = hdd[j + k], hdd[i + k]
block_len = 1
break
block_len = 1
print(checksum(hdd))
if __name__ == '__main__':
main()