This commit is contained in:
LordMZTE 2021-04-20 22:30:14 +02:00
commit 402054df64
4 changed files with 491 additions and 0 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
/target
Cargo.lock

11
Cargo.toml Normal file
View file

@ -0,0 +1,11 @@
[package]
name = "alchemysolver"
version = "0.1.0"
authors = ["LordMZTE <lord@mzte.de>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
anyhow = "1.0.40"
petgraph = "0.5.1"

385
assets/combinations.txt Normal file
View file

@ -0,0 +1,385 @@
Airplane = Metal + Bird
Albert Einstine = Scientist + Energy
Alcohol = Fire + water
Alcoholic = Man + Alcohol
Algae = Life + water
Alien = Star + Life
Aluminium = Airplane + Metal
Ambulance = Hospital + Car
Aquarium = Glass + Fish
Arable = Earth + Tool
Arms = Metal + Tool
Ash = Fire + Tree
Ashtray = Ash + Glass
Assassin = Man + Poisoned Weapons
Austrialia = Kangaroo + Country
Avian Flu = Bird + Flu
Baby = Man + Sex
Bacon = Fire + Pig
Bacteria = Life + Swamp
Bar = Brick House + Beer
Barbeque = Fire + Meat
Bat = Bird + Vampire
Batman = Bat + Man
Beach = Water + Sand
Bear = Beast + Forest
Beast = Earth + Lizard
Beaver = Dam + Beast
Bee = Flower + beetle
Beer = Alcohol + Wheat
Beetle = Earth + Worm
Beetroot = Sugar + Seed
Belarus = Tractor + Country
Berry = Grass + Fruit
Bicycle = Wheel + Wheel
Bird = Air + Egg
Bitumen = Kerogen + Pressure
Blood = Beast + Hunter
Boat = Wood + Water
Boiler = Metal + Steam
Book = Feather + Paper
Borscht = Beetroot + Fire
Bow = Robin Hood + Arms
Bread = Dough + Fire
Brick = Clay + Fire
Brick House = Brick + Concrete
Butterfly = Air + Worm
C-3PO = Gold + Robot
Cactus = Desert + Tree
Camel = Desert + Beast
Cancer = Man + Tobacco
Car = Cart + Combustion Engine
Caramel = Sugar + Fire
Carbon Dioxide = Oxygen + Man
Carmine = Cochineal + Fire
Cart = Wheel + Wood
Cat = Mouse + Hunter
Catdog = Cat + Dog
Caviar = Fish + Fish
Cement = Clay + Limestone
Cemetery = Grave + Grave
Ceramics = Clay + Man
Champagne = Soda water + Wine
Chariot = Warrior + Cart
Cheese = Quark + Fire
Chicken = Egg + Life
China = Dragon + Country
Chocolate = Cocoa + Sugar
Christmas Tree = Tree + Lightbulb
Cigarettes = Paper + Tobacco
City = Skyscraper + Skyscraper
Clay = Sand + Swamp
Clock = Hourglass + Electricity
Cloth = Tool + Wool
Clothing = Cloth + Man
Cloud = Air + Steam
Coal = Fire + Tree
Coca-Cola = Soda Water + Carmine
Cochineal = Cactus + Beetle
Cocoa = Seed + Mexico
Coffin = Corpse + wood
Coin = Silver + Pressure
Combustion Engine = Steam Engine + Gasoline
Concrete = Cement + Water
Continent = Country + Country
Copper = Silver + Gold
Corpse = Fire + Man
Country = City + City
Crop Circles = UFO + Arable
Dam = Water + Brick
Desert = Sand + Sand
Diamond = Uncut Diamond + Tool
Diet = Man + Yogurt
Dilemma = Egg + Chicken
Dinosaur = Earth + Egg
Doctor = Hospital + Scientist
Dog = Wolf + Man
Dough = Flour + Water
Dr. Zoidberg = Doctor + Lobster
Dragon = Dinosaur + Fire
Dust = Air + Earth
E-Book = Microchip + Book
Ectoplasm = Ghost + Energy
Egg = Life + Stone
Egypt = Cat + Country
Electric Eel = Snake + Electricity
Electric Ray = Fish + Electricity
Electricity = Energy + Metal
Elephant = Whale + Earth
Energy = Air + Fire
ENT = Tree + Life
Explosion = Gasoline + Fire
Fabrege Egg = Egg + Diamond
Farmer = Man + Seed
Fat = Man + Pig
Feather = Bird + Hunter
Fern = Moss + Swamp
Finland = Sauna + Country
Fire Elemental = Fire + Life
Firearms = Arms + Gunpowder
Firefighter = Hero + Fire
Firefly = Light + Beetle
Fish = Bacteria + Plankton
Fisherman = Hunter + Fish
Flour = Stone + Wheat
Flower = Water + Seed
Flu = Air + Bacteria
Fondue = Cheese + Fire
Forest = Grove + Grove
Fossil = Butterfly + Earth
France = Champagne + Country
Frankenstein = Corpse + Electricity
Fried Chicken = Chicken + Fire
Frog = Lizard + Swamp
Fruit = Tree + Farmer
Fugu = Fish + Poison
Gasoline = Petroleum + Pressure
Genie = Lamp + Ghost
Germany = VW Beetle + Country
Geyser = Steam + Earth
Ghost = Ash + Life
Ghostbusters = Hunter + Ghost
Glass = Fire + Sand
Gold = Philosopher's Stone + Silver
Golem = Clay + Life
Grape = Earth + Wood
Grass = Earth + Moss
Grave = Earth + Corpse
Grove = Tree + Tree
Gunpowder = Dust + Fire
Hen Coop = Chicken + Hut
Hero = Dragon + Warrior
Honey = Bee + Tree
Hospital = Brick House + Sick
Hot Chocolate = Chocolate + Fire
Hourglass = Glass + sand
House M.D. = Man + Vicodin
Hunter = Arms + Man
Hut = Man + Stone
Hydrogen = Electricity + Water
Ice = Water + Glass
Iceland = Volcano + Country
Idea = Man + Light bulb
India = Kama Sutra + Country
Iodine = Algae + Fire
iPhone = Phone + Fruit
Island = Sea + Volcano
Italy = Venice + Country
Japan = Sushi + Country
Jedi = Warrior + Lightsaber
Juice = Berry + Pressure
Kama Sutra = Book + Sex
Kangaroo = Frog + 1UP
Kerogen = Fossil + Pressure
Kilt = Scotland + Clothing
Knife = Meat + Tool
Lamp = Glass + Fire
Lance Armstrong = Cancer + Bike
Lava = Earth + Fire
Lava Golem = Lava + Life
Lava Lamp = Lava + Lamp
Lawn Mower = Scissors + Combustion Engine
Leech = Blood + Worm
Library = Book + Book
Lichen = Mushroom + Algae
Life = Energy + Swamp
Light = Electricity + Light Bulb
Light Bulb = Electricity + Glass
Lighthouse = Skyscraper + Light
Lightning Rod = Thunderstorm + Metal
Lightsaber = Arms + Light
Lime = Fire + Limestone
Limestone = Shells + Stone
Livestock = Beast + Man
Lizard = Snake + Worm
Lobster = Scorpion + Water
Locomotive = Cart + Steam Engine
Man = Golem + Life
Manure = Grass + Livestock
Mario = 1up + Man
McDonald's = Coca-Cola + Sandwich
Meat = Beast + Hunter
Mentos = Coca-Cola + Geyser
Mermaid = Woman + Fish
Metal = Fire + Stone
Metal Golem = Metal + Life
Mexico = Tequila + Country
Microchip = Transistor + Transistor
Milk = Livestock + Man
Mirror = Aliminium + Glass
Mite = Life + Dust
Mold = Mushroom + Mud
Molotov Cocktail = Fire + Alcohol
Money = Coin + Paper
Moon = Cheese + Sky
Moss = Swamp + Algae
Motorboat = Boat + Combustion Engine
Morotcycle = Bicycle + Combustion Engine
Mouse = Cheese + Beast
Mud = Dust + Water
Mummy = Paper + Zombie
Museum = Brick House + Fossil
Mushroom = Earth + Algae
Music = Sound + Idea
Obesity = Man + McDonald's
Old Man = Man + Time
Omelette = Egg + Fire
Oxygen = Electricity + Water
Oxyhydrogen = Oxygen + Hydrogen
Ozone = Oxygen + Electricity
Panda = Tree + Beast
Paper = Reed + Tool
Pearl = Sand + Shells
Peat = Swamp + Tree
Penguin = Bird + Ice
Penicillin = Mold + Scientist
Perfume = Alcohol + Flower
Petri Dish = Glass + Bacteria
Petroleum = Bitumen + Pressure
Philosopher's Stone = Philosophy + Stone
Philosophy = Dilemma + Scientist
Phoenix = Fire + Bird
Phone = Wire + Sound
Pie = Fruit + Dough
Pig = Livestock + Mud
Piggy Bank = Ceramics + Coin
Pillow = Feather + Cloth
Pinocchio = Wood + Life
Pizza = Cheese + Dough
Planet = Continent + Continent
Plankton = Bacteria + Water
Plesiosauria = Water + Dinosaur
Poison = Mushroom + Tool
Poisoned Weapons = Arms + Poison
Pressure = Earth + Earth
Prisoner = Assassin + Time
Pterodactyl = Air + Dinosaur
Quark = Soured Milk + Fire
Quetzalcoatl = Snake + Bird
R2-D2 = Star Wars + Robot
Rain = Water + Cloud
Rainbow = Light + Storm
Reed = Grass + Swamp
Robin Hood = Forest + Hero
Robot = Metal Golem + Electricity
Romania = Transylvania + Country
Ruby = Aluminuim + Oxygen
Russia = Vodka + Country
Rust = Water + Metal
Sailboat = Boat + Cloth
Sailing Ship = Cloth + Wooden ship
Sailor = Man + Boat
Salamander = Lizard + Fire
Salo = Man + Pig
Salt = Sea + Fire
Saltpeter = Limestone + Manure
Sand = Water + Stone
Sandstorm = Sand + Storm
Sandwich = Meat + Bread
Santa Clause = Old Man + Christmas Tree
Saudi Arabia = Petroleum + Country
Sauna = Steam + Hut
Scarab = Beetle + Manure
Scientist = Library + Man
Scissors = Knife + Knife
Scorpion = Beetle + Sand
Scotch Whiskey = Alcohol + Peat
Scotland = Scotch Whiskey + Country
Sea = Water + Water
Seed = Life + Sand
Sex = Man + Woman
Sex and the City = Sex + City
Shells = Plankton + Stone
Sick = Man + Flu
Silicon = Pressure + Sand
Silk = China + Cloth
Silver = Moon + Metal
Sith = Jedi + Assassin
Sky = Air + Cloud
Skyscraper = Brick House + Glass
Smoke = Tobacco + Fir
Snail = Shells + Worm
Snake = Swamp + Worm
Sniper = Assassin + Firearms
Soap = Ash + Fat
Soda Water = Carbon Dioxide + Water
Soldier = Firearms + Man
Sound = Metal + wind
Soured Milk = Milk + Yogurt
Spinning Wheel = Wheel + Wool
Stake = Wood + Knife
Star = Sun + Scientist
Star Wars = Jedi + Sith
Statue = Stone + Tool
Statue of Liberty = USA + Statue
Steam = Air + Water
Steam Engine = Boiler + Coal
Steamer = Steam Engine + Wooden Ship
Stone = Air + Lava
Storm = Air + Energy
Sugar = Lime + Reed
Sulfur = Bacteria + Swamp
Sun = Sky + Chariot
Sunflower = Sun + Flower
Sushi = Fish + Algae
Swamp = Earth + Water
Sweater = Yarn + Tool
Swine Flu = Pig + Flu
Swiss Army Knife = Knife + Tool
Switzerland = Fondue + Country
Team = Beast + Cart
Tequila = Alcohol + Worm
The Beatles = Beetle + Beetle
Thread = Spinning Wheel + Yarn
Thunderbird = Bird + Storm
Thunderstorm = Storm + Electricity
Time = Life + Hourglass
Toast = Fire + Bread
Tobacco = Fire + Grass
Tool = Man + Metal
Totoro = Forest + Ghost
Tractor = Lawn Mower + Arable
Transformers = Car + Life
Transistor = Silicon + Electricity
Transylvania = Vampire + Country
Tree = Earth + Seed
Turtle = Sand + Egg
Twilight Saga = Vampire + Werewolf
Typhoon = Storm + Water
UFO = Alien + Airplane
Ukraine = Salo + Country
Uncut Diamond = Coal + Pressure
Undead = Zombie + Corpse
United Kingdom = Beatles + Country
USA = Coca-Cola + Country
Vampire = Blood + Man
Venice = City + Water
Venus = Woman + Planet
Vicodin = Sick + Doctor
Vinegar = Alcohol + Oxygen
Vodka = Alcohol + Water
Volcano = Lava + Pressure
Vulture = Corpse + Bird
VW Beetle = Car + Beetle
Warrior = Arms + Hunter
Weevil = Flour + Beetle
Werewolf = Beast + Vampire
Whale = Beast + Water
Wheat = Arable + Seed
Wheel = Tool + Wood
Whey = Soured Milk + Fire
Wind = Air + Air
Wine = Grape + Alcohol
Wire = Thread + Copper
Wolf = Moon + Beast
Woman = Milk + Man
Wood = Tool + Tree
Wooden Ship = Boat + Wood
Wool = Beast + Hunter
Worm = Earth + Plankton
Yarn = Spinning Wheel + Wool
Yoda = Jedi + Swamp
Yogurt = Bacteria + Milk
Yoshi = 1up + Egg
Zombie = Fossil + Life
Zoo = Beast + Museum

93
src/main.rs Normal file
View file

@ -0,0 +1,93 @@
use petgraph::dot::{Dot, Config};
use petgraph::{Graph, graph::NodeIndex};
use anyhow::Context;
use std::{collections::HashMap, str::FromStr};
use std::env;
fn main() -> anyhow::Result<()> {
let g = include_str!("../assets/combinations.txt").parse::<GraphGen>()?;
let searched = env::args().nth(1).context("Supply argument for searched element")?;
let graph = g.create_graph(&searched);
let dot = Dot::with_config(&graph, &[Config::EdgeNoLabel]);
println!("{}", dot);
Ok(())
}
#[derive(Debug, Default)]
struct GraphGen {
map: HashMap<String, (String, String)>,
graph: Graph<String, String>,
}
impl GraphGen {
fn try_parse_comb(&mut self, c: &str) -> anyhow::Result<()> {
let err = "Failed to Parse combination";
let mut split = c.splitn(2, '=');
let name = split.next().context(err)?.trim().to_owned();
let rest = split.next().context(err)?;
let mut split = rest.splitn(2, '+');
let ing_a = split.next().context(err)?.trim().to_owned();
let ing_b = split.next().context(err)?.trim().to_owned();
self.map.insert(name, (ing_a, ing_b));
Ok(())
}
fn populate(&mut self, elem: String) {
let ab = self.map.get(&elem);
if !self.graph.node_indices().any(|n| self.graph.node_weight(n).unwrap() == &elem) {
self.graph.add_node(elem);
}
if let Some((a, b)) = ab {
let a = a.clone();
let b = b.clone();
self.populate(a);
self.populate(b);
}
}
fn link(&mut self) {
for n in self.graph.node_indices() {
let w = self.graph.node_weight(n).unwrap();
if let Some((a, b)) = self.map.get(w) {
if let Some(i) = self.find_node(a) {
self.graph.add_edge(i, n, String::new());
}
if let Some(i) = self.find_node(b) {
self.graph.add_edge(i, n, String::new());
}
}
}
}
fn find_node(&self, w: &str) -> Option<NodeIndex>{
self.graph.node_indices().find(|g| &self.graph[*g] == w)
}
fn create_graph(mut self, node: &str) -> Graph<String, String> {
self.populate(node.to_owned());
self.link();
self.graph
}
}
impl FromStr for GraphGen {
type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let mut this = Self::default();
for l in s.lines() {
this.try_parse_comb(l)?;
}
Ok(this)
}
}