Compare commits
2 Commits
7dde03f46f
...
879c2c411c
| Author | SHA1 | Date | |
|---|---|---|---|
| 879c2c411c | |||
| adb8cfd91a |
15
src/bin/d3p1.rs
Normal file
15
src/bin/d3p1.rs
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
use aoc22::day3;
|
||||||
|
use aoc22::util;
|
||||||
|
|
||||||
|
pub fn main() {
|
||||||
|
let rucksacks = day3::parse_rucksacks(&util::parse_input());
|
||||||
|
|
||||||
|
let mut sum = 0;
|
||||||
|
for (first, second) in rucksacks {
|
||||||
|
let common: Vec<_> = first.intersection(&second).collect();
|
||||||
|
assert!(common.len() == 1);
|
||||||
|
sum += day3::priority(**common.first().unwrap());
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("Sum of priorities is {}", sum);
|
||||||
|
}
|
||||||
17
src/bin/d3p2.rs
Normal file
17
src/bin/d3p2.rs
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
use aoc22::day3;
|
||||||
|
use aoc22::util;
|
||||||
|
|
||||||
|
pub fn main() {
|
||||||
|
let rucksacks = day3::parse_rucksacks(&util::parse_input());
|
||||||
|
|
||||||
|
let mut sum = 0;
|
||||||
|
for group in rucksacks.chunks_exact(3) {
|
||||||
|
// Intersection of the unions of each rucksack's compartments
|
||||||
|
let common = &(&(&group[0].0 | &group[0].1) & &(&group[1].0 | &group[1].1))
|
||||||
|
& &(&group[2].0 | &group[2].1);
|
||||||
|
assert!(common.len() == 1);
|
||||||
|
sum += day3::priority(*common.iter().next().unwrap());
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("Sum of priorities is {}", sum);
|
||||||
|
}
|
||||||
25
src/day3.rs
Normal file
25
src/day3.rs
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
use std::collections::HashSet;
|
||||||
|
|
||||||
|
pub fn parse_rucksacks(input: &String) -> Vec<(HashSet<char>, HashSet<char>)> {
|
||||||
|
let lines = input.lines();
|
||||||
|
let mut rucksacks = Vec::new();
|
||||||
|
|
||||||
|
for line in lines {
|
||||||
|
let length = line.len();
|
||||||
|
assert!(length % 2 == 0);
|
||||||
|
let (first, second) = line.split_at(length / 2);
|
||||||
|
rucksacks.push((first.chars().collect(), second.chars().collect()));
|
||||||
|
}
|
||||||
|
|
||||||
|
rucksacks
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn priority(c: char) -> u32 {
|
||||||
|
assert!(c.is_ascii_alphabetic());
|
||||||
|
let ord = c as u32;
|
||||||
|
if c.is_ascii_lowercase() {
|
||||||
|
1 + ord - 'a' as u32
|
||||||
|
} else {
|
||||||
|
27 + ord - 'A' as u32
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,3 +1,4 @@
|
|||||||
pub mod day1;
|
pub mod day1;
|
||||||
pub mod day2;
|
pub mod day2;
|
||||||
|
pub mod day3;
|
||||||
pub mod util;
|
pub mod util;
|
||||||
|
|||||||
Reference in New Issue
Block a user