「🎉」 init(2025): day 1
This commit is contained in:
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
|
||||
Reference in New Issue
Block a user