Commit 02ff3251 authored by Marcel Behrmann's avatar Marcel Behrmann

Merge branch 'master' of gitlab.com:EldoranDev/adventofcode

parents 9e9bb1e3 007390fc
mod utils;
mod intvm;
mod reaction;
use reaction::Reaction;
use std::fs;
use std::collections::{HashMap, VecDeque};
const INPUT_FILE: &str = "input.txt";
fn main() {
let input = fs::read_to_string(INPUT_FILE).expect("Error reading input");
let input =
"10 ORE => 10 A
1 ORE => 1 B
7 A, 1 B => 1 C
7 A, 1 C => 1 D
7 A, 1 D => 1 E
7 A, 1 E => 1 FUEL";
let input = input.lines();
let input: Vec<&str> = input.collect::<Vec<&str>>();
......@@ -14,7 +26,40 @@ fn main() {
}
#[allow(unused_variables)]
fn part_01(input: Vec<&str>) {
fn part_01(input: Vec<&str>) {
let mut recipies: HashMap<String, Reaction> = HashMap::new();
let mut requirments: VecDeque<(String, i64)> = VecDeque::new();
let mut produced: HashMap<String, i64> = HashMap::new();
let mut leftovers: HashMap<String, i64> = HashMap::new();
for line in input {
let reaction = Reaction::from_input(line);
recipies.insert(reaction.outcome.0.clone(), reaction);
}
requirments.push_back((String::from("FUEL"), 1));
while requirments.len() > 0 {
let req = requirments.pop_front().unwrap();
println!("NEED: {}x{}", req.0, req.1);
let reaction: &Reaction = recipies.get(&req.0).unwrap();
let left = leftovers.get(&req.0).or(Some(&0)).unwrap();
let need = (req.1 - left) / reaction.outcome.1;
println!("Need {} reactions for this", need);
println!("{:?}", reaction);
for r in &reaction.requirments {
requirments.push_back((r.0.clone(), r.1 * need));
}
}
println!("{:?}", requirments);
println!("Result of Part 1");
}
......
#[derive(Debug)]
pub struct Reaction {
pub outcome: (String, i64),
pub requirments: Vec<(String, i64)>,
}
impl Reaction {
pub fn from_input(input: &str) -> Reaction {
let parts = input.split("=>").map(|e| e.trim()).collect::<Vec<&str>>();
let reqs = parts.get(0).unwrap().split(",").map(|e| e.trim()).collect::<Vec<&str>>();
let res = parts.get(1).unwrap().split(" ").collect::<Vec<&str>>();
let mut requirments: Vec<(String, i64)> = Vec::new();
for req in &reqs {
let split = req.split(" ").collect::<Vec<&str>>();
requirments.push((
String::from(*split.get(1).unwrap()),
split.get(0).unwrap().parse::<i64>().unwrap(),
));
}
Reaction {
outcome: (String::from(*res.get(1).unwrap()), (*res.get(0).unwrap()).parse::<i64>().unwrap()),
requirments,
}
}
}
\ No newline at end of file
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