Implement day 2.

Part two was weird.
This commit is contained in:
2018-12-02 10:36:45 +01:00
parent e4e4432810
commit fd5af9245b
3 changed files with 362 additions and 3 deletions

107
2018/src/day02.rs Normal file
View File

@@ -0,0 +1,107 @@
use std::collections::HashMap;
use std::io;
use std::io::prelude::*;
use common;
/// Count the occurrence characters in a string.
fn count_chars(word: &str) -> HashMap<char, u32> {
let mut counts: HashMap<char, u32> = HashMap::new();
for c in word.chars() {
*counts.entry(c).or_insert(0) += 1;
}
counts
}
/// Compute the number of different positions between two strings.
fn distance(a: &str, b: &str) -> usize {
let mut dist = 0;
for (a, b) in a.chars().zip(b.chars()) {
if a != b {
dist += 1;
}
}
dist
}
#[derive(Default)]
pub struct Day02 {
}
impl Day02 {
pub fn new() -> Day02 {
Default::default()
}
}
impl common::Solution for Day02 {
fn part1(&mut self, input: &mut io::Read) -> String {
let reader = io::BufReader::new(input);
let mut twos = 0;
let mut threes = 0;
for line in reader.lines() {
let counts = count_chars(&line.unwrap());
if counts.values().any(|&x| x == 2) {
twos += 1;
}
if counts.values().any(|&x| x == 3) {
threes += 1;
}
}
return format!("{}", twos * threes);
}
fn part2(&mut self, input: &mut io::Read) -> String {
let mut ids: Vec<String> = io::BufReader::new(input)
.lines()
.map(|x| x.unwrap()).collect();
ids.sort_unstable();
for id1 in &ids {
for id2 in &ids {
if id2 > id1 {
break;
}
if distance(id1, id2) == 1 {
let mut answer = String::new();
for (a, b) in id1.chars().zip(id2.chars()) {
if a == b {
answer.push(a);
}
}
return answer;
}
}
}
unreachable!("Input does not contain a valid solution.");
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_count_letters() {
let result = count_chars("abcaba");
assert_eq!(3, *result.get(&'a').unwrap());
assert_eq!(2, *result.get(&'b').unwrap());
assert_eq!(1, *result.get(&'c').unwrap())
}
#[test]
fn test_distance() {
assert_eq!(2, distance("abcde", "axcye"));
assert_eq!(1, distance("fghij", "fguij"));
}
}

View File

@@ -5,10 +5,12 @@ use std::io;
pub mod common;
pub mod day01;
pub mod day02;
fn get_impl(day: &str) -> Box<common::Solution> {
match day.parse() {
Ok(1) => { Box::new(day01::Day01::new()) }
Ok(1) => Box::new(day01::Day01::new()),
Ok(2) => Box::new(day02::Day02::new()),
Ok(val) => panic!("Unimplemented day {}", val),
_ => panic!("Invalid number"),
}
@@ -54,8 +56,8 @@ mod tests {
#[test]
fn test_get_impl() {
// Verify that we can load all days
let last_implemented = 1;
for d in 1..(last_implemented + 1) {
let last_implemented = 2;
for d in 1..=last_implemented {
get_impl(&format!("{}", d));
}
}