day5 part 1
This commit is contained in:
1170
src/day5/input.txt
Normal file
1170
src/day5/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
1
src/day5/mod.rs
Normal file
1
src/day5/mod.rs
Normal file
@@ -0,0 +1 @@
|
||||
pub mod part1;
|
||||
34
src/day5/part1.rs
Normal file
34
src/day5/part1.rs
Normal file
@@ -0,0 +1,34 @@
|
||||
use std::fs;
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn solve() {
|
||||
let file = fs::read_to_string("src/day5/input.txt").expect("You didn't provide input.txt.");
|
||||
|
||||
let mut reading_ranges = true;
|
||||
let mut fresh_products = vec![];
|
||||
let mut ranges = vec![];
|
||||
for line in file.lines() {
|
||||
if line.is_empty() {
|
||||
reading_ranges = false;
|
||||
continue;
|
||||
}
|
||||
|
||||
if reading_ranges {
|
||||
let mut parts = line.split("-");
|
||||
let start = parts.next().unwrap().parse::<usize>().unwrap();
|
||||
let end = parts.next().unwrap().parse::<usize>().unwrap();
|
||||
let range = start..=end;
|
||||
ranges.push(range);
|
||||
} else {
|
||||
let value = line.parse::<usize>().unwrap();
|
||||
for range in &ranges {
|
||||
if range.contains(&value) {
|
||||
fresh_products.push(value);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
println!("Fresh products: {:?}", fresh_products.len());
|
||||
}
|
||||
11
src/day5/sample_input.txt
Normal file
11
src/day5/sample_input.txt
Normal file
@@ -0,0 +1,11 @@
|
||||
3-5
|
||||
10-14
|
||||
16-20
|
||||
12-18
|
||||
|
||||
1
|
||||
5
|
||||
8
|
||||
11
|
||||
17
|
||||
32
|
||||
@@ -2,7 +2,8 @@ mod day1;
|
||||
mod day2;
|
||||
mod day3;
|
||||
mod day4;
|
||||
mod day5;
|
||||
|
||||
fn main() {
|
||||
day4::part2::solve();
|
||||
day5::solve();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user