Part 1
This commit is contained in:
parent
fd676c4597
commit
e49727cbbb
1
2022/rust/.envrc
Normal file
1
2022/rust/.envrc
Normal file
|
@ -0,0 +1 @@
|
|||
use flake
|
3
2022/rust/.gitignore
vendored
3
2022/rust/.gitignore
vendored
|
@ -1 +1,2 @@
|
|||
target
|
||||
/target
|
||||
/.direnv
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
#!/usr/bin/env sh
|
||||
|
||||
AOC_YEAR="${AOC_YEAR:-2022}"
|
||||
AOC_YEAR="${AOC_YEAR:-2023}"
|
||||
if [ "$#" -lt 1 ]; then
|
||||
echo "Error: No day provided"
|
||||
exit 1
|
||||
|
@ -22,4 +22,4 @@ else
|
|||
echo "Error: curl failed"
|
||||
rm -f "$f"
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
|
|
27
2022/rust/flake.lock
Normal file
27
2022/rust/flake.lock
Normal file
|
@ -0,0 +1,27 @@
|
|||
{
|
||||
"nodes": {
|
||||
"nixpkgs": {
|
||||
"locked": {
|
||||
"lastModified": 1700390070,
|
||||
"narHash": "sha256-de9KYi8rSJpqvBfNwscWdalIJXPo8NjdIZcEJum1mH0=",
|
||||
"owner": "NixOS",
|
||||
"repo": "nixpkgs",
|
||||
"rev": "e4ad989506ec7d71f7302cc3067abd82730a4beb",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "NixOS",
|
||||
"repo": "nixpkgs",
|
||||
"rev": "e4ad989506ec7d71f7302cc3067abd82730a4beb",
|
||||
"type": "github"
|
||||
}
|
||||
},
|
||||
"root": {
|
||||
"inputs": {
|
||||
"nixpkgs": "nixpkgs"
|
||||
}
|
||||
}
|
||||
},
|
||||
"root": "root",
|
||||
"version": 7
|
||||
}
|
27
2022/rust/flake.nix
Normal file
27
2022/rust/flake.nix
Normal file
|
@ -0,0 +1,27 @@
|
|||
{
|
||||
inputs.nixpkgs.url = "github:NixOS/nixpkgs?rev=e4ad989506ec7d71f7302cc3067abd82730a4beb";
|
||||
outputs = {
|
||||
self,
|
||||
nixpkgs,
|
||||
}: let
|
||||
supportedSystems = ["x86_64-linux" "aarch64-linux" "x86_64-darwin" "aarch64-darwin"];
|
||||
forEachSupportedSystem = f:
|
||||
nixpkgs.lib.genAttrs supportedSystems (system:
|
||||
f {
|
||||
pkgs = import nixpkgs {inherit system;};
|
||||
});
|
||||
in {
|
||||
devShells = forEachSupportedSystem ({pkgs}: {
|
||||
default = pkgs.mkShell {
|
||||
buildInputs = with pkgs; [
|
||||
cargo
|
||||
rustc
|
||||
rustfmt
|
||||
rustPackages.clippy
|
||||
rust-analyzer
|
||||
curl
|
||||
];
|
||||
};
|
||||
});
|
||||
};
|
||||
}
|
|
@ -7,13 +7,39 @@ fn main() {
|
|||
}
|
||||
|
||||
struct Day1 {}
|
||||
impl Day1 {
|
||||
fn calibration_value(line: &str) -> i128 {
|
||||
println!("{line}");
|
||||
let bytes = line.as_bytes();
|
||||
let mut first_digit: Option<u8> = None;
|
||||
let mut last_digit: Option<u8> = None;
|
||||
for i in 0..bytes.len() {
|
||||
let n = bytes.len() - 1 - i;
|
||||
println!("{n} {i}");
|
||||
if first_digit.is_none() && (0x30..=0x39).contains(&bytes[i]) {
|
||||
first_digit = Some(bytes[i] - 0x30);
|
||||
println!("found first {first_digit:?}");
|
||||
}
|
||||
if last_digit.is_none() && (0x30..=0x39).contains(&bytes[n]) {
|
||||
last_digit = Some(bytes[n] - 0x30);
|
||||
println!("found last {last_digit:?}");
|
||||
}
|
||||
if first_digit.is_some() && last_digit.is_some() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
println!("{:?} {:?}", first_digit, last_digit);
|
||||
Into::<i128>::into(first_digit.or(last_digit).unwrap() * 10)
|
||||
+ Into::<i128>::into(last_digit.or(first_digit).unwrap())
|
||||
}
|
||||
}
|
||||
|
||||
impl AoCSolution for Day1 {
|
||||
type Input = String;
|
||||
type Solution = i128;
|
||||
|
||||
fn part1(input: Self::Input) -> Self::Solution {
|
||||
println!("{}", input);
|
||||
return 1;
|
||||
input.lines().map(Self::calibration_value).sum()
|
||||
}
|
||||
|
||||
fn part2(_input: Self::Input) -> Self::Solution {
|
||||
|
@ -27,7 +53,16 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn test() {
|
||||
assert_eq!(Day1::part1("asdf".into()), 1);
|
||||
assert_eq!(Day1::part2("asdf".into()), 2);
|
||||
assert_eq!(
|
||||
Day1::part1(
|
||||
r#"1abc2
|
||||
pqr3stu8vwx
|
||||
a1b2c3d4e5f
|
||||
treb7uchet"#
|
||||
.into()
|
||||
),
|
||||
142
|
||||
);
|
||||
assert_eq!(Day1::part2("asdf".into()), 0);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue