From eb2dabb284f754277ab72aa418ef778646f91dcd Mon Sep 17 00:00:00 2001 From: alyx Date: Fri, 1 Dec 2023 00:27:39 -0500 Subject: 2023, 2023.1 --- 2023/rs/code/one/src/main.rs | 47 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 2023/rs/code/one/src/main.rs (limited to '2023/rs/code/one/src/main.rs') diff --git a/2023/rs/code/one/src/main.rs b/2023/rs/code/one/src/main.rs new file mode 100644 index 0000000..1b04f08 --- /dev/null +++ b/2023/rs/code/one/src/main.rs @@ -0,0 +1,47 @@ +static INPUT: &[u8] = include_bytes!("input.txt"); + +static NUMS: &[&[u8]] = &[ b"one", b"two", b"three", b"four", b"five", b"six", b"seven", b"eight", b"nine" ]; + +fn main() { + let valueified = INPUT.split(|c| *c == b'\n').filter(|s| !s.is_empty()); + + let calvals = valueified.clone().map(|s| { + let mut d1 = 0; + let mut d2 = 0; + for c in s { + if *c >= b'0' && *c <= b'9' { + if d1 == 0 { d1 = *c; } + d2 = *c; + } + } + if d1 != 0 { ((d1 - b'0') * 10 + (d2 - b'0')) as u32 } else { 0 } + }); + + let sum = calvals.sum::(); + println!("Sum of Calibration Values: {sum}"); + + let calvals = valueified.map(|s| { + let mut d1 = 0; + let mut d2 = 0; + for i in 0..s.len() { + let c = s[i]; + if c >= b'0' && c <= b'9' { + if d1 == 0 { d1 = c; } + d2 = c; + } + else { + for i2 in 0..NUMS.len() { + let num = NUMS[i2]; + if s.len() >= i + num.len() && &s[i..i+num.len()] == num { + if d1 == 0 { d1 = i2 as u8 + b'1'; } + d2 = i2 as u8 + b'1'; + } + } + } + } + if d1 != 0 { ((d1 - b'0') * 10 + (d2 - b'0')) as u32 } else { 0 } + }); + + let sum = calvals.sum::(); + println!("Sum of (word-included) Calibration Values: {sum}"); +} -- cgit v1.2.3-54-g00ecf