From e49727cbbb60d6f32f68be193869c2b85391fe67 Mon Sep 17 00:00:00 2001 From: Daniel Flanagan Date: Fri, 1 Dec 2023 02:47:18 -0600 Subject: [PATCH] Part 1 --- 2022/rust/.envrc | 1 + 2022/rust/.gitignore | 3 ++- 2022/rust/fetch_input.sh | 4 ++-- 2022/rust/flake.lock | 27 +++++++++++++++++++++++++ 2022/rust/flake.nix | 27 +++++++++++++++++++++++++ 2023/rust/src/day1.rs | 43 ++++++++++++++++++++++++++++++++++++---- 6 files changed, 98 insertions(+), 7 deletions(-) create mode 100644 2022/rust/.envrc create mode 100644 2022/rust/flake.lock create mode 100644 2022/rust/flake.nix diff --git a/2022/rust/.envrc b/2022/rust/.envrc new file mode 100644 index 0000000..3550a30 --- /dev/null +++ b/2022/rust/.envrc @@ -0,0 +1 @@ +use flake diff --git a/2022/rust/.gitignore b/2022/rust/.gitignore index eb5a316..6abfe1b 100644 --- a/2022/rust/.gitignore +++ b/2022/rust/.gitignore @@ -1 +1,2 @@ -target +/target +/.direnv diff --git a/2022/rust/fetch_input.sh b/2022/rust/fetch_input.sh index 46f79dd..b926186 100755 --- a/2022/rust/fetch_input.sh +++ b/2022/rust/fetch_input.sh @@ -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 \ No newline at end of file +fi diff --git a/2022/rust/flake.lock b/2022/rust/flake.lock new file mode 100644 index 0000000..c1065db --- /dev/null +++ b/2022/rust/flake.lock @@ -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 +} diff --git a/2022/rust/flake.nix b/2022/rust/flake.nix new file mode 100644 index 0000000..7455acc --- /dev/null +++ b/2022/rust/flake.nix @@ -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 + ]; + }; + }); + }; +} diff --git a/2023/rust/src/day1.rs b/2023/rust/src/day1.rs index b06a581..c5a32d0 100644 --- a/2023/rust/src/day1.rs +++ b/2023/rust/src/day1.rs @@ -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 = None; + let mut last_digit: Option = 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::::into(first_digit.or(last_digit).unwrap() * 10) + + Into::::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); } }