comments and formating
This commit is contained in:
parent
efd5484204
commit
474680a2d7
|
@ -4,9 +4,6 @@ use std::io::{prelude::*, BufReader};
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
pub fn aoc_01_both(filename: &str) -> Result<(), Box<dyn 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);
|
||||||
|
@ -41,8 +38,6 @@ pub fn aoc_01_both(filename: &str) -> Result<(), Box<dyn Error>> {
|
||||||
|
|
||||||
pub fn aoc_02(filename: &str) -> Result<(), Box<dyn Error>> {
|
pub fn aoc_02(filename: &str) -> Result<(), Box<dyn Error>> {
|
||||||
let contents = fs::read_to_string(filename)?;
|
let contents = fs::read_to_string(filename)?;
|
||||||
|
|
||||||
|
|
||||||
let pairs: Vec<Vec<String>> = contents.lines().map(|line| {
|
let pairs: Vec<Vec<String>> = contents.lines().map(|line| {
|
||||||
line.split(":").map(|s| String::from(s)).collect()
|
line.split(":").map(|s| String::from(s)).collect()
|
||||||
}).collect();
|
}).collect();
|
||||||
|
@ -77,12 +72,12 @@ fn aoc_02_02(pairs: &Vec<Vec<String>>) -> Result<(), Box<dyn Error>> {
|
||||||
let first = rule[1].parse::<usize>().unwrap();
|
let first = rule[1].parse::<usize>().unwrap();
|
||||||
let sec = rule[2].parse::<usize>().unwrap();
|
let sec = rule[2].parse::<usize>().unwrap();
|
||||||
|
|
||||||
|
// the indexes are 1 based but the pairs[x][2] has a leading space so it works out... -_-;
|
||||||
let m1 = pairs[i][1].as_bytes()[first] == rule[3].as_bytes()[0];
|
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];
|
let m2 = pairs[i][1].as_bytes()[sec] == rule[3].as_bytes()[0];
|
||||||
if (m1 || m2) && !(m1 && m2) {
|
if (m1 || m2) && !(m1 && m2) {
|
||||||
valid += 1;
|
valid += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
println!("2_2 had {}/{} valid passwords", valid, pairs.len());
|
println!("2_2 had {}/{} valid passwords", valid, pairs.len());
|
||||||
|
|
Loading…
Reference in New Issue