parent
8c72063cd4
commit
f94c6c48cc
@ -0,0 +1,24 @@
|
||||
CARGO_FEATURE_FLAGS?=
|
||||
|
||||
RUSTFLAGS?=-C target-cpu=native
|
||||
|
||||
.PHONY: all
|
||||
.NOTPARALLEL: all
|
||||
all:
|
||||
$(MAKE) part1
|
||||
$(MAKE) part2
|
||||
|
||||
.NOTPARALLEL: part1
|
||||
part1: $(wildcard src/*.rs)
|
||||
RUSTFLAGS="$(RUSTFLAGS)" cargo build --release $(addprefix --features ,$(CARGO_FEATURE_FLAGS))
|
||||
mv -f target/release/day7 $@
|
||||
strip $@
|
||||
|
||||
.NOTPARALLEL: part2
|
||||
part2: $(wildcard src/*.rs)
|
||||
RUSTFLAGS="$(RUSTFLAGS)" cargo build --release --features $@ $(addprefix --features ,$(CARGO_FEATURE_FLAGS))
|
||||
mv -f target/release/day7 $@
|
||||
strip $@
|
||||
|
||||
clean:
|
||||
rm -f part{1,2}
|
@ -0,0 +1,53 @@
|
||||
#![feature(str_split_once)]
|
||||
|
||||
#![allow(dead_code)]
|
||||
|
||||
use std::{
|
||||
io::BufReader,
|
||||
fs::OpenOptions,
|
||||
collections::HashSet,
|
||||
};
|
||||
|
||||
#[cfg(feature="test")]
|
||||
const INPUT: &str ="input-test";
|
||||
#[cfg(not(feature="test"))]
|
||||
const INPUT: &str = "input";
|
||||
|
||||
const NEEDLE: &str = "shiny gold";
|
||||
|
||||
mod parse;
|
||||
mod bag;
|
||||
|
||||
fn cnt_rules(rule: &bag::Rule, set: &HashSet<bag::Rule>) -> usize
|
||||
{
|
||||
match rule.children() {
|
||||
&[] => 0,
|
||||
children => {
|
||||
children.iter().map(|(n, b)| *n + (*n * cnt_rules(set.get(b).unwrap(), set))).sum::<usize>()
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let parsed: HashSet<_> = parse::parse(BufReader::new(OpenOptions::new().read(true).open(INPUT)?)).collect();
|
||||
|
||||
#[cfg(not(feature="part2"))]
|
||||
{
|
||||
let mut found=0;
|
||||
for rule in parsed.iter() {
|
||||
#[cfg(debug_assertions)]
|
||||
eprintln!("{:?}", rule);
|
||||
|
||||
if rule.name() != NEEDLE {
|
||||
if rule.all_rules(&parsed).search(NEEDLE).is_some() {
|
||||
found +=1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
println!("{}", found);
|
||||
}
|
||||
#[cfg(feature="part2")] println!("{}", cnt_rules(parsed.get(NEEDLE).unwrap(), &parsed));
|
||||
|
||||
Ok(())
|
||||
}
|
@ -1,64 +0,0 @@
|
||||
#![feature(str_split_once)]
|
||||
|
||||
#![allow(dead_code)]
|
||||
|
||||
use std::{
|
||||
io::BufReader,
|
||||
fs::OpenOptions,
|
||||
collections::HashSet,
|
||||
};
|
||||
|
||||
#[cfg(feature="test")]
|
||||
const INPUT: &str ="input-test2";
|
||||
#[cfg(not(feature="test"))]
|
||||
const INPUT: &str = "input";
|
||||
|
||||
const NEEDLE: &str = "shiny gold";
|
||||
|
||||
mod parse;
|
||||
mod bag;
|
||||
|
||||
fn cnt_rules(rule: &bag::Rule, set: &HashSet<bag::Rule>) -> usize
|
||||
{
|
||||
let mut already_done: HashSet<&bag::Rule> = HashSet::new();
|
||||
rule.all_rules(set).map(|x| {
|
||||
let mut o = cnt_rules(x, set) + x.inner_rules(set).count();
|
||||
|
||||
if already_done.insert(x) || true {
|
||||
if o > 0 {
|
||||
eprintln!("{:?} -> {}", x, o);
|
||||
o+=1;
|
||||
} else if !already_done.contains(x) {
|
||||
eprintln!("{:?} -> {}", x ,1);
|
||||
return 1;
|
||||
}
|
||||
o
|
||||
} else {
|
||||
0
|
||||
}
|
||||
}).sum()
|
||||
}
|
||||
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let parsed: HashSet<_> = parse::parse(BufReader::new(OpenOptions::new().read(true).open(INPUT)?)).collect();
|
||||
|
||||
let mut found=0;
|
||||
for rule in parsed.iter() {
|
||||
#[cfg(debug_assertions)]
|
||||
eprintln!("{:?}", rule);
|
||||
|
||||
if rule.name() != NEEDLE {
|
||||
if rule.all_rules(&parsed).search(NEEDLE).is_some() {
|
||||
found +=1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
println!("{}", found);
|
||||
|
||||
//#[cfg(feature="part2")]
|
||||
{
|
||||
println!("{}", cnt_rules(parsed.get(NEEDLE).unwrap(), &parsed));
|
||||
}
|
||||
Ok(())
|
||||
}
|
Loading…
Reference in new issue