AOC2022/day03/src/main.rs

77 lines
2.3 KiB
Rust
Raw Permalink Normal View History

2022-12-04 16:17:34 +00:00
use std::fs;
fn main() {
const FILE_PATH: &str = "input.txt";
println!("Hi this is the third day of AOC2022, first we will read the file {}", FILE_PATH);
let contents = fs::read_to_string(FILE_PATH)
.expect("Should have been able to read the file");
println!("The sum of the priorities is: {}", &contents.lines().map(|l| get_priori(get_repeat(l))).sum::<u32>());
println!("The sum of the badges is: {}", get_groups(&contents).iter().map(|g| get_priori(grup_repeat(g))).sum::<u32>());
}
fn get_priori(c: char) -> u32{
let priori = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
priori.find(c).expect("must be a letre") as u32 + 1
}
fn get_groups(s: &str) -> Vec<Vec<&str>>{
let mut out = Vec::new();
let mut lines = s.lines();
loop {
let mut grup = Vec::new();
if let Some(line) = lines.next() {
grup.push(line);
grup.push(lines.next().expect("what"));
grup.push(lines.next().expect("Wharrr"))
}else{break;}
out.push(grup);
}
out
}
fn grup_repeat(grup: &Vec<&str>) -> char{
let mut repeated = Vec::new();
for c in grup[0].chars(){
if let Some(i) = grup[1].find(c){
repeated.push(grup[1].chars().nth(i).expect("should have failed before"));
}
};
for c in repeated {
if let Some(i) = grup[2].find(c){
2022-12-05 09:50:03 +00:00
return grup[2].chars().nth(i).expect("should have failed before");
2022-12-04 16:17:34 +00:00
}
};
2022-12-05 09:50:03 +00:00
panic!("Should not get here")
2022-12-04 16:17:34 +00:00
}
fn get_repeat(s: &str) -> char{
let slen = s.len();
let s1 = &s[0..slen/2];
let s2 = &s[slen/2..slen];
for c in s1.chars(){
if let Some(i) = s2.find(c){
return s2.chars().nth(i).expect("should have failed before");
}
};
2022-12-05 09:50:03 +00:00
panic!("Should not get here")
2022-12-04 16:17:34 +00:00
}
#[cfg(test)]
mod test {
2022-12-05 09:50:03 +00:00
const INPUT: &str =r#"vJrwpWtwJgWrhcsFMMfFFhFp
2022-12-04 16:17:34 +00:00
jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL
PmmdzqPrVvPwwTWBwg
wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn
ttgJtRGJQctTZtZT
CrZsJsPPZsGzwwsLwLmpwMDw"#;
2022-12-05 09:50:03 +00:00
use super::*;
#[test]
fn backpacks1() {
assert_eq!(INPUT.lines().map(|l| get_priori(get_repeat(l))).sum::<u32>(), 157);
2022-12-04 16:17:34 +00:00
}
#[test]
2022-12-05 09:50:03 +00:00
fn backpacks2() {
assert_eq!(get_groups(INPUT).iter().map(|g| get_priori(grup_repeat(g))).sum::<u32>(), 70);
2022-12-04 16:17:34 +00:00
}
}