「🎉」 init(2025): day 1
This commit is contained in:
2
2024/.gitignore
vendored
Normal file
2
2024/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
.direnv/
|
||||
*/*.txt
|
||||
3
2024/README.md
Normal file
3
2024/README.md
Normal file
@@ -0,0 +1,3 @@
|
||||
# 🎄 advent of code 2024
|
||||
|
||||
<img src="https://static.wikia.nocookie.net/59fc6b6c-9840-456b-afd2-fd775bf46bc2/scale-to-width/755" height=800 width=1200>
|
||||
24
2024/day01/part1.sh
Executable file
24
2024/day01/part1.sh
Executable file
@@ -0,0 +1,24 @@
|
||||
if test -f cleaned_input.txt; then
|
||||
INPUT=$(cat cleaned_input.txt)
|
||||
else
|
||||
INPUT="3 4\n4 3\n2 5\n1 3\n3 9\n3 3"
|
||||
fi
|
||||
|
||||
|
||||
L1=$(printf "%b" "$INPUT" | awk '{print $1}' | sort -n)
|
||||
L2=$(printf "%b" "$INPUT" | awk '{print $2}' | sort -n)
|
||||
|
||||
L1_ARRAY=($L1)
|
||||
L2_ARRAY=($L2)
|
||||
|
||||
SUM=0
|
||||
|
||||
for i in ${!L1_ARRAY[@]}; do
|
||||
DIFF=$(( ${L1_ARRAY[$i]} - ${L2_ARRAY[$i]} ))
|
||||
DIFF=${DIFF#-} #get absolute value
|
||||
SUM=$(( $SUM + $DIFF ))
|
||||
#echo $SUM
|
||||
done
|
||||
|
||||
echo $SUM
|
||||
|
||||
24
2024/day01/part2.sh
Executable file
24
2024/day01/part2.sh
Executable file
@@ -0,0 +1,24 @@
|
||||
if test -f cleaned_input.txt; then
|
||||
INPUT=$(cat cleaned_input.txt)
|
||||
else
|
||||
INPUT="3 4\n4 3\n2 5\n1 3\n3 9\n3 3"
|
||||
fi
|
||||
|
||||
L1=$(printf "%b" "$INPUT" | awk '{print $1}' | sort -n)
|
||||
L2=$(printf "%b" "$INPUT" | awk '{print $2}' | sort -n)
|
||||
|
||||
L1_ARRAY=($L1)
|
||||
L2_ARRAY=($L2)
|
||||
|
||||
SUM=0
|
||||
|
||||
for i in ${!L1_ARRAY[@]}; do
|
||||
for j in ${!L2_ARRAY[@]}; do
|
||||
if [ "${L1_ARRAY[$i]}" = "${L2_ARRAY[$j]}" ]; then
|
||||
SUM=$(( $SUM + ${L1_ARRAY[$i]} ))
|
||||
fi
|
||||
done
|
||||
done
|
||||
|
||||
echo $SUM
|
||||
|
||||
1
2024/day02/.envrc
Normal file
1
2024/day02/.envrc
Normal file
@@ -0,0 +1 @@
|
||||
use flake
|
||||
27
2024/day02/flake.lock
generated
Normal file
27
2024/day02/flake.lock
generated
Normal file
@@ -0,0 +1,27 @@
|
||||
{
|
||||
"nodes": {
|
||||
"nixpkgs": {
|
||||
"flake": false,
|
||||
"locked": {
|
||||
"lastModified": 1733016324,
|
||||
"narHash": "sha256-8qwPSE2g1othR1u4uP86NXxm6i7E9nHPyJX3m3lx7Q4=",
|
||||
"owner": "NixOS",
|
||||
"repo": "nixpkgs",
|
||||
"rev": "7e1ca67996afd8233d9033edd26e442836cc2ad6",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"id": "nixpkgs",
|
||||
"ref": "nixos-24.05",
|
||||
"type": "indirect"
|
||||
}
|
||||
},
|
||||
"root": {
|
||||
"inputs": {
|
||||
"nixpkgs": "nixpkgs"
|
||||
}
|
||||
}
|
||||
},
|
||||
"root": "root",
|
||||
"version": 7
|
||||
}
|
||||
29
2024/day02/flake.nix
Normal file
29
2024/day02/flake.nix
Normal file
@@ -0,0 +1,29 @@
|
||||
{
|
||||
description = "a minimal test development environment for rust";
|
||||
|
||||
inputs = {
|
||||
|
||||
nixpkgs = {
|
||||
url = "nixpkgs/nixos-24.05";
|
||||
flake = false;
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
outputs = inputs@{ nixpkgs, ... }:
|
||||
{
|
||||
devShell = {
|
||||
x86_64-linux = let
|
||||
pkgs = import nixpkgs { system = "x86_64-linux"; };
|
||||
in pkgs.mkShell {
|
||||
packages = with pkgs; [
|
||||
rustc gcc
|
||||
];
|
||||
shellHook = ''
|
||||
export CC=gcc
|
||||
echo -e "\x1B[0;33mentering rust minimalist test development environment...\x1B[0m"
|
||||
'';
|
||||
};
|
||||
};
|
||||
};
|
||||
}
|
||||
34
2024/day02/part1.rs
Normal file
34
2024/day02/part1.rs
Normal file
@@ -0,0 +1,34 @@
|
||||
use std::fs;
|
||||
|
||||
fn is_ordered(numbers: &[i32]) -> bool
|
||||
{
|
||||
let is_increasing = numbers.windows(2).all(|w| w[0] < w[1]);
|
||||
let is_decreasing = numbers.windows(2).all(|w| w[0] > w[1]);
|
||||
|
||||
is_increasing || is_decreasing
|
||||
}
|
||||
|
||||
fn check_range(numbers: &[i32]) -> bool
|
||||
{
|
||||
numbers.windows(2).all(|w| (1..=3).contains(&(w[1] - w[0]).abs()))
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let content = fs::read_to_string("input.txt")
|
||||
.expect("error with input file.");
|
||||
|
||||
let mut result = 0;
|
||||
|
||||
for lines in content.split('\n')
|
||||
{
|
||||
|
||||
let values: Vec<i32> = lines.split_whitespace().map(|x| x.parse().expect("Invalid number")).collect();
|
||||
|
||||
if is_ordered(&values) && check_range(&values)
|
||||
{
|
||||
result += 1;
|
||||
}
|
||||
}
|
||||
|
||||
println!("{}", result);
|
||||
}
|
||||
47
2024/day02/part2.rs
Normal file
47
2024/day02/part2.rs
Normal file
@@ -0,0 +1,47 @@
|
||||
use std::fs;
|
||||
|
||||
fn is_ordered(numbers: &[i32]) -> bool
|
||||
{
|
||||
let is_increasing = numbers.windows(2).all(|w| w[0] < w[1]);
|
||||
let is_decreasing = numbers.windows(2).all(|w| w[0] > w[1]);
|
||||
|
||||
is_increasing || is_decreasing
|
||||
}
|
||||
|
||||
fn check_range(numbers: &[i32]) -> bool
|
||||
{
|
||||
numbers.windows(2).all(|w| (1..=3).contains(&(w[1] - w[0]).abs()))
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let content = fs::read_to_string("input.txt")
|
||||
.expect("error with input file.");
|
||||
|
||||
let mut result = 0;
|
||||
|
||||
for lines in content.split('\n')
|
||||
{
|
||||
|
||||
let values: Vec<i32> = lines.split_whitespace().map(|x| x.parse().expect("Invalid number")).collect();
|
||||
|
||||
if is_ordered(&values) && check_range(&values)
|
||||
{
|
||||
result += 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
for i in 0..values.len()
|
||||
{
|
||||
let mut modified_values = values.to_vec();
|
||||
modified_values.remove(i);
|
||||
if is_ordered(&modified_values) && check_range(&modified_values)
|
||||
{
|
||||
result += 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
println!("{}", result);
|
||||
}
|
||||
1
2024/day03/.envrc
Normal file
1
2024/day03/.envrc
Normal file
@@ -0,0 +1 @@
|
||||
use flake
|
||||
27
2024/day03/flake.lock
generated
Normal file
27
2024/day03/flake.lock
generated
Normal file
@@ -0,0 +1,27 @@
|
||||
{
|
||||
"nodes": {
|
||||
"nixpkgs": {
|
||||
"flake": false,
|
||||
"locked": {
|
||||
"lastModified": 1733016324,
|
||||
"narHash": "sha256-8qwPSE2g1othR1u4uP86NXxm6i7E9nHPyJX3m3lx7Q4=",
|
||||
"owner": "NixOS",
|
||||
"repo": "nixpkgs",
|
||||
"rev": "7e1ca67996afd8233d9033edd26e442836cc2ad6",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"id": "nixpkgs",
|
||||
"ref": "nixos-24.05",
|
||||
"type": "indirect"
|
||||
}
|
||||
},
|
||||
"root": {
|
||||
"inputs": {
|
||||
"nixpkgs": "nixpkgs"
|
||||
}
|
||||
}
|
||||
},
|
||||
"root": "root",
|
||||
"version": 7
|
||||
}
|
||||
29
2024/day03/flake.nix
Normal file
29
2024/day03/flake.nix
Normal file
@@ -0,0 +1,29 @@
|
||||
{
|
||||
description = "a minimal test development environment for ruby";
|
||||
|
||||
inputs = {
|
||||
|
||||
nixpkgs = {
|
||||
url = "nixpkgs/nixos-24.05";
|
||||
flake = false;
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
outputs = inputs@{ nixpkgs, ... }:
|
||||
{
|
||||
devShell = {
|
||||
x86_64-linux = let
|
||||
pkgs = import nixpkgs { system = "x86_64-linux"; };
|
||||
in pkgs.mkShell {
|
||||
packages = with pkgs; [
|
||||
gcc ruby
|
||||
];
|
||||
shellHook = ''
|
||||
export CC=gcc
|
||||
echo -e "\x1B[0;33mentering ruby minimalist test development environment...\x1B[0m"
|
||||
'';
|
||||
};
|
||||
};
|
||||
};
|
||||
}
|
||||
24
2024/day03/part1.rb
Normal file
24
2024/day03/part1.rb
Normal file
@@ -0,0 +1,24 @@
|
||||
if File.exists?("input.txt")
|
||||
file = File.open("input.txt")
|
||||
file_data = file.read
|
||||
file.close
|
||||
else
|
||||
file_data = "xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))"
|
||||
end
|
||||
|
||||
#puts file_data
|
||||
sane_data = file_data.scan(/mul\(\d+,\d+\)/)
|
||||
#puts sane_data.inspect
|
||||
|
||||
numbers = sane_data.map do |item|
|
||||
item.scan(/\d+/).map(&:to_i)
|
||||
end
|
||||
#puts numbers.inspect
|
||||
|
||||
result = 0
|
||||
|
||||
for mul in numbers do
|
||||
result = result + mul[0] * mul[1]
|
||||
end
|
||||
|
||||
puts result
|
||||
24
2024/day03/part2.rb
Normal file
24
2024/day03/part2.rb
Normal file
@@ -0,0 +1,24 @@
|
||||
if File.exists?("input.txt")
|
||||
file = File.open("input.txt")
|
||||
file_data = file.read
|
||||
file.close
|
||||
else
|
||||
file_data = "xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))"
|
||||
end
|
||||
|
||||
#puts file_data
|
||||
sane_data = file_data.gsub(/don\'t\(\).*?do\(\)/m, "").scan(/mul\(\d+,\d+\)/)
|
||||
#puts sane_data.inspect
|
||||
|
||||
numbers = sane_data.map do |item|
|
||||
item.scan(/\d+/).map(&:to_i)
|
||||
end
|
||||
#puts numbers.inspect
|
||||
|
||||
result = 0
|
||||
|
||||
for mul in numbers do
|
||||
result = result + mul[0] * mul[1]
|
||||
end
|
||||
|
||||
puts result
|
||||
1
2024/day04/.envrc
Normal file
1
2024/day04/.envrc
Normal file
@@ -0,0 +1 @@
|
||||
use flake
|
||||
27
2024/day04/flake.lock
generated
Normal file
27
2024/day04/flake.lock
generated
Normal file
@@ -0,0 +1,27 @@
|
||||
{
|
||||
"nodes": {
|
||||
"nixpkgs": {
|
||||
"flake": false,
|
||||
"locked": {
|
||||
"lastModified": 1733120037,
|
||||
"narHash": "sha256-En+gSoVJ3iQKPDU1FHrR6zIxSLXKjzKY+pnh9tt+Yts=",
|
||||
"owner": "NixOS",
|
||||
"repo": "nixpkgs",
|
||||
"rev": "f9f0d5c5380be0a599b1fb54641fa99af8281539",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"id": "nixpkgs",
|
||||
"ref": "nixos-24.11",
|
||||
"type": "indirect"
|
||||
}
|
||||
},
|
||||
"root": {
|
||||
"inputs": {
|
||||
"nixpkgs": "nixpkgs"
|
||||
}
|
||||
}
|
||||
},
|
||||
"root": "root",
|
||||
"version": 7
|
||||
}
|
||||
28
2024/day04/flake.nix
Normal file
28
2024/day04/flake.nix
Normal 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"
|
||||
'';
|
||||
};
|
||||
};
|
||||
};
|
||||
}
|
||||
53
2024/day04/part1.py
Normal file
53
2024/day04/part1.py
Normal file
@@ -0,0 +1,53 @@
|
||||
# sorry i gave up.
|
||||
|
||||
import os
|
||||
import os.path
|
||||
|
||||
target = "XMAS"
|
||||
|
||||
def count_horizontal(grid):
|
||||
res = 0
|
||||
for line in grid:
|
||||
res += line.count(target)
|
||||
res += line[::-1].count(target)
|
||||
return (res)
|
||||
|
||||
def count_vertical(grid):
|
||||
res = 0
|
||||
for i in range(len(grid[0])):
|
||||
res += (''.join([s[i] for s in grid if len(s) > i])).count(target)
|
||||
res += (''.join([s[i] for s in grid if len(s) > i]))[::-1].count(target)
|
||||
return (res)
|
||||
|
||||
def count_diagonal_left(grid):
|
||||
res = 0
|
||||
for i in range(len(grid)):
|
||||
res += (''.join([grid[x + i][x] for x in range(min(len(grid) - i, len(grid[0])))])).count(target)
|
||||
res += (''.join([grid[x + i][x] for x in range(min(len(grid) - i, len(grid[0])))]))[::-1].count(target)
|
||||
for i in range(1,len(grid[0])):
|
||||
res += (''.join([grid[x][x + i] for x in range(min(len(grid), len(grid[0]) - i))])).count(target)
|
||||
res += (''.join([grid[x][x + i] for x in range(min(len(grid), len(grid[0]) - i))]))[::-1].count(target)
|
||||
return (res)
|
||||
|
||||
def count_diagonal_right(grid):
|
||||
res = 0
|
||||
for i in range(len(grid)):
|
||||
res += (''.join([(grid[x + i][::-1])[x] for x in range(min(len(grid) - i, len(grid[0])))])).count(target)
|
||||
res += (''.join([(grid[x + i][::-1])[x] for x in range(min(len(grid) - i, len(grid[0])))]))[::-1].count(target)
|
||||
for i in range(1,len(grid[0])):
|
||||
res += (''.join([(grid[x][::-1])[x + i] for x in range(min(len(grid), len(grid[0]) - i))])).count(target)
|
||||
res += (''.join([(grid[x][::-1])[x + i] for x in range(min(len(grid), len(grid[0]) - i))]))[::-1].count(target)
|
||||
return (res)
|
||||
|
||||
|
||||
def main():
|
||||
file_path = "./input.txt"
|
||||
if (os.path.isfile(file_path)):
|
||||
content = open(file_path, "r").read()
|
||||
else:
|
||||
content = "MMMSXXMASM\nMSAMXMSMSA\nAMXSXMAAMM\nMSAMASMSMX\nXMASAMXAMM\nXXAMMXXAMA\nSMSMSASXSS\nSAXAMASAAA\nMAMMMXMMMM\nMXMXAXMASX"
|
||||
grid = content.split('\n')
|
||||
print(count_horizontal(grid) + count_vertical(grid) + count_diagonal_left(grid) + count_diagonal_right(grid))
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
38
2024/day04/part2.py
Normal file
38
2024/day04/part2.py
Normal file
@@ -0,0 +1,38 @@
|
||||
# sorry i gave up.
|
||||
|
||||
import os
|
||||
import os.path
|
||||
|
||||
target = "XMAS"
|
||||
|
||||
def check_x(grid, x, y):
|
||||
if (x not in range(1, len(grid[0]) - 1) or y not in range(1, len(grid) - 1)):
|
||||
return (0)
|
||||
str = grid[y - 1][x - 1] + grid[y + 1][x + 1]
|
||||
if (str not in "SMS"):
|
||||
return (0)
|
||||
str = grid[y + 1][x - 1] + grid[y - 1][x + 1]
|
||||
if (str not in "SMS"):
|
||||
return (0)
|
||||
return (1)
|
||||
|
||||
def count_xmas(grid):
|
||||
res = 0
|
||||
for y in range(len(grid)):
|
||||
for x in range(len(grid[0])):
|
||||
if (grid[y][x] == 'A'):
|
||||
res += check_x(grid, x, y)
|
||||
return (res)
|
||||
|
||||
|
||||
def main():
|
||||
file_path = "./input.txt"
|
||||
if (os.path.isfile(file_path)):
|
||||
content = open(file_path, "r").read()
|
||||
else:
|
||||
content = "MMMSXXMASM\nMSAMXMSMSA\nAMXSXMAAMM\nMSAMASMSMX\nXMASAMXAMM\nXXAMMXXAMA\nSMSMSASXSS\nSAXAMASAAA\nMAMMMXMMMM\nMXMXAXMASX"
|
||||
grid = content.split('\n')
|
||||
print(count_xmas(grid))
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
1
2024/day05/.envrc
Normal file
1
2024/day05/.envrc
Normal file
@@ -0,0 +1 @@
|
||||
use flake
|
||||
27
2024/day05/flake.lock
generated
Normal file
27
2024/day05/flake.lock
generated
Normal file
@@ -0,0 +1,27 @@
|
||||
{
|
||||
"nodes": {
|
||||
"nixpkgs": {
|
||||
"flake": false,
|
||||
"locked": {
|
||||
"lastModified": 1733261153,
|
||||
"narHash": "sha256-eq51hyiaIwtWo19fPEeE0Zr2s83DYMKJoukNLgGGpek=",
|
||||
"owner": "NixOS",
|
||||
"repo": "nixpkgs",
|
||||
"rev": "b681065d0919f7eb5309a93cea2cfa84dec9aa88",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"id": "nixpkgs",
|
||||
"ref": "nixos-24.11",
|
||||
"type": "indirect"
|
||||
}
|
||||
},
|
||||
"root": {
|
||||
"inputs": {
|
||||
"nixpkgs": "nixpkgs"
|
||||
}
|
||||
}
|
||||
},
|
||||
"root": "root",
|
||||
"version": 7
|
||||
}
|
||||
28
2024/day05/flake.nix
Normal file
28
2024/day05/flake.nix
Normal 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"
|
||||
'';
|
||||
};
|
||||
};
|
||||
};
|
||||
}
|
||||
30
2024/day05/part1.py
Normal file
30
2024/day05/part1.py
Normal file
@@ -0,0 +1,30 @@
|
||||
import os
|
||||
import os.path
|
||||
|
||||
def main():
|
||||
res = 0
|
||||
valid = 1
|
||||
|
||||
file_path = "./input.txt"
|
||||
if (os.path.isfile(file_path)):
|
||||
content = open(file_path, "r").read()
|
||||
else:
|
||||
content = "47|53\n97|13\n97|61\n97|47\n75|29\n61|13\n75|53\n29|13\n97|29\n53|29\n61|53\n97|53\n61|29\n47|13\n75|47\n97|75\n47|61\n75|61\n47|29\n75|13\n53|13\n\n75,47,61,53,29\n97,61,53,29,13\n75,29,13\n75,97,47,61,53\n61,13,29\n97,13,75,29,47"
|
||||
grid = content.split('\n')
|
||||
pages_part = grid[:grid.index("")]
|
||||
|
||||
for line in grid[grid.index("") + 1:]:
|
||||
valid = 1
|
||||
for num in line.split(','):
|
||||
for page in pages_part:
|
||||
if num in page:
|
||||
if page.split('|')[0] in line and page.split('|')[1] in line:
|
||||
if page.split('|')[1] not in line.split(',')[line.split(',').index(page.split('|')[0]):]:
|
||||
valid = 0
|
||||
if valid:
|
||||
res += int(line.split(',')[len(line.split(',')) // 2])
|
||||
|
||||
print(res)
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
46
2024/day05/part2.py
Normal file
46
2024/day05/part2.py
Normal file
@@ -0,0 +1,46 @@
|
||||
import os
|
||||
import os.path
|
||||
|
||||
def is_incorrect(line, pages_part):
|
||||
r = 0
|
||||
for num in line.split(','):
|
||||
for page in pages_part:
|
||||
if num in page:
|
||||
if page.split('|')[0] in line and page.split('|')[1] in line:
|
||||
if page.split('|')[1] not in line.split(',')[line.split(',').index(page.split('|')[0]):]:
|
||||
r = 1
|
||||
return (r)
|
||||
|
||||
def main():
|
||||
res = 0
|
||||
valid = 1
|
||||
|
||||
file_path = "./input.txt"
|
||||
if (os.path.isfile(file_path)):
|
||||
content = open(file_path, "r").read()
|
||||
else:
|
||||
content = "47|53\n97|13\n97|61\n97|47\n75|29\n61|13\n75|53\n29|13\n97|29\n53|29\n61|53\n97|53\n61|29\n47|13\n75|47\n97|75\n47|61\n75|61\n47|29\n75|13\n53|13\n\n75,47,61,53,29\n97,61,53,29,13\n75,29,13\n75,97,47,61,53\n61,13,29\n97,13,75,29,47"
|
||||
grid = content.split('\n')
|
||||
pages_part = grid[:grid.index("")]
|
||||
|
||||
for line in grid[grid.index("") + 1:]:
|
||||
if not is_incorrect(line, pages_part):
|
||||
continue
|
||||
while is_incorrect(line, pages_part):
|
||||
for num in line.split(','):
|
||||
for page in pages_part:
|
||||
if num in page:
|
||||
if page.split('|')[0] in line and page.split('|')[1] in line:
|
||||
if page.split('|')[1] not in line.split(',')[line.split(',').index(page.split('|')[0]):]:
|
||||
new = line.split(',')
|
||||
i1 = new.index(page.split('|')[0])
|
||||
i2 = new.index(page.split('|')[1])
|
||||
new[i1], new[i2] = new[i2], new[i1]
|
||||
line = ','.join(new)
|
||||
res += int(line.split(',')[len(line.split(',')) // 2])
|
||||
|
||||
print(res)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
1
2024/day06/.envrc
Normal file
1
2024/day06/.envrc
Normal file
@@ -0,0 +1 @@
|
||||
use flake
|
||||
59
2024/day06/flake.lock
generated
Normal file
59
2024/day06/flake.lock
generated
Normal file
@@ -0,0 +1,59 @@
|
||||
{
|
||||
"nodes": {
|
||||
"flake-utils": {
|
||||
"inputs": {
|
||||
"systems": "systems"
|
||||
},
|
||||
"locked": {
|
||||
"lastModified": 1731533236,
|
||||
"narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=",
|
||||
"owner": "numtide",
|
||||
"repo": "flake-utils",
|
||||
"rev": "11707dc2f618dd54ca8739b309ec4fc024de578b",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"id": "flake-utils",
|
||||
"type": "indirect"
|
||||
}
|
||||
},
|
||||
"nixpkgs": {
|
||||
"locked": {
|
||||
"lastModified": 1733261153,
|
||||
"narHash": "sha256-eq51hyiaIwtWo19fPEeE0Zr2s83DYMKJoukNLgGGpek=",
|
||||
"owner": "NixOS",
|
||||
"repo": "nixpkgs",
|
||||
"rev": "b681065d0919f7eb5309a93cea2cfa84dec9aa88",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"id": "nixpkgs",
|
||||
"ref": "nixos-24.11",
|
||||
"type": "indirect"
|
||||
}
|
||||
},
|
||||
"root": {
|
||||
"inputs": {
|
||||
"flake-utils": "flake-utils",
|
||||
"nixpkgs": "nixpkgs"
|
||||
}
|
||||
},
|
||||
"systems": {
|
||||
"locked": {
|
||||
"lastModified": 1681028828,
|
||||
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
|
||||
"owner": "nix-systems",
|
||||
"repo": "default",
|
||||
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "nix-systems",
|
||||
"repo": "default",
|
||||
"type": "github"
|
||||
}
|
||||
}
|
||||
},
|
||||
"root": "root",
|
||||
"version": 7
|
||||
}
|
||||
23
2024/day06/flake.nix
Normal file
23
2024/day06/flake.nix
Normal file
@@ -0,0 +1,23 @@
|
||||
{
|
||||
description = "a development environment for the c programming language";
|
||||
|
||||
inputs = {
|
||||
nixpkgs.url = "nixpkgs/nixos-24.11";
|
||||
};
|
||||
|
||||
outputs = { self, flake-utils, nixpkgs, ... }:
|
||||
flake-utils.lib.eachDefaultSystem(system:
|
||||
let
|
||||
pkgs = nixpkgs.legacyPackages.${system};
|
||||
in
|
||||
{
|
||||
devShells.default = pkgs.mkShell {
|
||||
packages = with pkgs; [ clang gcc valgrind gdb ];
|
||||
hardeningDisable = [ "all" ];
|
||||
buildInputs = with pkgs; [];
|
||||
shellHook = ''
|
||||
echo -e "\x1B[0;33mentering c environment...\x1B[0m"
|
||||
'';
|
||||
};
|
||||
});
|
||||
}
|
||||
229
2024/day06/part1.c
Normal file
229
2024/day06/part1.c
Normal file
@@ -0,0 +1,229 @@
|
||||
#include <stdio.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
static int free_list(char **result, int j)
|
||||
{
|
||||
while (j)
|
||||
{
|
||||
free(result[j]);
|
||||
j--;
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
|
||||
static int word_counter(const char *str, char ch)
|
||||
{
|
||||
int i;
|
||||
int c;
|
||||
int bool_c;
|
||||
|
||||
i = 0;
|
||||
c = 0;
|
||||
bool_c = 1;
|
||||
while (str[i])
|
||||
{
|
||||
if (str[i] == ch)
|
||||
{
|
||||
bool_c = 1;
|
||||
}
|
||||
else if (str[i] != ch && bool_c)
|
||||
{
|
||||
c++;
|
||||
bool_c = 0;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
return (c);
|
||||
}
|
||||
|
||||
static int per_word_fill(char **result, const char *str, char ch)
|
||||
{
|
||||
int i;
|
||||
int j;
|
||||
int c;
|
||||
|
||||
i = 0;
|
||||
j = 0;
|
||||
while (str[i])
|
||||
{
|
||||
c = 0;
|
||||
while (str[i] != ch && str[i])
|
||||
{
|
||||
c++;
|
||||
i++;
|
||||
}
|
||||
if (c != 0)
|
||||
{
|
||||
result[j] = calloc(sizeof(char), c + 1);
|
||||
if (result[j++] == NULL)
|
||||
return (free_list(result, j - 2));
|
||||
}
|
||||
if (str[i])
|
||||
i++;
|
||||
}
|
||||
result[j] = NULL;
|
||||
return (0);
|
||||
}
|
||||
|
||||
static void ft_split_resolver(char **result, const char *str, char ch)
|
||||
{
|
||||
int i;
|
||||
int j;
|
||||
int wi;
|
||||
int bool_w;
|
||||
|
||||
i = 0;
|
||||
j = 0;
|
||||
wi = 0;
|
||||
bool_w = 0;
|
||||
while (str[i])
|
||||
{
|
||||
if (str[i] == ch && bool_w)
|
||||
{
|
||||
bool_w = 0;
|
||||
wi = 0;
|
||||
j++;
|
||||
}
|
||||
else if (str[i] != ch)
|
||||
{
|
||||
bool_w = 1;
|
||||
result[j][wi] = str[i];
|
||||
wi++;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
char **ft_split(const char *str, char c)
|
||||
{
|
||||
char **result;
|
||||
|
||||
result = malloc(sizeof(char *) * (word_counter(str, c) + 1));
|
||||
if (result == NULL)
|
||||
return (NULL);
|
||||
if (per_word_fill(result, str, c))
|
||||
return (NULL);
|
||||
ft_split_resolver(result, str, c);
|
||||
return (result);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
char *content, **grid;
|
||||
int fd;
|
||||
|
||||
if ((fd = open("./input.txt", O_RDONLY)) > 0)
|
||||
{
|
||||
content = calloc(sizeof(char), 17030);
|
||||
read(fd, content, 17029);
|
||||
}
|
||||
else
|
||||
{
|
||||
content = "....#.....\n.........#\n..........\n..#.......\n.......#..\n..........\n.#..^.....\n........#.\n#.........\n......#...";
|
||||
}
|
||||
grid = ft_split(content, '\n');
|
||||
//for (int i = 0; grid[i]; i++)
|
||||
// printf("%s\n", grid[i]);
|
||||
|
||||
int res, x, y = 0;
|
||||
|
||||
for (int i = 0; grid[i]; i++)
|
||||
{
|
||||
for (int j = 0; grid[i][j]; j++)
|
||||
{
|
||||
if (grid[i][j] == '^')
|
||||
{
|
||||
x = j;
|
||||
y = i;
|
||||
goto next;
|
||||
}
|
||||
}
|
||||
}
|
||||
next:
|
||||
for(;;)
|
||||
{
|
||||
switch (grid[y][x]) {
|
||||
case '^':
|
||||
if (y == 0 || y == (strlen(grid[0]) - 1))
|
||||
{
|
||||
res++;
|
||||
printf("%d\n", res);
|
||||
return (0);
|
||||
}
|
||||
|
||||
if (grid[y - 1][x] == '#')
|
||||
grid[y][x] = '>';
|
||||
else
|
||||
{
|
||||
res += grid[y - 1][x] == '.';
|
||||
grid[y - 1][x] = '^';
|
||||
grid[y][x] = 'X';
|
||||
y--;
|
||||
}
|
||||
break;
|
||||
case '>':
|
||||
if (x == 0 || x == (strlen(grid[0]) - 1))
|
||||
{
|
||||
res++;
|
||||
printf("%d\n", res);
|
||||
return (0);
|
||||
}
|
||||
|
||||
if (grid[y][x + 1] == '#')
|
||||
{
|
||||
grid[y][x] = 'v';
|
||||
}
|
||||
else
|
||||
{
|
||||
res += grid[y][x + 1] == '.';
|
||||
grid[y][x + 1] = '>';
|
||||
grid[y][x] = 'X';
|
||||
x++;
|
||||
}
|
||||
break;
|
||||
case 'v':
|
||||
if (y == 0 || y == (strlen(grid[0]) - 1))
|
||||
{
|
||||
res++;
|
||||
printf("%d\n", res);
|
||||
return (0);
|
||||
}
|
||||
|
||||
if (grid[y + 1][x] == '#')
|
||||
{
|
||||
grid[y][x] = '<';
|
||||
}
|
||||
else
|
||||
{
|
||||
res += grid[y + 1][x] == '.';
|
||||
grid[y + 1][x] = 'v';
|
||||
grid[y][x] = 'X';
|
||||
y++;
|
||||
}
|
||||
break;
|
||||
case '<':
|
||||
if (x == 0 || x == (strlen(grid[0] - 1)))
|
||||
{
|
||||
res++;
|
||||
printf("%d\n", res);
|
||||
return (0);
|
||||
}
|
||||
|
||||
if (grid[y][x - 1] == '#')
|
||||
{
|
||||
grid[y][x] = '^';
|
||||
}
|
||||
else
|
||||
{
|
||||
res += grid[y][x - 1] == '.';
|
||||
grid[y][x - 1] = '<';
|
||||
grid[y][x] = 'X';
|
||||
x--;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
399
2024/day06/part2.c
Normal file
399
2024/day06/part2.c
Normal file
@@ -0,0 +1,399 @@
|
||||
#include <stdio.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
static int free_list(char **result, int j)
|
||||
{
|
||||
while (j)
|
||||
{
|
||||
free(result[j]);
|
||||
j--;
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
|
||||
static int word_counter(const char *str, char ch)
|
||||
{
|
||||
int i;
|
||||
int c;
|
||||
int bool_c;
|
||||
|
||||
i = 0;
|
||||
c = 0;
|
||||
bool_c = 1;
|
||||
while (str[i])
|
||||
{
|
||||
if (str[i] == ch)
|
||||
{
|
||||
bool_c = 1;
|
||||
}
|
||||
else if (str[i] != ch && bool_c)
|
||||
{
|
||||
c++;
|
||||
bool_c = 0;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
return (c);
|
||||
}
|
||||
|
||||
static int per_word_fill(char **result, const char *str, char ch)
|
||||
{
|
||||
int i;
|
||||
int j;
|
||||
int c;
|
||||
|
||||
i = 0;
|
||||
j = 0;
|
||||
while (str[i])
|
||||
{
|
||||
c = 0;
|
||||
while (str[i] != ch && str[i])
|
||||
{
|
||||
c++;
|
||||
i++;
|
||||
}
|
||||
if (c != 0)
|
||||
{
|
||||
result[j] = calloc(sizeof(char), c + 1);
|
||||
if (result[j++] == NULL)
|
||||
return (free_list(result, j - 2));
|
||||
}
|
||||
if (str[i])
|
||||
i++;
|
||||
}
|
||||
result[j] = NULL;
|
||||
return (0);
|
||||
}
|
||||
|
||||
static void ft_split_resolver(char **result, const char *str, char ch)
|
||||
{
|
||||
int i;
|
||||
int j;
|
||||
int wi;
|
||||
int bool_w;
|
||||
|
||||
i = 0;
|
||||
j = 0;
|
||||
wi = 0;
|
||||
bool_w = 0;
|
||||
while (str[i])
|
||||
{
|
||||
if (str[i] == ch && bool_w)
|
||||
{
|
||||
bool_w = 0;
|
||||
wi = 0;
|
||||
j++;
|
||||
}
|
||||
else if (str[i] != ch)
|
||||
{
|
||||
bool_w = 1;
|
||||
result[j][wi] = str[i];
|
||||
wi++;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
char **ft_split(const char *str, char c)
|
||||
{
|
||||
char **result;
|
||||
|
||||
result = malloc(sizeof(char *) * (word_counter(str, c) + 1));
|
||||
if (result == NULL)
|
||||
return (NULL);
|
||||
if (per_word_fill(result, str, c))
|
||||
return (NULL);
|
||||
ft_split_resolver(result, str, c);
|
||||
return (result);
|
||||
}
|
||||
|
||||
typedef struct s_state{
|
||||
int x;
|
||||
int y;
|
||||
char direction;
|
||||
} t_state;
|
||||
|
||||
typedef struct s_coord{
|
||||
int x;
|
||||
int y;
|
||||
} t_coord;
|
||||
|
||||
char **cpy_grid(char **grid)
|
||||
{
|
||||
char **new;
|
||||
|
||||
new = calloc(sizeof(char *), 131);
|
||||
for (int i = 0; grid[i]; i++)
|
||||
new[i] = strdup(grid[i]);
|
||||
return (new);
|
||||
}
|
||||
|
||||
int has_visited(t_state *visited, int count, int x, int y, char direction) {
|
||||
for (int i = 0; i < count; i++)
|
||||
if (visited[i].x == x && visited[i].y == y && visited[i].direction == direction)
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int add_solution(t_coord *solutions, int count, int x, int y)
|
||||
{
|
||||
for (int i = 0; i < count; i++)
|
||||
if (solutions[i].x == x && solutions[i].y == y)
|
||||
return (0);
|
||||
return (1);
|
||||
}
|
||||
|
||||
int check_ray(char **old_grid, int x, int y, char dir)
|
||||
{
|
||||
int old_x = x;
|
||||
int old_y = y;
|
||||
int c = 0;
|
||||
|
||||
char **grid = cpy_grid(old_grid);
|
||||
t_state *visited = calloc(sizeof(t_state), (strlen(grid[0]) * strlen(grid[0])) * 4);
|
||||
int visited_index = 0;
|
||||
|
||||
switch (dir)
|
||||
{
|
||||
case '^':
|
||||
if (grid[y][x - 1] == '^')
|
||||
grid[y - 1][x - 1] = '^';
|
||||
grid[y][x - 1] = '#';
|
||||
break;
|
||||
case '>':
|
||||
if (grid[y - 1][x] == '^')
|
||||
grid[y - 2][x] = '^';
|
||||
grid[y - 1][x] = '#';
|
||||
break;
|
||||
case 'v':
|
||||
if (grid[y][x + 1] == '^')
|
||||
grid[y - 1][x + 1] = '^';
|
||||
grid[y][x + 1] = '#';
|
||||
break;
|
||||
case '<':
|
||||
if (grid[y][x] == '^')
|
||||
grid[y][x] = '^';
|
||||
grid[y + 1][x] = '#';
|
||||
break;
|
||||
}
|
||||
|
||||
for (int i = 0; grid[i]; i++)
|
||||
{
|
||||
for (int j = 0; grid[i][j]; j++)
|
||||
{
|
||||
if (grid[i][j] == '^')
|
||||
{
|
||||
x = j;
|
||||
y = i;
|
||||
goto next_check_ray;
|
||||
}
|
||||
}
|
||||
}
|
||||
next_check_ray:
|
||||
|
||||
|
||||
for(;;)
|
||||
{
|
||||
switch (grid[y][x])
|
||||
{
|
||||
case '^':
|
||||
if (y == 0 || y == (strlen(grid[0]) - 1))
|
||||
return (0);
|
||||
|
||||
if (grid[y - 1][x] == '#')
|
||||
grid[y][x] = '>';
|
||||
else
|
||||
{
|
||||
grid[y - 1][x] = '^';
|
||||
y--;
|
||||
}
|
||||
break;
|
||||
case '>':
|
||||
if (x == 0 || x == (strlen(grid[0]) - 1))
|
||||
return (0);
|
||||
|
||||
if (grid[y][x + 1] == '#')
|
||||
grid[y][x] = 'v';
|
||||
else
|
||||
{
|
||||
grid[y][x + 1] = '>';
|
||||
x++;
|
||||
}
|
||||
break;
|
||||
case 'v':
|
||||
if (y == 0 || y == (strlen(grid[0]) - 1))
|
||||
return (0);
|
||||
|
||||
if (grid[y + 1][x] == '#')
|
||||
grid[y][x] = '<';
|
||||
else
|
||||
{
|
||||
grid[y + 1][x] = 'v';
|
||||
y++;
|
||||
}
|
||||
break;
|
||||
case '<':
|
||||
if (x == 0 || x == (strlen(grid[0] - 1)))
|
||||
return (0);
|
||||
|
||||
if (grid[y][x - 1] == '#')
|
||||
grid[y][x] = '^';
|
||||
else
|
||||
{
|
||||
grid[y][x - 1] = '<';
|
||||
x--;
|
||||
}
|
||||
break;
|
||||
}
|
||||
if (has_visited(visited, visited_index, x, y, grid[y][x]))
|
||||
return (1);
|
||||
visited[visited_index++] = (t_state){x, y, grid[y][x]};
|
||||
}
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
char *content, **grid;
|
||||
int fd;
|
||||
|
||||
if ((fd = open("./input.txt", O_RDONLY)) > 0)
|
||||
{
|
||||
content = calloc(sizeof(char), 17030);
|
||||
read(fd, content, 17029);
|
||||
}
|
||||
else
|
||||
{
|
||||
content = "....#.....\n.........#\n..........\n..#.......\n.......#..\n..........\n.#..^.....\n........#.\n#.........\n......#...";
|
||||
}
|
||||
grid = ft_split(content, '\n');
|
||||
|
||||
char **clean_grid = cpy_grid(grid);
|
||||
t_coord *solutions = calloc(sizeof(t_coord), strlen(grid[0]) * strlen(grid[0]));
|
||||
int solutions_index = 0;
|
||||
int x, y = 0;
|
||||
|
||||
for (int i = 0; grid[i]; i++)
|
||||
{
|
||||
for (int j = 0; grid[i][j]; j++)
|
||||
{
|
||||
if (grid[i][j] == '^')
|
||||
{
|
||||
x = j;
|
||||
y = i;
|
||||
goto next;
|
||||
}
|
||||
}
|
||||
}
|
||||
next:
|
||||
for(;;)
|
||||
{
|
||||
switch (grid[y][x]) {
|
||||
case '^':
|
||||
grid[y][x] = '+';
|
||||
while (y != 0 && y != (strlen(grid[0]) - 1) && grid[y - 1][x] != '#')
|
||||
{
|
||||
if (grid[y][x] == '.')
|
||||
grid[y][x] = '^';
|
||||
else
|
||||
grid[y][x] = '+';
|
||||
if(check_ray(clean_grid, x, y, '>'))
|
||||
if (add_solution(solutions, solutions_index, x, y - 1))
|
||||
solutions[solutions_index++] = (t_coord){x, y - 1};
|
||||
//res += check_ray(clean_grid, x, y, '>');
|
||||
y--;
|
||||
}
|
||||
if (y == 0 || y == (strlen(grid[0]) - 1))
|
||||
{
|
||||
printf("%d\n", solutions_index);
|
||||
return (0);
|
||||
}
|
||||
if (grid[y - 1][x] == '#')
|
||||
grid[y][x] = '>';
|
||||
break;
|
||||
|
||||
|
||||
case '>':
|
||||
grid[y][x] = '+';
|
||||
while (x != 0 && x != (strlen(grid[0]) - 1) && grid[y][x + 1] != '#')
|
||||
{
|
||||
if (grid[y][x] == '.')
|
||||
grid[y][x] = '>';
|
||||
else
|
||||
grid[y][x] = '+';
|
||||
if(check_ray(clean_grid, x, y, 'v'))
|
||||
if (add_solution(solutions, solutions_index, x + 1, y))
|
||||
solutions[solutions_index++] = (t_coord){x + 1, y};
|
||||
//res += check_ray(clean_grid, x, y, 'v');
|
||||
x++;
|
||||
}
|
||||
if (x == 0 || x == (strlen(grid[0]) - 1))
|
||||
{
|
||||
printf("%d\n", solutions_index);
|
||||
return (0);
|
||||
}
|
||||
if (grid[y][x + 1] == '#')
|
||||
{
|
||||
grid[y][x] = 'v';
|
||||
}
|
||||
break;
|
||||
|
||||
|
||||
case 'v':
|
||||
grid[y][x] = '+';
|
||||
while (y != 0 && y != (strlen(grid[0]) - 1) && grid[y + 1][x] != '#')
|
||||
{
|
||||
if (grid[y][x] == '.')
|
||||
grid[y][x] = 'v';
|
||||
else
|
||||
grid[y][x] = '+';
|
||||
if(check_ray(clean_grid, x, y, '<'))
|
||||
if (add_solution(solutions, solutions_index, x, y + 1))
|
||||
solutions[solutions_index++] = (t_coord){x, y + 1};
|
||||
//res += check_ray(clean_grid, x, y, '<');
|
||||
y++;
|
||||
}
|
||||
if (y == 0 || y == (strlen(grid[0]) - 1))
|
||||
{
|
||||
printf("%d\n", solutions_index);
|
||||
return (0);
|
||||
}
|
||||
if (grid[y + 1][x] == '#')
|
||||
{
|
||||
grid[y][x] = '<';
|
||||
}
|
||||
break;
|
||||
|
||||
|
||||
case '<':
|
||||
grid[y][x] = '+';
|
||||
while (x != 0 && x != (strlen(grid[0]) - 1) && grid[y][x - 1] != '#')
|
||||
{
|
||||
if (grid[y][x] == '.')
|
||||
grid[y][x] = '<';
|
||||
else
|
||||
grid[y][x] = '+';
|
||||
if(check_ray(clean_grid, x, y, '^'))
|
||||
if (add_solution(solutions, solutions_index, x - 1, y))
|
||||
solutions[solutions_index++] = (t_coord){x - 1, y};
|
||||
//res += check_ray(clean_grid, x, y, '^');
|
||||
x--;
|
||||
}
|
||||
if (x == 0 || x == (strlen(grid[0] - 1)))
|
||||
{
|
||||
printf("%d\n", solutions_index);
|
||||
return (0);
|
||||
}
|
||||
if (grid[y][x - 1] == '#')
|
||||
{
|
||||
grid[y][x] = '^';
|
||||
}
|
||||
break;
|
||||
|
||||
|
||||
}
|
||||
printf("i am alive %d %d\n", x, y);
|
||||
}
|
||||
}
|
||||
1
2024/day07/.envrc
Normal file
1
2024/day07/.envrc
Normal file
@@ -0,0 +1 @@
|
||||
use flake
|
||||
27
2024/day07/flake.lock
generated
Normal file
27
2024/day07/flake.lock
generated
Normal file
@@ -0,0 +1,27 @@
|
||||
{
|
||||
"nodes": {
|
||||
"nixpkgs": {
|
||||
"flake": false,
|
||||
"locked": {
|
||||
"lastModified": 1733220138,
|
||||
"narHash": "sha256-Yh5XZ9yVurrcYdNTSWxYgW4+EJ0pcOqgM1043z9JaRc=",
|
||||
"owner": "NixOS",
|
||||
"repo": "nixpkgs",
|
||||
"rev": "bcb68885668cccec12276bbb379f8f2557aa06ce",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"id": "nixpkgs",
|
||||
"ref": "nixos-24.05",
|
||||
"type": "indirect"
|
||||
}
|
||||
},
|
||||
"root": {
|
||||
"inputs": {
|
||||
"nixpkgs": "nixpkgs"
|
||||
}
|
||||
}
|
||||
},
|
||||
"root": "root",
|
||||
"version": 7
|
||||
}
|
||||
29
2024/day07/flake.nix
Normal file
29
2024/day07/flake.nix
Normal file
@@ -0,0 +1,29 @@
|
||||
{
|
||||
description = "a minimal test development environment for ruby";
|
||||
|
||||
inputs = {
|
||||
|
||||
nixpkgs = {
|
||||
url = "nixpkgs/nixos-24.05";
|
||||
flake = false;
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
outputs = inputs@{ nixpkgs, ... }:
|
||||
{
|
||||
devShell = {
|
||||
x86_64-linux = let
|
||||
pkgs = import nixpkgs { system = "x86_64-linux"; };
|
||||
in pkgs.mkShell {
|
||||
packages = with pkgs; [
|
||||
gcc ruby
|
||||
];
|
||||
shellHook = ''
|
||||
export CC=gcc
|
||||
echo -e "\x1B[0;33mentering ruby minimalist test development environment...\x1B[0m"
|
||||
'';
|
||||
};
|
||||
};
|
||||
};
|
||||
}
|
||||
60
2024/day07/part1.rb
Normal file
60
2024/day07/part1.rb
Normal file
@@ -0,0 +1,60 @@
|
||||
if File.exists?("input.txt")
|
||||
file = File.open("input.txt")
|
||||
file_data = file.read
|
||||
file.close
|
||||
else
|
||||
file_data = "190: 10 19\n3267: 81 40 27\n83: 17 5\n156: 15 6\n7290: 6 8 6 15\n161011: 16 10 13\n192: 17 8 14\n21037: 9 7 18 13\n292: 11 6 16 20"
|
||||
end
|
||||
|
||||
def eval_left_to_right(expression)
|
||||
tokens = expression.split(/(\+|\*)/).map(&:strip)
|
||||
|
||||
result = tokens.shift.to_i
|
||||
until tokens.empty?
|
||||
operator = tokens.shift
|
||||
operand = tokens.shift.to_i
|
||||
|
||||
result = result.send(operator, operand)
|
||||
end
|
||||
|
||||
result
|
||||
end
|
||||
|
||||
#puts file_data
|
||||
data_array = file_data.split("\n")
|
||||
results = []
|
||||
values = []
|
||||
|
||||
for line in data_array do
|
||||
res, val = line.split(":").map(&:strip)
|
||||
res = res.to_i
|
||||
val = val.split.map(&:to_i)
|
||||
results.push(res)
|
||||
values.push(val)
|
||||
end
|
||||
|
||||
puts results.inspect
|
||||
puts values.inspect
|
||||
|
||||
res = 0
|
||||
operations = ['+', '*']
|
||||
|
||||
results.each_with_index do |result, index|
|
||||
puts "\nindex: #{index} | #{result} : #{values[index]}"
|
||||
|
||||
combinations = operations.repeated_permutation(values[index].length - 1).to_a
|
||||
|
||||
combinations.each do |comb|
|
||||
expr = values[index].zip(comb).flatten.compact.join
|
||||
puts "#{expr} = #{eval_left_to_right(expr)}"
|
||||
|
||||
if eval_left_to_right(expr) == result
|
||||
puts "#{result}"
|
||||
res += result
|
||||
break
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
puts res
|
||||
64
2024/day07/part2.rb
Normal file
64
2024/day07/part2.rb
Normal file
@@ -0,0 +1,64 @@
|
||||
if File.exists?("input.txt")
|
||||
file = File.open("input.txt")
|
||||
file_data = file.read
|
||||
file.close
|
||||
else
|
||||
file_data = "190: 10 19\n3267: 81 40 27\n83: 17 5\n156: 15 6\n7290: 6 8 6 15\n161011: 16 10 13\n192: 17 8 14\n21037: 9 7 18 13\n292: 11 6 16 20"
|
||||
end
|
||||
|
||||
def eval_left_to_right(expression)
|
||||
tokens = expression.split(/(\+|\*|\|\|)/).map(&:strip)
|
||||
|
||||
result = tokens.shift.to_i
|
||||
until tokens.empty?
|
||||
operator = tokens.shift
|
||||
operand = tokens.shift.to_i
|
||||
|
||||
if (operator == '||')
|
||||
result = (result.to_s + operand.to_s).to_i
|
||||
else
|
||||
result = result.send(operator, operand)
|
||||
end
|
||||
end
|
||||
|
||||
result
|
||||
end
|
||||
|
||||
#puts file_data
|
||||
data_array = file_data.split("\n")
|
||||
results = []
|
||||
values = []
|
||||
|
||||
for line in data_array do
|
||||
res, val = line.split(":").map(&:strip)
|
||||
res = res.to_i
|
||||
val = val.split.map(&:to_i)
|
||||
results.push(res)
|
||||
values.push(val)
|
||||
end
|
||||
|
||||
puts results.inspect
|
||||
puts values.inspect
|
||||
|
||||
res = 0
|
||||
operations = ['+', '*', '||']
|
||||
|
||||
results.each_with_index do |result, index|
|
||||
#puts "\nindex: #{index} | #{result} : #{values[index]}"
|
||||
|
||||
combinations = operations.repeated_permutation(values[index].length - 1).to_a
|
||||
|
||||
combinations.each do |comb|
|
||||
expr = values[index].zip(comb).flatten.compact.join
|
||||
#puts "#{expr} = #{eval_left_to_right(expr)}"
|
||||
|
||||
if eval_left_to_right(expr) == result
|
||||
#puts "#{result}"
|
||||
res += result
|
||||
break
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
puts res
|
||||
1
2024/day08/.envrc
Normal file
1
2024/day08/.envrc
Normal file
@@ -0,0 +1 @@
|
||||
use flake
|
||||
27
2024/day08/flake.lock
generated
Normal file
27
2024/day08/flake.lock
generated
Normal file
@@ -0,0 +1,27 @@
|
||||
{
|
||||
"nodes": {
|
||||
"nixpkgs": {
|
||||
"flake": false,
|
||||
"locked": {
|
||||
"lastModified": 1733384649,
|
||||
"narHash": "sha256-K5DJ2LpPqht7K76bsxetI+YHhGGRyVteTPRQaIIKJpw=",
|
||||
"owner": "NixOS",
|
||||
"repo": "nixpkgs",
|
||||
"rev": "190c31a89e5eec80dd6604d7f9e5af3802a58a13",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"id": "nixpkgs",
|
||||
"ref": "nixos-24.05",
|
||||
"type": "indirect"
|
||||
}
|
||||
},
|
||||
"root": {
|
||||
"inputs": {
|
||||
"nixpkgs": "nixpkgs"
|
||||
}
|
||||
}
|
||||
},
|
||||
"root": "root",
|
||||
"version": 7
|
||||
}
|
||||
29
2024/day08/flake.nix
Normal file
29
2024/day08/flake.nix
Normal file
@@ -0,0 +1,29 @@
|
||||
{
|
||||
description = "a minimal test development environment for ruby";
|
||||
|
||||
inputs = {
|
||||
|
||||
nixpkgs = {
|
||||
url = "nixpkgs/nixos-24.05";
|
||||
flake = false;
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
outputs = inputs@{ nixpkgs, ... }:
|
||||
{
|
||||
devShell = {
|
||||
x86_64-linux = let
|
||||
pkgs = import nixpkgs { system = "x86_64-linux"; };
|
||||
in pkgs.mkShell {
|
||||
packages = with pkgs; [
|
||||
gcc ruby
|
||||
];
|
||||
shellHook = ''
|
||||
export CC=gcc
|
||||
echo -e "\x1B[0;33mentering ruby minimalist test development environment...\x1B[0m"
|
||||
'';
|
||||
};
|
||||
};
|
||||
};
|
||||
}
|
||||
47
2024/day08/part1.rb
Normal file
47
2024/day08/part1.rb
Normal file
@@ -0,0 +1,47 @@
|
||||
require 'set'
|
||||
|
||||
if File.exists?("input.txt")
|
||||
file = File.open("input.txt")
|
||||
file_data = file.read
|
||||
file.close
|
||||
else
|
||||
file_data = "............\n........0...\n.....0......\n.......0....\n....0.......\n......A.....\n............\n............\n........A...\n.........A..\n............\n............"
|
||||
end
|
||||
|
||||
antennas = []
|
||||
map = file_data.split("\n")
|
||||
map.each_with_index do |row, r|
|
||||
row.chars.each_with_index do |char, c|
|
||||
antennas << [c, r, char] if char != '.'
|
||||
end
|
||||
end
|
||||
|
||||
antinodes = Set.new
|
||||
|
||||
antennas.each_with_index do |(x1, y1, value1), i|
|
||||
antennas.each_with_index do |(x2, y2, value2), j|
|
||||
next if i == j
|
||||
next unless value1 == value2
|
||||
|
||||
x_dir = x2 - x1
|
||||
y_dir = y2 - y1
|
||||
|
||||
[1, -1].each do |dir|
|
||||
if dir > 0
|
||||
x_point = x1 - x_dir * dir
|
||||
else
|
||||
x_point = x2 - x_dir * dir
|
||||
end
|
||||
|
||||
if dir > 0
|
||||
y_point = y1 - y_dir * dir
|
||||
else
|
||||
y_point = y2 - y_dir * dir
|
||||
end
|
||||
|
||||
antinodes.add([x_point, y_point]) if (x_point.between?(0, map.size - 1) && y_point.between?(0, map[0].size - 1))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
puts antinodes.size
|
||||
47
2024/day08/part2.rb
Normal file
47
2024/day08/part2.rb
Normal file
@@ -0,0 +1,47 @@
|
||||
require 'set'
|
||||
|
||||
if File.exists?("input.txt")
|
||||
file = File.open("input.txt")
|
||||
file_data = file.read
|
||||
file.close
|
||||
else
|
||||
file_data = "............\n........0...\n.....0......\n.......0....\n....0.......\n......A.....\n............\n............\n........A...\n.........A..\n............\n............"
|
||||
end
|
||||
|
||||
antennas = []
|
||||
map = file_data.split("\n")
|
||||
map.each_with_index do |row, r|
|
||||
row.chars.each_with_index do |char, c|
|
||||
antennas << [c, r, char] if char != '.'
|
||||
end
|
||||
end
|
||||
|
||||
antinodes = Set.new
|
||||
|
||||
antennas.each_with_index do |(x1, y1, value1), i|
|
||||
antennas.each_with_index do |(x2, y2, value2), j|
|
||||
next if i == j
|
||||
next unless value1 == value2
|
||||
|
||||
x_dir = x2 - x1
|
||||
y_dir = y2 - y1
|
||||
|
||||
[1, -1].each do |dir|
|
||||
x_point = x1
|
||||
y_point = y1
|
||||
|
||||
loop do
|
||||
x_point = x_point - x_dir * dir
|
||||
y_point = y_point - y_dir * dir
|
||||
|
||||
if (!x_point.between?(0, map.size - 1) || !y_point.between?(0, map[0].size - 1))
|
||||
break
|
||||
end
|
||||
|
||||
antinodes.add([x_point, y_point])
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
puts antinodes.size
|
||||
1
2024/day09/.envrc
Normal file
1
2024/day09/.envrc
Normal file
@@ -0,0 +1 @@
|
||||
use flake
|
||||
27
2024/day09/flake.lock
generated
Normal file
27
2024/day09/flake.lock
generated
Normal 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
28
2024/day09/flake.nix
Normal 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
39
2024/day09/part1.py
Normal 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
56
2024/day09/part2.py
Normal 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()
|
||||
1
2024/day10/.envrc
Normal file
1
2024/day10/.envrc
Normal file
@@ -0,0 +1 @@
|
||||
use flake
|
||||
27
2024/day10/flake.lock
generated
Normal file
27
2024/day10/flake.lock
generated
Normal file
@@ -0,0 +1,27 @@
|
||||
{
|
||||
"nodes": {
|
||||
"nixpkgs": {
|
||||
"flake": false,
|
||||
"locked": {
|
||||
"lastModified": 1733730953,
|
||||
"narHash": "sha256-dlK7n82FEyZlHH7BFHQAM5tua+lQO1Iv7aAtglc1O5s=",
|
||||
"owner": "NixOS",
|
||||
"repo": "nixpkgs",
|
||||
"rev": "7109b680d161993918b0a126f38bc39763e5a709",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"id": "nixpkgs",
|
||||
"ref": "nixos-24.05",
|
||||
"type": "indirect"
|
||||
}
|
||||
},
|
||||
"root": {
|
||||
"inputs": {
|
||||
"nixpkgs": "nixpkgs"
|
||||
}
|
||||
}
|
||||
},
|
||||
"root": "root",
|
||||
"version": 7
|
||||
}
|
||||
29
2024/day10/flake.nix
Normal file
29
2024/day10/flake.nix
Normal file
@@ -0,0 +1,29 @@
|
||||
{
|
||||
description = "a minimal test development environment for ruby";
|
||||
|
||||
inputs = {
|
||||
|
||||
nixpkgs = {
|
||||
url = "nixpkgs/nixos-24.05";
|
||||
flake = false;
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
outputs = inputs@{ nixpkgs, ... }:
|
||||
{
|
||||
devShell = {
|
||||
x86_64-linux = let
|
||||
pkgs = import nixpkgs { system = "x86_64-linux"; };
|
||||
in pkgs.mkShell {
|
||||
packages = with pkgs; [
|
||||
gcc ruby
|
||||
];
|
||||
shellHook = ''
|
||||
export CC=gcc
|
||||
echo -e "\x1B[0;33mentering ruby minimalist test development environment...\x1B[0m"
|
||||
'';
|
||||
};
|
||||
};
|
||||
};
|
||||
}
|
||||
52
2024/day10/part1.rb
Normal file
52
2024/day10/part1.rb
Normal file
@@ -0,0 +1,52 @@
|
||||
require 'set'
|
||||
|
||||
def find_trailheads(grid)
|
||||
trailheads = []
|
||||
grid.each_with_index do |row, y|
|
||||
row.each_char.with_index do |height, x|
|
||||
trailheads << [x, y] if height == '0'
|
||||
end
|
||||
end
|
||||
trailheads
|
||||
end
|
||||
|
||||
def find_path(grid, x, y, visited)
|
||||
score = 0
|
||||
|
||||
visited.add([x, y])
|
||||
|
||||
return 1 if grid[y][x] == '9'
|
||||
|
||||
[[-1, 0], [1, 0], [0, -1], [0, 1]].each do |x_dir, y_dir|
|
||||
nx, ny = x + x_dir, y + y_dir
|
||||
next unless ny.between?(0, grid.size - 1) && nx.between?(0, grid[0].length - 1)
|
||||
next if visited.include?([nx, ny])
|
||||
|
||||
if grid[ny][nx].to_i == grid[y][x].to_i + 1
|
||||
score += find_path(grid, nx, ny, visited)
|
||||
end
|
||||
end
|
||||
score
|
||||
end
|
||||
|
||||
|
||||
|
||||
if File.exists?("input.txt")
|
||||
file = File.open("input.txt")
|
||||
file_data = file.read
|
||||
file.close
|
||||
else
|
||||
file_data = "89010123\n78121874\n87430965\n96549874\n45678903\n32019012\n01329801\n10456732"
|
||||
end
|
||||
|
||||
grid = file_data.split("\n")
|
||||
|
||||
trailheads = find_trailheads(grid)
|
||||
result = 0
|
||||
|
||||
trailheads.each do |trailhead|
|
||||
result += find_path(grid, trailhead[0], trailhead[1], Set.new)
|
||||
#puts result
|
||||
end
|
||||
|
||||
puts "result : #{result}"
|
||||
54
2024/day10/part2.rb
Normal file
54
2024/day10/part2.rb
Normal file
@@ -0,0 +1,54 @@
|
||||
require 'set'
|
||||
|
||||
def find_trailheads(grid)
|
||||
trailheads = []
|
||||
grid.each_with_index do |row, y|
|
||||
row.each_char.with_index do |height, x|
|
||||
trailheads << [x, y] if height == '0'
|
||||
end
|
||||
end
|
||||
trailheads
|
||||
end
|
||||
|
||||
def find_path(grid, x, y, visited)
|
||||
score = 0
|
||||
|
||||
return 1 if grid[y][x] == '9'
|
||||
|
||||
visited.add([x, y])
|
||||
|
||||
[[-1, 0], [1, 0], [0, -1], [0, 1]].each do |x_dir, y_dir|
|
||||
nx, ny = x + x_dir, y + y_dir
|
||||
next unless ny.between?(0, grid.size - 1) && nx.between?(0, grid[0].length - 1)
|
||||
next if visited.include?([nx, ny])
|
||||
|
||||
if grid[ny][nx].to_i == grid[y][x].to_i + 1
|
||||
score += find_path(grid, nx, ny, visited)
|
||||
end
|
||||
end
|
||||
|
||||
visited.delete([x, y])
|
||||
score
|
||||
end
|
||||
|
||||
|
||||
|
||||
if File.exists?("input.txt")
|
||||
file = File.open("input.txt")
|
||||
file_data = file.read
|
||||
file.close
|
||||
else
|
||||
file_data = "89010123\n78121874\n87430965\n96549874\n45678903\n32019012\n01329801\n10456732"
|
||||
end
|
||||
|
||||
grid = file_data.split("\n")
|
||||
|
||||
trailheads = find_trailheads(grid)
|
||||
result = 0
|
||||
|
||||
trailheads.each do |trailhead|
|
||||
result += find_path(grid, trailhead[0], trailhead[1], Set.new)
|
||||
#puts result
|
||||
end
|
||||
|
||||
puts "result : #{result}"
|
||||
Reference in New Issue
Block a user