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