From fb2dd0f45fa8f8a1045ee9c76c1bca3cc71cb0ef Mon Sep 17 00:00:00 2001 From: j-james Date: Sun, 4 Dec 2022 23:15:53 -0800 Subject: Day Three --- 2022/rust/day03/src/main.rs | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 2022/rust/day03/src/main.rs (limited to '2022/rust/day03/src') diff --git a/2022/rust/day03/src/main.rs b/2022/rust/day03/src/main.rs new file mode 100644 index 0000000..db5b83e --- /dev/null +++ b/2022/rust/day03/src/main.rs @@ -0,0 +1,43 @@ +#![feature(iter_array_chunks)] +use std::env; +use std::fs; + +fn main() { + let args = env::args().nth(1).expect("missing input"); + let input = fs::read_to_string(args).unwrap(); + let rucksacks = input.trim().split("\n"); + + let compartments: Vec<(&str, &str)> = rucksacks.clone() + .map(|x| x.split_at(x.len() / 2)).collect(); + let mut sum = 0; + for (a, b) in compartments.iter() { + for c in a.chars() { + if b.contains(c) { + sum += deascii(c); + break; + } + } + // println!("{:?}, {:?}", a, b); + } + println!("{}", sum); + + sum = 0; + // yoo this is sick + for a in rucksacks.array_chunks::<3>() { + for c in a[0].chars() { + if a[1].contains(c) && a[2].contains(c) { + sum += deascii(c); + break; + } + } + } + println!("{}", sum); +} + +fn deascii(x: char) -> i64 { + if x as i64 > 96 { + return x as i64 - 96; + } else { + return x as i64 - 38; + } +} -- cgit v1.2.3-70-g09d2