1
0
Fork 0

Compare commits

...

2 Commits

Author SHA1 Message Date
Andrew Coleman 84842992d0 day 4 solution 2022-12-05 10:10:12 -05:00
Andrew Coleman 408a690061 cargo fmt 2022-12-05 09:59:07 -05:00
6 changed files with 1060 additions and 5 deletions

1000
2022/day4.txt Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
use anyhow::Result;
use std::fs;
use std::collections::HashSet;
use std::fs;
fn get_priority(t: &char) -> u32 {
if t.is_ascii_uppercase() {
@ -47,14 +47,20 @@ pub fn run() -> Result<()> {
let elf2map: HashSet<char> = HashSet::from_iter(elf2.chars());
let elf3map: HashSet<char> = HashSet::from_iter(elf3.chars());
for dupe in elf1map.iter().filter(|t| elf2map.contains(t) && elf3map.contains(t)) {
for dupe in elf1map
.iter()
.filter(|t| elf2map.contains(t) && elf3map.contains(t))
{
rucksacks2.push(get_priority(dupe));
}
} else {
break;
}
}
println!("part two priorities {}", rucksacks2.into_iter().sum::<u32>());
println!(
"part two priorities {}",
rucksacks2.into_iter().sum::<u32>()
);
Ok(())
}

39
2022/src/days/day4.rs Normal file
View File

@ -0,0 +1,39 @@
use anyhow::Result;
use std::fs;
pub fn run() -> Result<()> {
let file_contents = fs::read_to_string("day4.txt")?;
let lines = file_contents.lines();
let mut part_one_pairs = 0;
let mut part_two_pairs = 0;
for line in lines {
let v: Vec<&str> = line.split(",").collect();
let left: Vec<u32> = v[0].split("-").map(|t| t.parse::<u32>().unwrap()).collect();
let right: Vec<u32> = v[1].split("-").map(|t| t.parse::<u32>().unwrap()).collect();
let left_outside = left[0] <= right[0] && left[1] >= right[1];
let left_inside = left[0] >= right[0] && left[1] <= right[1];
let left_covered = left_outside || left_inside;
let right_outside = right[0] <= left[0] && right[1] >= left[1];
let right_inside = right[0] >= left[0] && right[1] <= left[1];
let right_covered = right_outside || right_inside;
if left_covered && right_covered {
part_one_pairs += 1;
}
let left_1_inside = left[0] >= right[0] && left[0] <= right[1];
let left_2_inside = left[1] >= right[0] && left[1] <= right[1];
let right_1_inside = right[0] >= left[0] && right[0] <= left[1];
let right_2_inside = right[1] >= left[0] && right[1] <= left[1];
if left_1_inside || left_2_inside || right_1_inside || right_2_inside {
part_two_pairs += 1;
}
}
println!("part one pairs {}", part_one_pairs);
println!("part two pairs {}", part_two_pairs);
Ok(())
}

View File

@ -1,3 +1,4 @@
pub mod day1;
pub mod day2;
pub mod day3;
pub mod day4;

View File

@ -14,10 +14,13 @@ fn run_day(number: i32) -> Result<()> {
}
2 => {
days::day2::run()?;
},
}
3 => {
days::day3::run()?;
},
}
4 => {
days::day4::run()?;
}
_ => return Err(anyhow!("Invalid day provided")),
}
Ok(())

6
2022/tests/day4.txt Normal file
View File

@ -0,0 +1,6 @@
2-4,6-8
2-3,4-5
5-7,7-9
2-8,3-7
6-6,4-6
2-6,4-8