Day 2
This commit is contained in:
45
src/day2/mod.rs
Normal file
45
src/day2/mod.rs
Normal file
@@ -0,0 +1,45 @@
|
||||
mod id_range;
|
||||
|
||||
use id_range::IDRange;
|
||||
use std::{collections::HashSet, fs};
|
||||
|
||||
fn parse_range(string: &str) -> IDRange {
|
||||
let mut split = string.split("-");
|
||||
let lhs = split
|
||||
.nth(0)
|
||||
.unwrap()
|
||||
.parse::<u64>()
|
||||
.expect("lhs not a digit");
|
||||
|
||||
let rhs = split
|
||||
.nth(0)
|
||||
.unwrap()
|
||||
.parse::<u64>()
|
||||
.expect("rhs not a digit");
|
||||
|
||||
IDRange::new(lhs, rhs)
|
||||
}
|
||||
|
||||
pub fn solve_part1() {
|
||||
let solution = solve(Some(2));
|
||||
println!("The solution for part 1 is {}", solution);
|
||||
}
|
||||
|
||||
pub fn solve_part2() {
|
||||
let solution = solve(None);
|
||||
println!("The solution for part 2 is {}", solution);
|
||||
}
|
||||
|
||||
fn solve(limit: Option<usize>) -> u64 {
|
||||
let file = fs::read_to_string("src/day2/input.txt").expect("You didn't provide input.txt.");
|
||||
|
||||
let mut invalid_ids: HashSet<u64> = HashSet::new();
|
||||
for line in file.split(",") {
|
||||
let range = parse_range(line);
|
||||
for invalid in range.find_invalid_ids(limit) {
|
||||
invalid_ids.insert(invalid);
|
||||
}
|
||||
}
|
||||
|
||||
invalid_ids.iter().sum::<u64>()
|
||||
}
|
||||
Reference in New Issue
Block a user