This commit is contained in:
Daniel Flanagan 2023-12-01 02:47:18 -06:00
parent fd676c4597
commit e49727cbbb
Signed by: lytedev
GPG key ID: 5B2020A0F9921EF4
6 changed files with 98 additions and 7 deletions

1
2022/rust/.envrc Normal file
View file

@ -0,0 +1 @@
use flake

View file

@ -1 +1,2 @@
target /target
/.direnv

View file

@ -1,6 +1,6 @@
#!/usr/bin/env sh #!/usr/bin/env sh
AOC_YEAR="${AOC_YEAR:-2022}" AOC_YEAR="${AOC_YEAR:-2023}"
if [ "$#" -lt 1 ]; then if [ "$#" -lt 1 ]; then
echo "Error: No day provided" echo "Error: No day provided"
exit 1 exit 1

27
2022/rust/flake.lock Normal file
View 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
View 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
];
};
});
};
}

View file

@ -7,13 +7,39 @@ fn main() {
} }
struct Day1 {} 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 { impl AoCSolution for Day1 {
type Input = String; type Input = String;
type Solution = i128; type Solution = i128;
fn part1(input: Self::Input) -> Self::Solution { fn part1(input: Self::Input) -> Self::Solution {
println!("{}", input); input.lines().map(Self::calibration_value).sum()
return 1;
} }
fn part2(_input: Self::Input) -> Self::Solution { fn part2(_input: Self::Input) -> Self::Solution {
@ -27,7 +53,16 @@ mod tests {
#[test] #[test]
fn test() { fn test() {
assert_eq!(Day1::part1("asdf".into()), 1); assert_eq!(
assert_eq!(Day1::part2("asdf".into()), 2); Day1::part1(
r#"1abc2
pqr3stu8vwx
a1b2c3d4e5f
treb7uchet"#
.into()
),
142
);
assert_eq!(Day1::part2("asdf".into()), 0);
} }
} }