「🎉」 init(2025): day 1
This commit is contained in:
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