readying 2022 to merge

This commit is contained in:
Dylan Thies
2023-12-12 08:58:38 -05:00
parent 6eaaa4630e
commit 3b75d20516
64 changed files with 0 additions and 0 deletions

90
2022/day3/src/main.rs Normal file
View File

@@ -0,0 +1,90 @@
#![warn(clippy::all, clippy::pedantic)]
use std::fs;
fn part1(input: &str) -> String {
input
.lines()
.map(|line| {
let (comp1, comp2) = line.split_at(line.len() / 2);
let duplicate = comp2.chars().find(|c| comp1.contains(*c)).unwrap();
match duplicate {
n @ 'a'..='z' => (n as i32) - ('a' as i32) + 1_i32,
n @ 'A'..='Z' => (n as i32) - ('A' as i32) + 27_i32,
_ => 0,
}
})
.sum::<i32>()
.to_string()
}
fn part2(input: &str) -> String {
input
.lines()
.fold(Vec::new(), |mut acc: Vec<Vec<String>>, line| {
if acc.is_empty() || acc.last().unwrap().len() == 3 {
acc.push(Vec::new());
}
acc.last_mut().unwrap().push(line.to_owned());
acc
})
.iter()
.map(|group| {
let [g1, g2, g3] = group.as_slice() else {
panic!("not get here")
};
match g1
.chars()
.fold(Vec::new(), |mut combo: Vec<char>, ch| {
if g2.contains(ch) {
combo.push(ch);
}
combo
})
.iter()
.find(|c| g3.contains(**c))
.unwrap()
{
n @ 'a'..='z' => (*n as i32) - ('a' as i32) + 1_i32,
n @ 'A'..='Z' => (*n as i32) - ('A' as i32) + 27_i32,
_ => 0,
}
})
.sum::<i32>()
.to_string()
}
fn main() -> std::io::Result<()> {
//Read in file
let file = fs::read_to_string("input")?;
// fold the lines into groups of three
println!("Part 1: {}", part1(&file));
println!("Part 2: {}", part2(&file));
// find common letter in the groups
// find common letters in the first 2 then find the common in the third
// sum the common letters
Ok(())
}
#[cfg(test)]
mod test {
use super::*;
const INPUT: &str = "vJrwpWtwJgWrhcsFMMfFFhFp
jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL
PmmdzqPrVvPwwTWBwg
wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn
ttgJtRGJQctTZtZT
CrZsJsPPZsGzwwsLwLmpwMDw";
#[test]
fn part1_works() {
assert_eq!(part1(&INPUT), "157");
}
#[test]
fn part2_works() {
assert_eq!(part2(&INPUT), "70");
}
}