Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Feature/day 7 #16

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open

WIP: Feature/day 7 #16

wants to merge 8 commits into from

Conversation

jimgerth
Copy link
Owner

@jimgerth jimgerth commented Jan 1, 2021

No description provided.

}
}

println!("{:#?}", contained_in_rules);
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove debug print. (Can be added in #11)


let containers = get_containers("shiny gold", &contained_in_rules);

// println!("{:#?}", containers);
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above.


// println!("{:#?}", containers);

containers.len() as i32
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider inlining containers.

total
}

fn parse_rules(input: Vec<&str>) -> HashMap<&str, HashMap<&str, i32>> {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move passe_rules above the other two helper functions.

get_content_count("shiny gold", &rules)
}

fn get_containers<'a>(
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a function get_containers_count to wrap the len() call on the HashSet.

}

fn get_content_count(color: &str, rules: &HashMap<&str, HashMap<&str, i32>>) -> i32 {
let rule = rules.get(color).expect("Color not listed in rules.");
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Default to 0 without panicking!

let bag_rules_regex = Regex::new(r"bags contain (.*).").unwrap();
let bag_rule_regex = Regex::new(r"(\d+) (\w+ \w+) \w+").unwrap();

// Parse rules.
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove comment or reword to be more helpful.

Bags contain different types of other bags. Especially when wanting to
determine how many different types of bags a bag can contian, a set is
more suitable for the problem, as duplicates are meaningless and even
harmful in this situation.

Signed-off-by: Jim Gerth <[email protected]>
Signed-off-by: Jim Gerth <[email protected]>
Both part 1 and part 2 used this code, thus refactored into its own function.

Signed-off-by: Jim Gerth <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant