Commit d734d878 authored by Kai Brandes's avatar Kai Brandes

add solution for 7a

parent 7e9d59cc
......@@ -6,6 +6,8 @@ authors = ["Kai Brandes <Kai Brandes>"]
[dependencies]
cfg-if = "0.1.2"
wasm-bindgen = "0.2"
regex = "1"
# The `console_error_panic_hook` crate provides better debugging of panics by
# logging them with `console.error`. This is great for development, but requires
# all the `std::fmt` and `std::panicking` infrastructure, so isn't great for
......
......@@ -14,4 +14,5 @@ fn main() {
println!("Solution for day 5b is: {}", adventofcode_2017::results::day_5_b());
println!("Solution for day 6a is: {}", adventofcode_2017::results::day_6_a());
println!("Solution for day 6b is: {}", adventofcode_2017::results::day_6_b());
println!("Solution for day 7a is: {}", adventofcode_2017::results::day_7_a());
}
\ No newline at end of file
This diff is collapsed.
use wasm_bindgen::prelude::*;
use std::collections::HashMap;
use std::collections::HashSet;
use regex::Regex;
const REGEX: &str = r"([a-z]+)\s\(([0-9]+)\)(\s->\s(.*))?";
struct Node {
name: String,
weight: u32,
blocks: Vec<String>,
is_blocked_by: Vec<String>,
}
fn parse_line(line: &str) -> (String, u32, Option<Vec<String>>) {
let regex: Regex = Regex::new(REGEX).unwrap();
let caps = regex.captures(line).unwrap();
let name = caps.get(1).map_or("", |m| m.as_str());
let weight = caps.get(2).map_or(0, |m| m.as_str().parse::<u32>().unwrap());
let deps: Option<Vec<String>> = caps.get(4)
.map_or(None, |m| Some(m
.as_str()
.split(", ")
.map(|a| a.to_string())
.collect()));
(name.to_string(), weight, deps)
}
fn calc_blocks(blocked_name: &String, all: &Vec<(String, u32, Option<Vec<String>>)>) -> Vec<String> {
all.iter()
.filter(|(name_ref, weight_ref, opt_deps)| name_ref != blocked_name
&& opt_deps.clone().map(|deps|deps.contains(blocked_name)).unwrap_or(false))
.map(|item| item.0.clone())
.collect::<Vec<String>>()
}
fn as_nodes(vec: &Vec<(String, u32, Option<Vec<String>>)>) -> Vec<Node> {
vec.iter()
.map(|(name_ref, weight_ref, deps)| Node {
name: name_ref.clone().to_string(),
weight: weight_ref.clone(),
blocks: deps.clone().unwrap_or(vec![]),
is_blocked_by: calc_blocks(name_ref, vec),
}).collect::<Vec<Node>>()
}
#[wasm_bindgen]
pub fn calc_day_7_a(input: String) -> String {
let parsed_lines = input.split("\n")
.map(parse_line)
.collect::<Vec<_>>();
let nodes: Vec<Node> = as_nodes(&parsed_lines);
nodes.iter()
.filter(|node| node.blocks.len() > 0 && node.is_blocked_by.len() == 0)
.map(|node| node.name.clone())
.nth(0)
.expect("No blocking node found!")
}
#[cfg(test)]
mod tests {
use super::*;
const EXAMPLE_INPUT: &str = "pbga (66)
xhth (57)
ebii (61)
havc (66)
ktlj (57)
fwft (72) -> ktlj, cntj, xhth
qoyq (66)
padx (45) -> pbga, havc, qoyq
tknk (41) -> ugml, padx, fwft
jptl (61)
ugml (68) -> gyxo, ebii, jptl
gyxo (61)\
cntj (57)";
#[test]
fn it_should_calc_a() {
assert_eq!("tknk", calc_day_7_a(EXAMPLE_INPUT.to_string()))
}
#[test]
fn it_parses_a_single_line_with_deps() {
let (name, weight, deps) = parse_line("tknk (41) -> ugml, padx, fwft");
assert_eq!("tknk".to_string(), name);
assert_eq!(41, weight);
assert_eq!(Some(vec!("ugml".to_string(), "padx".to_string(), "fwft".to_string())), deps);
}
#[test]
fn it_parses_a_single_line_without_deps() {
let (name, weight, deps) = parse_line("tknk (41)");
assert_eq!("tknk".to_string(), name);
assert_eq!(41, weight);
assert_eq!(None, deps);
}
#[test]
fn it_builds_dep_tree() {
let (name, weight, deps) = parse_line("tknk (41)");
assert_eq!("tknk".to_string(), name);
assert_eq!(41, weight);
assert_eq!(None, deps);
}
}
\ No newline at end of file
extern crate cfg_if;
extern crate wasm_bindgen;
extern crate regex;
pub mod day1;
pub mod day2;
......@@ -7,6 +8,7 @@ pub mod day3;
pub mod day4;
pub mod day5;
pub mod day6;
pub mod day7;
pub mod results;
mod utils;
......
......@@ -4,6 +4,7 @@ use day3;
use day4;
use day5;
use day6;
use day7;
use std::fs::File;
use std::io::prelude::*;
......@@ -63,3 +64,7 @@ pub fn day_6_a() -> u32 {
pub fn day_6_b() -> u32 {
return day6::calc_day_6_b(parse_input_from_file(String::from("src/day6/b.txt")));
}
pub fn day_7_a() -> String {
return day7::calc_day_7_a(parse_input_from_file(String::from("src/day7/a.txt")));
}
extern crate adventofcode_2017;
#[test]
fn it_shoul_assert_result_of_day7_a() {
assert_eq!("rqwgj".to_string(), adventofcode_2017::results::day_7_a());
}
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