M Cargo.lock => Cargo.lock +7 -0
@@ 24,6 24,13 @@ dependencies = [
]
[[package]]
+name = "day3"
+version = "0.1.0"
+dependencies = [
+ "common",
+]
+
+[[package]]
name = "sc"
version = "0.2.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
M Cargo.toml => Cargo.toml +1 -1
@@ 1,3 1,3 @@
[workspace]
resolver = "3"
-members = ["crates/common", "crates/day1", "crates/day2"]
+members = ["crates/common", "crates/day1", "crates/day2", "crates/day3"]
A crates/day3/Cargo.toml => crates/day3/Cargo.toml +7 -0
@@ 0,0 1,7 @@
+[package]
+name = "day3"
+version = "0.1.0"
+edition = "2024"
+
+[dependencies]
+common = { path = "../common" }
A crates/day3/src/main.rs => crates/day3/src/main.rs +65 -0
@@ 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::<usize>().unwrap()
+ })
+ .sum::<usize>()
+}
+
+#[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);
+ }
+}