This commit is contained in:
2022-12-03 01:10:06 +01:00
parent 98184053e9
commit 3cdb4b991d
4 changed files with 2594 additions and 0 deletions

11
day_2/Cargo.toml Normal file
View File

@@ -0,0 +1,11 @@
[package]
name = "advent_of_code_2022"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
rfd = "0.10.0"
phf = { version = "0.11", default-features = false }

12
day_2/day_2.iml Normal file
View File

@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="RUST_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

2500
day_2/input Normal file

File diff suppressed because it is too large Load Diff

71
day_2/src/main.rs Normal file
View File

@@ -0,0 +1,71 @@
use std::collections::HashMap;
use std::fs::File;
use std::io::{BufRead, BufReader};
use phf::phf_map;
static scores: phf::Map<&'static str, u8> = phf_map! (
"A" => 1, //rock
"B" => 2, //paper
"C" => 3, //scizzors
);
// figuring out a way to do this :/
// 1 2 = lose
// 1 3 = win
// 2 1 = win
// 2 3 = lose
// 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 shape_map: phf::Map<&'static String, &'static String> = phf_map! {
"X" => "A",
"Y" => "B",
"Z" => "C",
};
struct Play {
them: u8,
you: u8
}
fn main() {
println!("Hello, world!");
let current_path = std::env::current_dir().unwrap();
let res = rfd::FileDialog::new().set_directory(&current_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;
}
println!("the completed score for the whole book is {}",complete_score)
}
fn parse_line(line: String) -> Play {
let mut temp = line.split(' ').collect::<Vec<&str>>();
return Play {them: scores[temp[0].parse().unwrap()], you: scores[temp[1].parse().unwrap()] }
}
fn get_score(p: Play) -> u8 {
return possibilities[p.you*10+p.them]+scores[p.you];
}