Day 5 part 1
This commit is contained in:
parent
3f74dcb73e
commit
20ab475034
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -3,6 +3,7 @@ name = "advent-of-code-2018"
|
|||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
"chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
"lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"regex 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
"regex 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -6,4 +6,5 @@ edition = "2018"
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
chrono = "0.4"
|
chrono = "0.4"
|
||||||
|
lazy_static = "1.2.0"
|
||||||
regex = "1"
|
regex = "1"
|
||||||
|
1
inputs/5.txt
Normal file
1
inputs/5.txt
Normal file
File diff suppressed because one or more lines are too long
1
inputs/5_test.txt
Normal file
1
inputs/5_test.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
dabAcCaCBAcCcaDA
|
70
src/day5.rs
Normal file
70
src/day5.rs
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
extern crate regex;
|
||||||
|
|
||||||
|
use std::error::Error;
|
||||||
|
use std::fs::File;
|
||||||
|
use std::io::{BufRead, BufReader};
|
||||||
|
|
||||||
|
use regex::Regex;
|
||||||
|
|
||||||
|
const INPUT: &str = "inputs/5.txt";
|
||||||
|
|
||||||
|
pub fn solve_part1() -> Result<usize, Box<Error>> {
|
||||||
|
let polymer = read_polymer(INPUT)?;
|
||||||
|
Ok(reduce_polymer_completely(polymer).len())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn read_polymer(filename: &str) -> Result<String, Box<Error>> {
|
||||||
|
let file = File::open(filename)?;
|
||||||
|
let polymer = BufReader::new(file).lines().next().unwrap_or(Ok("".to_string()));
|
||||||
|
Ok(polymer?)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn reduce_polymer(polymer: &String) -> String {
|
||||||
|
lazy_static! {
|
||||||
|
static ref REACTING_UNITS: Regex = Regex::new(concat!(
|
||||||
|
r"aA|bB|cC|dD|eE|fF|gG|hH|iI|jJ|kK|lL|mM|nN|",
|
||||||
|
r"oO|pP|qQ|rR|sS|tT|uU|vV|wW|xX|yY|zZ|",
|
||||||
|
r"Aa|Bb|Cc|Dd|Ee|Ff|Gg|Hh|Ii|Jj|Kk|Ll|Mm|Nn|",
|
||||||
|
r"Oo|Pp|Qq|Rr|Ss|Tt|Uu|Vv|Ww|Xx|Yy|Zz")).unwrap();
|
||||||
|
}
|
||||||
|
REACTING_UNITS.replace_all(&polymer, "").to_string()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn reduce_polymer_completely(polymer: String) -> String {
|
||||||
|
let reduced = reduce_polymer(&polymer);
|
||||||
|
if reduced == polymer {
|
||||||
|
reduced
|
||||||
|
} else {
|
||||||
|
reduce_polymer_completely(reduced)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
const TEST_INPUT: &str = "inputs/5_test.txt";
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn reduces_polymer() {
|
||||||
|
assert_eq!(reduce_polymer(&"aA".to_string()), "");
|
||||||
|
assert_eq!(reduce_polymer(&"aAbB".to_string()), "");
|
||||||
|
assert_eq!(reduce_polymer(&"aAfgbB".to_string()), "fg");
|
||||||
|
assert_eq!(reduce_polymer(&"abAB".to_string()), "abAB");
|
||||||
|
assert_eq!(reduce_polymer(&"aabAAB".to_string()), "aabAAB");
|
||||||
|
assert_eq!(reduce_polymer(&"dabAcCaCBAcCcaDA".to_string()), "dabAaCBAcaDA");
|
||||||
|
assert_eq!(reduce_polymer(&"dabAaCBAcCcaDA".to_string()), "dabCBAcaDA");
|
||||||
|
assert_eq!(reduce_polymer(&"dabCBAcCcaDA".to_string()), "dabCBAcaDA");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn reduces_polymer_completely() {
|
||||||
|
assert_eq!(reduce_polymer_completely("dabAcCaCBAcCcaDA".to_string()), "dabCBAcaDA");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn reads_polymer() {
|
||||||
|
assert_eq!(read_polymer(TEST_INPUT).unwrap(), "dabAcCaCBAcCcaDA");
|
||||||
|
}
|
||||||
|
}
|
@ -1,7 +1,11 @@
|
|||||||
|
#[macro_use]
|
||||||
|
extern crate lazy_static;
|
||||||
|
|
||||||
mod day1;
|
mod day1;
|
||||||
mod day2;
|
mod day2;
|
||||||
mod day3;
|
mod day3;
|
||||||
mod day4;
|
mod day4;
|
||||||
|
mod day5;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
println!("Day 1:");
|
println!("Day 1:");
|
||||||
@ -16,4 +20,6 @@ fn main() {
|
|||||||
println!("Day 4:");
|
println!("Day 4:");
|
||||||
println!("{}", day4::solve_part1().unwrap());
|
println!("{}", day4::solve_part1().unwrap());
|
||||||
println!("{}", day4::solve_part2().unwrap());
|
println!("{}", day4::solve_part2().unwrap());
|
||||||
|
println!("Day 5:");
|
||||||
|
println!("{}", day5::solve_part1().unwrap());
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user