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