From 8186283cf6c64439c61e71a9ffbfab8da831192e Mon Sep 17 00:00:00 2001 From: Beppe Date: Sat, 3 Dec 2022 01:32:10 +0100 Subject: [PATCH] i'm taking a break --- day_2/Cargo.toml | 2 -- day_2/readme.md | 27 +++++++++++++++++++++++- day_2/src/main.rs | 54 +++++++++++++++++++++++------------------------ 3 files changed, 53 insertions(+), 30 deletions(-) diff --git a/day_2/Cargo.toml b/day_2/Cargo.toml index 087548f..170376e 100644 --- a/day_2/Cargo.toml +++ b/day_2/Cargo.toml @@ -7,5 +7,3 @@ edition = "2021" [dependencies] rfd = "0.10.0" - -phf = { version = "0.11", default-features = false } diff --git a/day_2/readme.md b/day_2/readme.md index 332dd7b..38c4054 100644 --- a/day_2/readme.md +++ b/day_2/readme.md @@ -55,4 +55,29 @@ static COUNTRIES: phf::Map<&'static str, &'static str> = phf_map! { "US" => "United States", "UK" => "United Kingdom", }; -``` \ No newline at end of file +``` + +this gave some issues when trying to import the crate (something with the macro's). +I couldn't find the fix for this in the documentation so i decided in the end to go back to the ugly but native solution + +example of the errors: + +``` +error[E0432]: unresolved import `phf::phf_map` + --> src\main.rs:5:5 + | +5 | use phf::phf_map; + | ^^^^^^^^^^^^ no `phf_map` in the root +``` + + +``` +error: cannot determine resolution for the macro `phf_map` + --> src\main.rs:7:49 + | +7 | pub static scores: phf::Map<&'static str, u8> = phf_map! ( + | ^^^^^^^ + | + = note: import resolution is stuck, try simplifying macro imports +``` + diff --git a/day_2/src/main.rs b/day_2/src/main.rs index 8d7679e..d9c444b 100644 --- a/day_2/src/main.rs +++ b/day_2/src/main.rs @@ -1,14 +1,14 @@ use std::collections::HashMap; use std::fs::File; use std::io::{BufRead, BufReader}; -use phf::phf_map; +use std::iter::Iterator; -static scores: phf::Map<&'static str, u8> = phf_map! ( - "A" => 1, //rock - "B" => 2, //paper - "C" => 3, //scizzors -); +pub const scores:HashMap<&str, u32> = HashMap::from([ + ("A",1), //rock + ("B",2), //paper + ("C",3) //scizzors +]); // figuring out a way to do this :/ // 1 2 = lose @@ -18,27 +18,27 @@ static scores: phf::Map<&'static str, u8> = phf_map! ( // 3 1 = lose // 3 2 = win -static possibilities: phf::Map<&'static u8, &'static u8> = phf_map! { - 12 => 0, - 13 => 6, - 21 => 6, - 23 => 0, - 31 => 0, - 32 => 6, - 11 => 3, - 22 => 3, - 33 => 3, -}; +static possibilities: HashMap = HashMap::from([ + (12,0), + (13,6), + (21,6), + (23,0), + (31,0), + (32,6), + (11,3), + (22,3), + (33,3) +]); -static shape_map: phf::Map<&'static String, &'static String> = phf_map! { - "X" => "A", - "Y" => "B", - "Z" => "C", -}; +static shape_map: HashMap<&str, &str> = [ + ("X","A"), + ("Y","B"), + ("Z","C") +].iter().cloned().collect(); struct Play { - them: u8, - you: u8 + them: u32, + you: u32 } @@ -63,9 +63,9 @@ fn main() { fn parse_line(line: String) -> Play { let mut temp = line.split(' ').collect::>(); - return Play {them: scores[temp[0].parse().unwrap()], you: scores[temp[1].parse().unwrap()] } + return Play {them: scores[temp[0]].clone(), you: scores[temp[1]].clone() } } -fn get_score(p: Play) -> u8 { - return possibilities[p.you*10+p.them]+scores[p.you]; +fn get_score(p: Play) -> u32 { + return possibilities[&(p.you*10+p.them)]+&p.you; }