2019-11-14 18:19:23 +01:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
2019-12-01 22:22:12 +01:00
|
|
|
use aoc::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-15 15:18:36 +01:00
|
|
|
println!("part 2: {}", part2(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
|
|
|
|
2019-11-15 15:18:36 +01:00
|
|
|
fn part2(input: &str) -> Result<String> {
|
|
|
|
let lines = input.lines().collect::<Vec<&str>>();
|
|
|
|
|
|
|
|
for i in 0..lines.len() {
|
|
|
|
for j in (i + 1)..lines.len() {
|
|
|
|
let different = lines[i]
|
|
|
|
.chars()
|
|
|
|
.zip(lines[j].chars())
|
|
|
|
.filter(|tuple| tuple.0 != tuple.1)
|
|
|
|
.count();
|
|
|
|
|
|
|
|
if different == 1 {
|
|
|
|
return Ok(common_letters(lines[i], lines[j]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok("".into())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn common_letters(a: &str, b: &str) -> String {
|
|
|
|
a.chars()
|
|
|
|
.zip(b.chars())
|
|
|
|
.filter_map(|e| match e {
|
|
|
|
(a, b) if a == b => Some(a),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2019-11-15 15:18:36 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn part2_provided() {
|
|
|
|
let input = "abcde
|
|
|
|
fghij
|
|
|
|
klmno
|
|
|
|
pqrst
|
|
|
|
fguij
|
|
|
|
axcye
|
|
|
|
wvxyz
|
|
|
|
";
|
|
|
|
|
|
|
|
assert_eq!(part2(input).unwrap(), "fgij");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn part2_real() {
|
|
|
|
assert_eq!(part2(INPUT).unwrap(), "tzyvunogzariwkpcbdewmjhxi");
|
|
|
|
}
|
2019-11-14 19:32:36 +01:00
|
|
|
}
|