advent-of-code/aoc2018/src/day02.rs

58 lines
955 B
Rust
Raw Normal View History

2019-11-14 18:19:23 +01:00
use std::collections::HashMap;
2019-11-15 00:14:07 +01:00
use super::Result;
2019-11-14 18:19:23 +01:00
2019-11-15 00:14:07 +01:00
const INPUT: &str = include_str!("../input/day02.txt");
2019-11-14 18:19:23 +01:00
2019-11-15 00:14:07 +01:00
pub fn run() -> Result<()> {
println!("part 1: {}", part1(INPUT)?);
2019-11-14 18:19:23 +01:00
Ok(())
}
2019-11-14 19:16:49 +01:00
fn part1(input: &str) -> Result<u32> {
2019-11-14 18:19:23 +01:00
let mut twice = 0;
let mut thrice = 0;
for line in input.lines() {
let mut seen: HashMap<char, u32> = HashMap::new();
for c in line.chars() {
*seen.entry(c).or_default() += 1;
}
if seen.values().any(|x| *x == 2) {
twice += 1;
}
if seen.values().any(|x| *x == 3) {
thrice += 1;
}
}
2019-11-14 19:16:49 +01:00
Ok(twice * thrice)
2019-11-14 18:19:23 +01:00
}
2019-11-14 19:32:36 +01:00
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn part1_provided() {
let input = "abcdef
bababc
abbcde
abcccd
aabcdd
abcdee
ababab
";
assert_eq!(part1(input).unwrap(), 12);
}
#[test]
fn part1_real() {
2019-11-15 00:14:07 +01:00
assert_eq!(part1(INPUT).unwrap(), 5750);
2019-11-14 19:32:36 +01:00
}
}