Added Day 7 Part 1

master
Ishan Jain 2 years ago
parent e55900f904
commit 9227bd9208
No known key found for this signature in database
GPG Key ID: F261A0E73038D89D
  1. 2794
      input
  2. 23
      small.input
  3. 112
      src/main.rs

2794
input

File diff suppressed because it is too large Load Diff

@ -1,15 +1,10 @@
abc
light red bags contain 1 bright white bag, 2 muted yellow bags.
dark orange bags contain 3 bright white bags, 4 muted yellow bags.
bright white bags contain 1 shiny gold bag.
muted yellow bags contain 2 shiny gold bags, 9 faded blue bags.
shiny gold bags contain 1 dark olive bag, 2 vibrant plum bags.
dark olive bags contain 3 faded blue bags, 4 dotted black bags.
vibrant plum bags contain 5 faded blue bags, 6 dotted black bags.
faded blue bags contain no other bags.
dotted black bags contain no other bags.
a
b
c
ab
ac
a
a
a
a
b

@ -1,32 +1,98 @@
use std::collections::HashMap;
use std::io::{stdin, Read};
type BagChildren = Vec<(String, usize)>;
#[derive(Debug)]
struct Bag {
color: String,
children: BagChildren,
}
fn main() {
let mut input = String::new();
stdin().read_to_string(&mut input).unwrap();
let questions = input
.split("\n\n")
let mut map: HashMap<String, BagChildren> = HashMap::new();
input
.split('\n')
.filter(|x| !x.is_empty())
.into_iter()
.map(|set| {
let groups: Vec<&str> = set.split('\n').filter(|x| !x.is_empty()).collect();
let mut q = vec![vec![false; groups.len()]; 26];
for (i, group) in groups.into_iter().enumerate() {
group.chars().for_each(|x| {
q[(x as u8 - 97) as usize][i] = true;
});
}
q.into_iter().filter(|x| !x.is_empty()).fold(0, |a, x| {
if x.into_iter().all(|y| y) {
a + 1
} else {
a
}
})
})
.sum::<i32>();
println!("questions = {:?}", questions);
.for_each(|record| {
let line = parse_line(record);
map.insert(line.color, line.children);
});
let mut ans = Vec::new();
for k in map.keys() {
if contains_bag(&map, "shiny gold", k) {
ans.push(k.clone());
}
}
println!("answer = {:?}", ans);
println!("ans = {}", ans.len());
}
pub fn contains_bag(dag: &HashMap<String, BagChildren>, pattern: &str, src: &str) -> bool {
let this = dag.get(src).unwrap();
this.iter()
.any(|(color, _)| color == pattern || contains_bag(dag, pattern, color))
}
fn parse_line(record: &str) -> Bag {
let mut res = record
.split(',')
.filter(|x| !x.is_empty())
.map(|x| x.trim_end_matches(|x| !char::is_alphanumeric(x)).trim())
.map(|x| x.replace("bags", "").replace("bag", ""));
let first = res.next().unwrap();
let mut first = first.split("contain").map(|x| x.trim());
let src = first.next().unwrap().to_owned();
if record.contains("contain no other bags") {
return Bag {
color: src,
children: vec![],
};
}
let mut children = vec![];
let (ccount, cname) = parse_child(&first.next().unwrap());
children.push((cname, ccount));
for r in res {
let (ccount, cname) = parse_child(&r);
children.push((cname, ccount));
}
Bag {
color: src,
children,
}
}
fn parse_child(a: &str) -> (usize, String) {
let t = a
.chars()
.filter(|x| char::is_alphabetic(*x) | char::is_ascii_whitespace(x))
.collect::<String>();
let s = a
.chars()
.filter(|x| char::is_numeric(*x))
.collect::<String>();
let s = s.trim().parse::<usize>().unwrap();
let t = t.trim().to_owned();
(s, t)
}

Loading…
Cancel
Save