From 44396ade0adbb061dc948f1eb7be84149f6ab230 Mon Sep 17 00:00:00 2001 From: Jonni Liljamo Date: Wed, 3 Dec 2025 13:12:35 +0200 Subject: [PATCH] feat: day3 part one --- Cargo.lock | 7 +++++ Cargo.toml | 2 +- crates/day3/Cargo.toml | 7 +++++ crates/day3/src/main.rs | 65 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 crates/day3/Cargo.toml create mode 100644 crates/day3/src/main.rs diff --git a/Cargo.lock b/Cargo.lock index f6578a0..db74037 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -23,6 +23,13 @@ dependencies = [ "common", ] +[[package]] +name = "day3" +version = "0.1.0" +dependencies = [ + "common", +] + [[package]] name = "sc" version = "0.2.7" diff --git a/Cargo.toml b/Cargo.toml index f2246d7..3bba91c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,3 +1,3 @@ [workspace] resolver = "3" -members = ["crates/common", "crates/day1", "crates/day2"] +members = ["crates/common", "crates/day1", "crates/day2", "crates/day3"] diff --git a/crates/day3/Cargo.toml b/crates/day3/Cargo.toml new file mode 100644 index 0000000..22def1f --- /dev/null +++ b/crates/day3/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "day3" +version = "0.1.0" +edition = "2024" + +[dependencies] +common = { path = "../common" } diff --git a/crates/day3/src/main.rs b/crates/day3/src/main.rs new file mode 100644 index 0000000..d3bbe94 --- /dev/null +++ b/crates/day3/src/main.rs @@ -0,0 +1,65 @@ +#![no_std] + +use core::fmt::Write; + +use common::{print, read}; + +fn main() { + let buf = [0u8; 65000]; + read(buf.as_ptr(), buf.len()); + let mut input = str::from_utf8(&buf).unwrap(); + input = input.trim_end_matches(char::MIN); + input = input.trim(); + let result_one = one(input); + let result_two = two(input); + print!(64, "one: {}\ntwo: {}\n", result_one, result_two); +} + +fn one(input: &str) -> usize { + input + .split("\n") + .map(|bank| { + let mut first = char::MIN; + let mut first_index = 0; + let mut second = char::MIN; + let mut second_index = 0; + + for (i, c) in bank.chars().enumerate() { + if c > first && i != bank.len() - 1 { + first = c; + first_index = i; + + second = char::MIN; + second_index = 0; + } else if c > second && i > first_index { + second = c; + second_index = i; + } + } + + let a = &bank[first_index..first_index + 1]; + let b = &bank[second_index..second_index + 1]; + + let mut buf = [0; 2]; + let c = common::format_str(&mut buf, format_args!("{}{}", a, b)); + + c.parse::().unwrap() + }) + .sum::() +} + +#[cfg(test)] +mod test { + use super::*; + + const EXAMPLE_INPUT: &str = r#"987654321111111 +811111111111119 +234234234234278 +818181911112111"#; + + #[test] + fn example() { + assert_eq!(one(EXAMPLE_INPUT), 357); + assert_eq!(two(EXAMPLE_INPUT), 3121910778619); + } +} -- 2.44.1