Commit 28fff7fa authored by Kai Brandes's avatar Kai Brandes

added solutions for 4 to wasm

parent 1da06267
use wasm_bindgen::prelude::*;
use std::iter::Iterator;
use std::iter::FromIterator;
use wasm_bindgen::prelude::*;
fn line_valid(input: &str) -> bool {
let mut whitespace: Vec<&str> = input.split_whitespace().collect();
let total_words = whitespace.len() as u32;
......@@ -10,8 +11,8 @@ fn line_valid(input: &str) -> bool {
total_words == whitespace.len() as u32
}
pub fn calc_day_4_a(input: &String) -> u32 {
#[wasm_bindgen]
pub fn calc_day_4_a(input: String) -> u32 {
return input.lines()
.map(line_valid)
.filter(|a| *a == true)
......@@ -28,8 +29,8 @@ fn to_normalized_line(input: &str) -> String {
splitted.join(" ")
}
pub fn calc_day_4_b(input: &String) -> u32 {
#[wasm_bindgen]
pub fn calc_day_4_b(input: String) -> u32 {
return input.lines()
.map(to_normalized_line)
.map(|a|line_valid(&a))
......@@ -59,7 +60,7 @@ mod tests {
#[test]
fn it_aoc_example_4() {
assert_eq!(2, calc_day_4_a(&String::from("aa bb cc dd ee\naa bb cc dd aa\naa bb cc dd aaa")));
assert_eq!(2, calc_day_4_a(String::from("aa bb cc dd ee\naa bb cc dd aa\naa bb cc dd aaa")));
}
......@@ -75,22 +76,22 @@ mod tests {
#[test]
fn it_aoc_b_example3() {
assert_eq!(1, calc_day_4_b(&String::from("abcde fghij")));
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")));
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")));
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")));
assert_eq!(1, calc_day_4_b(String::from("iiii oiii ooii oooi oooo")));
}
......
......@@ -39,9 +39,9 @@ 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")));
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")));
return day4::calc_day_4_b(parse_input_from_file(String::from("src/day4/b.txt")));
}
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