This commit is contained in:
Dan Ballard 2020-12-06 11:11:09 -08:00
parent dd87be81ed
commit efd5484204
4 changed files with 1071 additions and 7 deletions

View File

@ -7,3 +7,4 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
regex = "1"

1000
inputs/02-01.txt Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,11 +1,13 @@
use std::fs;
use std::fs::File; use std::fs::File;
use std::io::{prelude::*, BufReader, Error}; use std::io::{prelude::*, BufReader};
use std::error::Error;
use regex::Regex;
pub fn aoc_01_01(filename: &str) -> Result<(), Error> {
pub fn aoc_01_both(filename: &str) -> Result<(), Box<dyn Error>> {
let file = File::open(filename).expect("No file"); let file = File::open(filename).expect("No file");
let br = BufReader::new(file); let br = BufReader::new(file);
let mut v: Vec<i64> = vec![]; let mut v: Vec<i64> = vec![];
@ -35,5 +37,54 @@ pub fn aoc_01_01(filename: &str) -> Result<(), Error> {
} }
return Ok(()); return Ok(());
}
pub fn aoc_02(filename: &str) -> Result<(), Box<dyn Error>> {
let contents = fs::read_to_string(filename)?;
let pairs: Vec<Vec<String>> = contents.lines().map(|line| {
line.split(":").map(|s| String::from(s)).collect()
}).collect();
aoc_02_01(&pairs).unwrap();
aoc_02_02(&pairs).unwrap();
Ok(())
}
fn aoc_02_01(pairs: &Vec<Vec<String>>) -> Result<(), Box<dyn Error>> {
let mut valid = 0;
let rules_re = Regex::new(r"(\d*)-(\d*) ([a-z])").unwrap();
for i in 0..pairs.len() {
let rule = rules_re.captures(&pairs[i][0]).unwrap();
let min = rule[1].parse::<usize>().unwrap();
let max = rule[2].parse::<usize>().unwrap();
let matches: usize = pairs[i][1].matches(&rule[3]).count();
if matches >= min && matches <= max {
valid += 1;
}
}
println!("2_1 had {}/{} valid passwords", valid, pairs.len());
return Ok(());
}
fn aoc_02_02(pairs: &Vec<Vec<String>>) -> Result<(), Box<dyn Error>> {
let mut valid = 0;
let rules_re = Regex::new(r"(\d*)-(\d*) ([a-z])").unwrap();
for i in 0..pairs.len() {
let rule = rules_re.captures(&pairs[i][0]).unwrap();
let first = rule[1].parse::<usize>().unwrap();
let sec = rule[2].parse::<usize>().unwrap();
let m1 = pairs[i][1].as_bytes()[first] == rule[3].as_bytes()[0];
let m2 = pairs[i][1].as_bytes()[sec] == rule[3].as_bytes()[0];
if (m1 || m2) && !(m1 && m2) {
valid += 1;
}
}
println!("2_2 had {}/{} valid passwords", valid, pairs.len());
return Ok(());
} }

View File

@ -1,8 +1,20 @@
use std::env;
fn main() { fn main() {
//let filename = env::args().nth(1).expect("input filename required"); let day = match env::args().nth(1) {
Some(arg) => arg.parse().unwrap(),
None => 1
};
let result = match day {
1 => advent_of_code::aoc_01_both("./inputs/01-01.txt"),
2 => advent_of_code::aoc_02("./inputs/02-01.txt"),
_ => Err(format!("unsupported day {}", day).into()),
};
match result {
Ok(_) => (),
Err(e) => println!("Error executing day {}: {}", day, e),
}
advent_of_code::aoc_01_01("./inputs/01-01.txt");
} }