solve year 2022 day 03 part 1
This commit is contained in:
@@ -14,4 +14,5 @@ pub mod year2016 {
|
||||
pub mod year2022 {
|
||||
pub mod day01;
|
||||
pub mod day02;
|
||||
}
|
||||
pub mod day03;
|
||||
}
|
||||
|
@@ -58,7 +58,7 @@ fn main() {
|
||||
} in solutions
|
||||
{
|
||||
let start = Instant::now();
|
||||
let (aw1, el1, aw2, el2) = (run)(input);
|
||||
let (aw1, el1, aw2, el2) = (run)(input);
|
||||
let elapsed = start.elapsed();
|
||||
overall_duration += elapsed;
|
||||
println!("{year} Day {day:02} ({elapsed:?})");
|
||||
@@ -84,5 +84,6 @@ fn year2022() -> Vec<Solution> {
|
||||
vec![
|
||||
solution!(year2022, day01),
|
||||
solution!(year2022, day02),
|
||||
solution!(year2022, day03),
|
||||
]
|
||||
}
|
||||
}
|
||||
|
39
src/year2022/day03.rs
Normal file
39
src/year2022/day03.rs
Normal file
@@ -0,0 +1,39 @@
|
||||
use std::collections::BTreeSet;
|
||||
|
||||
fn char_to_score(c: char) -> u32 {
|
||||
match c {
|
||||
'a'..='z' => c as u32 - 96,
|
||||
'A'..='Z' => c as u32 - 38,
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn part1(input: &str) -> impl std::fmt::Display {
|
||||
input
|
||||
.lines()
|
||||
.map(|l| l.split_at(l.len() / 2))
|
||||
.map(|(l, r)| {
|
||||
let a: BTreeSet<char> = l.chars().collect();
|
||||
let b: BTreeSet<char> = r.chars().collect();
|
||||
a.intersection(&b)
|
||||
.next()
|
||||
.expect("We know there is at least one")
|
||||
.clone()
|
||||
})
|
||||
.map(char_to_score)
|
||||
.sum::<u32>()
|
||||
}
|
||||
|
||||
pub fn part2(input: &str) -> impl std::fmt::Display {
|
||||
""
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_part1() {
|
||||
assert_eq!("15", part1("A Y\nB X\nC Z").to_string())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_part2() {
|
||||
assert_eq!("12", part2("A Y\nB X\nC Z").to_string())
|
||||
}
|
Reference in New Issue
Block a user