mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-25 12:50:32 +01:00
Implementation day 2.
This commit is contained in:
@@ -6,3 +6,4 @@ edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
clap = "3.0.0-beta.2"
|
||||
regex = "1"
|
||||
|
||||
1000
2020/inputs/02.txt
Normal file
1000
2020/inputs/02.txt
Normal file
File diff suppressed because it is too large
Load Diff
3
2020/samples/02.txt
Normal file
3
2020/samples/02.txt
Normal file
@@ -0,0 +1,3 @@
|
||||
1-3 a: abcde
|
||||
1-3 b: cdefg
|
||||
2-9 c: ccccccccc
|
||||
101
2020/src/day02.rs
Normal file
101
2020/src/day02.rs
Normal file
@@ -0,0 +1,101 @@
|
||||
use std::io::BufRead;
|
||||
use std::io::BufReader;
|
||||
use std::io::Read;
|
||||
|
||||
use regex::Regex;
|
||||
|
||||
use crate::Solution;
|
||||
|
||||
struct Rule {
|
||||
c: char,
|
||||
min: usize,
|
||||
max: usize,
|
||||
sample: String,
|
||||
}
|
||||
|
||||
impl Rule {
|
||||
fn matches1(&self) -> bool {
|
||||
let occurrences = self.sample.matches(self.c).count();
|
||||
|
||||
occurrences >= self.min && occurrences <= self.max
|
||||
}
|
||||
|
||||
fn matches2(&self) -> bool {
|
||||
let c = self.c as u8;
|
||||
let s = self.sample.as_bytes();
|
||||
|
||||
(s[self.min - 1] == c) ^ (s[self.max - 1] == c)
|
||||
}
|
||||
}
|
||||
|
||||
fn read_rules(input: &mut dyn Read) -> Vec<Rule> {
|
||||
let parser = Regex::new(r"^(\d+)-(\d+) ([a-z]): ([a-z]+)$").unwrap();
|
||||
|
||||
let mut reader = BufReader::new(input);
|
||||
let mut buffer = String::new();
|
||||
|
||||
let mut rules = Vec::new();
|
||||
|
||||
while let Ok(read) = reader.read_line(&mut buffer) {
|
||||
if read == 0 {
|
||||
break;
|
||||
}
|
||||
|
||||
let cap = parser.captures(buffer.trim()).unwrap();
|
||||
|
||||
rules.push(Rule {
|
||||
c: cap[3].chars().next().unwrap(),
|
||||
min: cap[1].parse().unwrap(),
|
||||
max: cap[2].parse().unwrap(),
|
||||
sample: cap[4].to_owned(),
|
||||
});
|
||||
|
||||
buffer.clear();
|
||||
}
|
||||
|
||||
rules
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Day02;
|
||||
|
||||
impl Solution for Day02 {
|
||||
fn part1(&mut self, input: &mut dyn Read) -> String {
|
||||
read_rules(input)
|
||||
.into_iter()
|
||||
.filter(Rule::matches1)
|
||||
.count()
|
||||
.to_string()
|
||||
}
|
||||
|
||||
fn part2(&mut self, input: &mut dyn Read) -> String {
|
||||
read_rules(input)
|
||||
.into_iter()
|
||||
.filter(Rule::matches2)
|
||||
.count()
|
||||
.to_string()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
const SAMPLE: &[u8] = include_bytes!("../samples/02.txt");
|
||||
|
||||
#[test]
|
||||
fn sample_part1() {
|
||||
let mut implementation = Day02;
|
||||
|
||||
let result = implementation.part1(&mut SAMPLE.as_ref());
|
||||
assert_eq!("2", &result);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sample_part2() {
|
||||
let mut implementation = Day02;
|
||||
|
||||
let result = implementation.part2(&mut SAMPLE.as_ref());
|
||||
assert_eq!("1", &result);
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@ use std::io::Read;
|
||||
|
||||
mod common;
|
||||
mod day01;
|
||||
mod day02;
|
||||
|
||||
pub trait Solution {
|
||||
fn part1(&mut self, input: &mut dyn Read) -> String;
|
||||
@@ -14,6 +15,7 @@ pub trait Solution {
|
||||
pub fn get_implementation(day: usize) -> Box<dyn Solution> {
|
||||
match day {
|
||||
1 => Box::new(day01::Day01::default()),
|
||||
2 => Box::new(day02::Day02::default()),
|
||||
_ => panic!("Unsupported day {}", day),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user