Commit 22171fb8 authored by Kai Brandes's avatar Kai Brandes

solution for 4b

parent dabd19af
......@@ -9,4 +9,5 @@ fn main() {
println!("Solution for day 3a is: {}", adventofcode_2017::results::day_3_a());
println!("Solution for day 3b is: {}", adventofcode_2017::results::day_3_b());
println!("Solution for day 4a is: {}", adventofcode_2017::results::day_4_a());
println!("Solution for day 4b is: {}", adventofcode_2017::results::day_4_b());
}
\ No newline at end of file
This diff is collapsed.
use wasm_bindgen::prelude::*;
use std::iter::Iterator;
use std::iter::FromIterator;
fn line_valid(input: &str) -> bool {
let mut whitespace: Vec<&str> = input.split_whitespace().collect();
......@@ -17,6 +18,25 @@ pub fn calc_day_4_a(input: String) -> u32 {
.count() as u32;
}
fn to_normalized_line(input: &str) -> String {
let splitted = input.split_whitespace()
.map(|a: &str| {
let mut c: Vec<char> = a.chars().collect();
c.sort();
String::from_iter(c)
}).collect::<Vec<String>>();
splitted.join(" ")
}
pub fn calc_day_4_b(input: String) -> u32 {
return input.lines()
.map(to_normalized_line)
.map(|a|line_valid(&a))
.filter(|a| *a == true)
.count() as u32;
}
#[cfg(test)]
mod tests {
......@@ -43,5 +63,37 @@ mod tests {
}
#[test]
fn it_aoc_b_example1() {
assert_eq!(String::from("abcde fghij"), to_normalized_line("abcde fghij"));
}
#[test]
fn it_aoc_b_example2() {
assert_eq!(String::from("abcde xyz abcde"), to_normalized_line("abcde xyz ecdab"));
}
#[test]
fn it_aoc_b_example3() {
assert_eq!(1, calc_day_4_b(String::from("abcde fghij")));
}
#[test]
fn it_aoc_b_example4() {
assert_eq!(0, calc_day_4_b(String::from("abcde xyz ecdab")));
}
#[test]
fn it_aoc_b_example5() {
assert_eq!(1, calc_day_4_b(String::from("a ab abc abd abf abj")));
}
#[test]
fn it_aoc_b_example6() {
assert_eq!(1, calc_day_4_b(String::from("iiii oiii ooii oooi oooo")));
}
}
......@@ -41,3 +41,7 @@ pub fn day_3_b() -> u32 {
pub fn day_4_a() -> u32 {
return day4::calc_day_4_a(parse_input_from_file(String::from("src/day4/a.txt")));
}
pub fn day_4_b() -> u32 {
return day4::calc_day_4_b(parse_input_from_file(String::from("src/day4/b.txt")));
}
extern crate adventofcode_2017;
#[test]
fn it_shoul_assert_result_of_day4_a() {
assert_eq!(466, adventofcode_2017::results::day_4_a());
}
#[test]
fn it_shoul_assert_result_of_day4_b() {
assert_eq!(251, adventofcode_2017::results::day_4_b());
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment