2019: day04: part 1

This commit is contained in:
Antoine Martin 2019-12-04 23:18:01 +01:00
parent c2297ba849
commit 69000f9254
4 changed files with 83 additions and 1 deletions

1
aoc2019/input/day04.txt Normal file
View file

@ -0,0 +1 @@
153517-630395

79
aoc2019/src/day04.rs Normal file
View file

@ -0,0 +1,79 @@
use aoc::err;
use aoc::Result;
const INPUT: &str = include_str!("../input/day04.txt");
pub fn run() -> Result<()> {
println!("part 1: {}", part1(INPUT)?);
Ok(())
}
fn part1(input: &str) -> Result<usize> {
let mut range = input.trim_end().split('-');
let begin: usize = range
.next()
.ok_or_else(|| err!("invalid input: {}", input))?
.parse()?;
let end: usize = range
.next()
.ok_or_else(|| err!("invalid input: {}", input))?
.parse()?;
let res = (begin..=end)
.map(|n| DigitsIter::new(n).collect::<Vec<_>>())
.filter(|digits| digits.windows(2).any(|window| window[0] == window[1]))
.filter(|digits| digits.windows(2).all(|window| window[0] <= window[1]))
.count();
Ok(res)
}
struct DigitsIter {
n: usize,
div: usize,
}
impl DigitsIter {
fn new(n: usize) -> Self {
let mut div = 1;
while n >= div * 10 {
div *= 10;
}
DigitsIter { n, div }
}
}
impl Iterator for DigitsIter {
type Item = usize;
fn next(&mut self) -> Option<Self::Item> {
match self.div {
0 => None,
_ => {
let res = self.n / self.div;
self.n %= self.div;
self.div /= 10;
Some(res)
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn part1_provided() {
assert_eq!(part1("111111-111111").unwrap(), 1);
assert_eq!(part1("223450-223450").unwrap(), 0);
assert_eq!(part1("123789-123789").unwrap(), 0);
}
#[test]
fn part1_real() {
assert_eq!(part1(INPUT).unwrap(), 1729);
}
}

View file

@ -1,3 +1,4 @@
pub mod day01;
pub mod day02;
pub mod day03;
pub mod day04;

View file

@ -3,9 +3,10 @@ use aoc::Result;
use aoc2019::day01;
use aoc2019::day02;
use aoc2019::day03;
use aoc2019::day04;
fn main() -> Result<()> {
let days: &[fn() -> Result<()>] = &[day01::run, day02::run, day03::run];
let days: &[fn() -> Result<()>] = &[day01::run, day02::run, day03::run, day04::run];
aoc::run(days)
}