Add D14P1

This commit is contained in:
kageru 2019-12-14 11:35:34 +01:00
parent 875c0959a4
commit 82a55b5a1e
Signed by: kageru
GPG Key ID: 8282A2BEA4ADA3D2
3 changed files with 163 additions and 0 deletions

10
2019/14/Cargo.toml Normal file
View File

@ -0,0 +1,10 @@
[package]
name = "day14"
version = "0.1.0"
authors = ["kageru <kageru@encode.moe>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
lazy_static = "1.4.0"

58
2019/14/input Normal file
View File

@ -0,0 +1,58 @@
8 SPJN, 2 LJRB, 1 QMDTJ => 1 TFPRF
111 ORE => 5 GCFP
5 NGCKP => 6 QXQZ
21 RGRLZ => 7 DKVN
2 DCKF => 9 FCMVJ
7 SGHSV, 4 LZPCS => 9 DQRCZ
4 QNRH => 8 WGKHJ
135 ORE => 6 BPLFB
4 SPJN, 1 DCKF, 9 KJVZ, 1 DKVN, 4 ZKVPL, 11 TFPRF, 1 CWPVT => 8 BVMK
8 TGPV, 4 MQPLD => 2 SPFZ
11 QMDTJ, 15 LVPK, 5 LZPCS => 3 KJVZ
2 RNXF, 3 MKMQ => 6 LJRB
11 RKCXJ, 4 BJHW, 2 DKDST => 3 QNRH
3 NZHP, 1 QMDTJ => 9 BCMKN
10 DQRCZ, 1 GBJF => 7 RGRLZ
2 WLKC, 1 GBJF, 7 SPJN => 5 GBWQT
4 TGPV, 1 LTSB => 2 LZPCS
6 LJRB => 4 LQHB
3 LZPCS, 3 MDTZL, 12 DLHS => 2 CBTK
1 TGPV, 1 CQPR => 9 XQZFV
26 FSQBL => 8 HQPG
9 LQHB => 1 GBJF
7 NGCKP => 5 WLKC
9 DKDST, 1 XQZFV => 9 TPZBM
144 ORE => 9 RNXF
1 LJRB => 6 CQPR
9 MKMQ, 12 RNXF => 9 JWPLZ
5 LZPCS, 28 QMDTJ, 1 QNRH => 5 LVPK
5 TGPV, 1 HQPG => 6 FCBLK
8 LVPK, 9 DQRCZ, 1 MDTZL => 6 DCKF
1 RKCXJ, 2 LZPCS, 13 LJNJ => 1 QWFG
4 DKDST, 1 XQZFV, 10 NSXFK => 4 JRDXQ
7 QWFG, 1 BVMK, 4 BJHW, 21 QNSWJ, 3 FBTW, 3 FCBLK, 59 SPFZ, 4 GBWQT => 1 FUEL
28 LZPCS, 17 NGCKP, 1 MQPLD => 5 MDTZL
1 FCBLK, 5 WGKHJ => 7 ZKVPL
7 LJNJ => 9 BLDJP
11 FSQBL, 2 BCMKN, 1 CBTK => 9 CWPVT
1 BJHW => 1 MQPLD
11 SGHSV, 3 LJNJ => 1 NGCKP
2 FSQBL, 7 FCBLK, 1 CQPR => 4 RKCXJ
1 JRDXQ => 4 SGHSV
107 ORE => 6 MKMQ
1 DQRCZ, 3 QMDTJ, 9 XQZFV => 4 FZVH
6 NSXFK, 1 MKMQ => 6 DLHS
4 CQPR, 1 RNXF, 1 HQPG => 5 DKDST
9 RNXF => 8 LTZTR
1 LTSB, 8 BLDJP => 4 SPJN
1 FCBLK => 4 LJNJ
1 NGCKP => 3 NZHP
11 LZPCS, 22 DQRCZ, 1 QWFG, 1 QXQZ, 6 DKVN, 16 FZVH, 3 MQPLD, 23 HQPG => 3 QNSWJ
26 DLHS, 1 NSXFK => 9 BJHW
3 FCBLK, 10 HQPG => 3 LTSB
10 LTZTR, 13 JWPLZ, 16 FSQBL => 4 TGPV
11 LTSB, 1 XQZFV, 3 DQRCZ => 4 CZCJ
1 HQPG, 12 XQZFV, 17 TPZBM => 6 QMDTJ
2 LTZTR => 7 FSQBL
1 GCFP, 5 BPLFB => 1 NSXFK
3 KJVZ, 1 QXQZ, 6 DKDST, 1 FCMVJ, 2 CZCJ, 1 QNRH, 7 WLKC => 4 FBTW

95
2019/14/src/main.rs Normal file
View File

@ -0,0 +1,95 @@
#[macro_use]
extern crate lazy_static;
use std::collections::HashMap;
use std::default::Default;
use std::io::{stdin, BufRead};
use std::sync::Mutex;
#[derive(Debug, Clone, Copy, Default)]
struct Element<'a> {
quantity: usize,
element: &'a str,
}
#[derive(Debug, Clone, Default)]
struct Reaction<'a> {
inputs: Vec<Element<'a>>,
output: Element<'a>,
}
impl<'a> From<&'a str> for Element<'a> {
fn from(other: &'a str) -> Self {
let mut parts = other.split(' ');
Element {
quantity: parts.next().unwrap().parse().unwrap(),
element: parts.next().unwrap(),
}
}
}
impl<'a> From<&'a str> for Reaction<'a> {
fn from(other: &'a str) -> Self {
let in_out: Vec<_> = other.split(" => ").collect();
let inputs = in_out[0].split(", ").map(|p| p.into()).collect();
let output = in_out[1].into();
Reaction { inputs, output }
}
}
lazy_static! {
static ref LINES: Vec<String> = stdin().lock().lines().map(|l| l.unwrap()).collect();
static ref REACTIONS: HashMap<&'static str, Reaction<'static>> = LINES
.iter()
.map::<Reaction, _>(|l| l.as_str().into())
.map(|r| (r.output.element.clone(), r))
.collect();
static ref STORAGE: Mutex<HashMap<&'static str, usize>> = Mutex::new(HashMap::new());
}
const ORE: &'static str = "ORE";
const FUEL: &'static str = "FUEL";
fn main() {
println!("FUEL: {:?}", REACTIONS.get("FUEL"));
println!("{}", count_ingredients(FUEL, 1));
}
fn count_ingredients(element: &'static str, desired_quantity: usize) -> usize {
if element == ORE {
return desired_quantity;
}
let mut ores = 0;
let reaction = REACTIONS.get(&element).unwrap();
let output_quantity = reaction.output.quantity;
//dbg!(&reaction);
let num_reactions = (desired_quantity as f32 / output_quantity as f32).ceil() as usize;
//dbg!(&num_reactions);
//println!("A: {}, B: {}", get_from_storage("A"), get_from_storage("B"));
for input in &reaction.inputs {
let in_storage = get_from_storage(input.element);
let mut needed = input.quantity * num_reactions;
if in_storage >= needed {
put_into_storage(input.element, in_storage - needed);
} else {
needed -= in_storage;
put_into_storage(input.element, 0);
ores += count_ingredients(input.element, needed);
}
}
//let num_reactions = (desired_quantity as f32 / output_quantity as f32).ceil() as usize;
//dbg!(&num_reactions);
let surplus = output_quantity * num_reactions - desired_quantity;
put_into_storage(element, get_from_storage(element) + surplus);
return ores;// * num_reactions;
}
#[inline]
fn get_from_storage(element: &str) -> usize {
*STORAGE.lock().unwrap().get(element).unwrap_or(&0)
}
#[inline]
fn put_into_storage(element: &'static str, quantity: usize) {
STORAGE.lock().unwrap().insert(element, quantity);
}