mirror of
https://github.com/bvanroll/advent_of_code_2022.git
synced 2025-08-29 03:32:40 +00:00
finished day 2
This commit is contained in:
@@ -40,7 +40,7 @@ At some point i wanted to define the map as a global variable and stumbled upon
|
||||
```rust
|
||||
use std::collections::HashMap;
|
||||
|
||||
pub const Countries: HashMap<&str, &str> = [
|
||||
pub const COUNTRIES: HashMap<&str, &str> = [
|
||||
("UK", "United Kingdom"),
|
||||
("US", "United States")
|
||||
].iter().cloned().collect();
|
||||
@@ -81,3 +81,8 @@ error: cannot determine resolution for the macro `phf_map`
|
||||
= note: import resolution is stuck, try simplifying macro imports
|
||||
```
|
||||
|
||||
--update
|
||||
all of that did not work... going to try to figure something out. i really didn't wanna initialise the maps on start.
|
||||
|
||||
--update 2
|
||||
we're skipping global, first answer [here](https://stackoverflow.com/a/27826181) says avoid global as much as possible followed by 19 paragraphs of explanation so i'm guessing this guy knows his stuff. Gonna initialise some map and pass it's ref to the functions needed
|
||||
|
@@ -4,11 +4,11 @@ use std::io::{BufRead, BufReader};
|
||||
use std::iter::Iterator;
|
||||
|
||||
|
||||
pub const scores:HashMap<&str, u32> = HashMap::from([
|
||||
("A",1), //rock
|
||||
("B",2), //paper
|
||||
("C",3) //scizzors
|
||||
]);
|
||||
// pub const scores: HashMap<&str, u8> = HashMap::from([
|
||||
// ("A",1), //rock
|
||||
// ("B",2), //paper
|
||||
// ("C",3) //scizzors
|
||||
// ]);
|
||||
|
||||
// figuring out a way to do this :/
|
||||
// 1 2 = lose
|
||||
@@ -17,55 +17,80 @@ pub const scores:HashMap<&str, u32> = HashMap::from([
|
||||
// 2 3 = lose
|
||||
// 3 1 = lose
|
||||
// 3 2 = win
|
||||
//
|
||||
// static possibilities: HashMap<i32,i32> = HashMap::from([
|
||||
// (12,0),
|
||||
// (13,6),
|
||||
// (21,6),
|
||||
// (23,0),
|
||||
// (31,0),
|
||||
// (32,6),
|
||||
// (11,3),
|
||||
// (22,3),
|
||||
// (33,3)
|
||||
// ]);
|
||||
|
||||
static possibilities: HashMap<u32,u32> = HashMap::from([
|
||||
(12,0),
|
||||
(13,6),
|
||||
(21,6),
|
||||
(23,0),
|
||||
(31,0),
|
||||
(32,6),
|
||||
(11,3),
|
||||
(22,3),
|
||||
(33,3)
|
||||
]);
|
||||
|
||||
static shape_map: HashMap<&str, &str> = [
|
||||
("X","A"),
|
||||
("Y","B"),
|
||||
("Z","C")
|
||||
].iter().cloned().collect();
|
||||
// static shape_map: HashMap<&str, &str> = [
|
||||
// ("X","A"),
|
||||
// ("Y","B"),
|
||||
// ("Z","C")
|
||||
// ].iter().cloned().collect();
|
||||
|
||||
struct Play {
|
||||
them: u32,
|
||||
you: u32
|
||||
them: i32,
|
||||
you: i32
|
||||
}
|
||||
|
||||
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
let scores: HashMap<&str, i32> = HashMap::from([
|
||||
("A",1), //rock
|
||||
("B",2), //paper
|
||||
("C",3), //scizzors
|
||||
("X",1),
|
||||
("Y",2),
|
||||
("Z",3)
|
||||
|
||||
]);
|
||||
|
||||
let possibilities: HashMap<i32,i32> = HashMap::from([
|
||||
(12,0),
|
||||
(13,6),
|
||||
(21,6),
|
||||
(23,0),
|
||||
(31,0),
|
||||
(32,6),
|
||||
(11,3),
|
||||
(22,3),
|
||||
(33,3)
|
||||
]);
|
||||
|
||||
|
||||
|
||||
let current_path = std::env::current_dir().unwrap();
|
||||
let res = rfd::FileDialog::new().set_directory(¤t_path).pick_file().unwrap();
|
||||
let book = File::open(res.as_path()).unwrap();
|
||||
|
||||
let reader = BufReader::new(book);
|
||||
let mut complete_score:u32 = 0;
|
||||
for line in reader.lines() {
|
||||
let play = parse_line(line.unwrap());
|
||||
let score = get_score(play);
|
||||
println!("score for line {} is {}", line.unwrap(), score);
|
||||
complete_score += score;
|
||||
let mut complete_score:i32 = 0;
|
||||
for buffer in reader.lines() {
|
||||
if let Ok(unparsed_play) = buffer { //found the way to handle these results :)
|
||||
let play = parse_line(&unparsed_play, &scores);
|
||||
let score = get_score(play, &possibilities);
|
||||
println!("score for line {} is {}", &unparsed_play, score);
|
||||
complete_score += score;
|
||||
}
|
||||
}
|
||||
|
||||
println!("the completed score for the whole book is {}",complete_score)
|
||||
}
|
||||
|
||||
fn parse_line(line: String) -> Play {
|
||||
fn parse_line(line: &String, scores: &HashMap<&str, i32>) -> Play {
|
||||
let mut temp = line.split(' ').collect::<Vec<&str>>();
|
||||
return Play {them: scores[temp[0]].clone(), you: scores[temp[1]].clone() }
|
||||
}
|
||||
|
||||
fn get_score(p: Play) -> u32 {
|
||||
fn get_score(p: Play, possibilities: &HashMap<i32, i32>) -> i32 {
|
||||
return possibilities[&(p.you*10+p.them)]+&p.you;
|
||||
}
|
||||
|
Reference in New Issue
Block a user