Completed day 2
This commit is contained in:
parent
447940ad7e
commit
b2379b06a3
14
day02/Cargo.lock
generated
Normal file
14
day02/Cargo.lock
generated
Normal file
@ -0,0 +1,14 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
[[package]]
|
||||
name = "anyhow"
|
||||
version = "1.0.34"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bf8dcb5b4bbaa28653b647d8c77bd4ed40183b48882e130c1f1ffb73de069fd7"
|
||||
|
||||
[[package]]
|
||||
name = "day02"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
]
|
10
day02/Cargo.toml
Normal file
10
day02/Cargo.toml
Normal file
@ -0,0 +1,10 @@
|
||||
[package]
|
||||
name = "day02"
|
||||
version = "0.1.0"
|
||||
authors = ["Tyler Hallada <tyler@hallada.net>"]
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
anyhow= "1.0"
|
1000
day02/input/input.txt
Executable file
1000
day02/input/input.txt
Executable file
File diff suppressed because it is too large
Load Diff
3
day02/input/test.txt
Normal file
3
day02/input/test.txt
Normal file
@ -0,0 +1,3 @@
|
||||
1-3 a: abcde
|
||||
1-3 b: cdefg
|
||||
2-9 c: ccccccccc
|
171
day02/src/main.rs
Normal file
171
day02/src/main.rs
Normal file
@ -0,0 +1,171 @@
|
||||
use anyhow::{anyhow, Error, Result};
|
||||
|
||||
use std::fs::File;
|
||||
use std::io::prelude::*;
|
||||
use std::io::BufReader;
|
||||
use std::ops::Range;
|
||||
use std::str::FromStr;
|
||||
|
||||
const INPUT: &str = "input/input.txt";
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
struct PasswordRule {
|
||||
letter: char,
|
||||
range: Range<usize>,
|
||||
}
|
||||
|
||||
impl FromStr for PasswordRule {
|
||||
type Err = Error;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self> {
|
||||
let mut parts = s.split(" ");
|
||||
let mut range = parts
|
||||
.next()
|
||||
.ok_or_else(|| anyhow!("Failed to parse PasswordRule range"))?
|
||||
.split("-");
|
||||
let start = range
|
||||
.next()
|
||||
.ok_or_else(|| anyhow!("Failed to parse PasswordRule range start"))?
|
||||
.parse()?;
|
||||
let end: usize = range
|
||||
.next()
|
||||
.ok_or_else(|| anyhow!("Failed to parse PasswordRule range end"))?
|
||||
.parse()?;
|
||||
let letter = parts
|
||||
.next()
|
||||
.ok_or_else(|| anyhow!("Failed to parse PasswordRule letter"))?
|
||||
.parse()?;
|
||||
Ok(Self {
|
||||
letter,
|
||||
range: Range {
|
||||
start,
|
||||
end: end + 1,
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
struct PasswordEntry {
|
||||
rule: PasswordRule,
|
||||
password: String,
|
||||
}
|
||||
|
||||
impl FromStr for PasswordEntry {
|
||||
type Err = Error;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self> {
|
||||
let mut parts = s.split(": ");
|
||||
let rule = parts
|
||||
.next()
|
||||
.ok_or_else(|| anyhow!("Failed to parse PasswordRule range"))?
|
||||
.parse()?;
|
||||
let password = parts
|
||||
.next()
|
||||
.ok_or_else(|| anyhow!("Failed to parse PasswordRule range start"))?
|
||||
.to_owned();
|
||||
Ok(Self { rule, password })
|
||||
}
|
||||
}
|
||||
|
||||
impl PasswordEntry {
|
||||
fn validate_occurences(&self) -> bool {
|
||||
self.rule
|
||||
.range
|
||||
.contains(&self.password.chars().fold(0, |acc, c| match c {
|
||||
_ if c == self.rule.letter => acc + 1,
|
||||
_ => acc,
|
||||
}))
|
||||
}
|
||||
|
||||
fn validate_positions(&self) -> bool {
|
||||
let mut chars = self.password.chars();
|
||||
let left = chars.nth(self.rule.range.start - 1) == Some(self.rule.letter);
|
||||
let right =
|
||||
chars.nth(self.rule.range.end - self.rule.range.start - 2) == Some(self.rule.letter);
|
||||
left ^ right
|
||||
}
|
||||
}
|
||||
|
||||
fn solve_part1(input_path: &str) -> Result<usize> {
|
||||
let file = File::open(input_path)?;
|
||||
let reader = BufReader::new(file);
|
||||
|
||||
Ok(reader
|
||||
.lines()
|
||||
.map(|line| PasswordEntry::from_str(&line.unwrap()).unwrap())
|
||||
.filter(|entry| entry.validate_occurences())
|
||||
.count())
|
||||
}
|
||||
|
||||
fn solve_part2(input_path: &str) -> Result<usize> {
|
||||
let file = File::open(input_path)?;
|
||||
let reader = BufReader::new(file);
|
||||
Ok(reader
|
||||
.lines()
|
||||
.map(|line| PasswordEntry::from_str(&line.unwrap()).unwrap())
|
||||
.filter(|entry| entry.validate_positions())
|
||||
.count())
|
||||
}
|
||||
|
||||
fn main() {
|
||||
println!("Part 1: {}", solve_part1(INPUT).unwrap());
|
||||
println!("Part 2: {}", solve_part2(INPUT).unwrap());
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
const TEST_INPUT: &str = "input/test.txt";
|
||||
|
||||
#[test]
|
||||
fn parses_input() {
|
||||
let file = File::open(TEST_INPUT).unwrap();
|
||||
let reader = BufReader::new(file);
|
||||
let input: Vec<PasswordEntry> = reader
|
||||
.lines()
|
||||
.map(|line| PasswordEntry::from_str(&line.unwrap()).unwrap())
|
||||
.collect();
|
||||
assert_eq!(
|
||||
input[0],
|
||||
PasswordEntry {
|
||||
rule: PasswordRule {
|
||||
letter: 'a',
|
||||
range: Range { start: 1, end: 4 }
|
||||
},
|
||||
password: "abcde".to_string(),
|
||||
}
|
||||
);
|
||||
assert_eq!(
|
||||
input[1],
|
||||
PasswordEntry {
|
||||
rule: PasswordRule {
|
||||
letter: 'b',
|
||||
range: Range { start: 1, end: 4 }
|
||||
},
|
||||
password: "cdefg".to_string(),
|
||||
}
|
||||
);
|
||||
assert_eq!(
|
||||
input[2],
|
||||
PasswordEntry {
|
||||
rule: PasswordRule {
|
||||
letter: 'c',
|
||||
range: Range { start: 2, end: 10 }
|
||||
},
|
||||
password: "ccccccccc".to_string(),
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn solves_part1() {
|
||||
assert_eq!(solve_part1(TEST_INPUT).unwrap(), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn solves_part2() {
|
||||
assert_eq!(solve_part2(TEST_INPUT).unwrap(), 1);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user