Initial commit
This commit is contained in:
commit
038f6841b3
15 changed files with 5750 additions and 0 deletions
34
src/cli/commands/day001.rs
Normal file
34
src/cli/commands/day001.rs
Normal file
|
@ -0,0 +1,34 @@
|
|||
use crate::cli::PathBuf;
|
||||
use crate::util::read_lines;
|
||||
|
||||
pub fn subcmd_day001(file: &Option<PathBuf>, top: &i32, total: &bool) {
|
||||
if let Ok(lines) = read_lines(file.as_ref().unwrap().as_path()) {
|
||||
// Consumes the iterator, returns an (Optional) String
|
||||
let mut nutri: i32 = 0;
|
||||
let mut hist: Vec<i32> = Vec::new();
|
||||
|
||||
for line in lines {
|
||||
if let Ok(num) = line {
|
||||
if num.is_empty() {
|
||||
hist.push(nutri);
|
||||
nutri = 0;
|
||||
} else {
|
||||
nutri += num.parse::<i32>().unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
hist.sort();
|
||||
let hist_len = hist.len();
|
||||
let mut top_total: i32 = 0;
|
||||
for x in 0..*top{
|
||||
let index: usize = hist_len - 1 - usize::try_from(x).unwrap();
|
||||
top_total += hist[index];
|
||||
println!("{:?}", hist[index]);
|
||||
}
|
||||
if *total{
|
||||
println!("Total:");
|
||||
println!("{:?}", top_total);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
209
src/cli/commands/day002.rs
Normal file
209
src/cli/commands/day002.rs
Normal file
|
@ -0,0 +1,209 @@
|
|||
use crate::cli::PathBuf;
|
||||
use crate::util::read_lines;
|
||||
#[derive(Debug)]
|
||||
enum Shape {
|
||||
Rock,
|
||||
Paper,
|
||||
Scissor,
|
||||
}
|
||||
|
||||
impl Shape {
|
||||
fn get_value(&self) -> u32 {
|
||||
match self {
|
||||
Shape::Rock => 1,
|
||||
Shape::Paper => 2,
|
||||
Shape::Scissor => 3,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
enum EncrypedShape {
|
||||
A,
|
||||
B,
|
||||
C,
|
||||
X,
|
||||
Y,
|
||||
Z,
|
||||
}
|
||||
|
||||
impl EncrypedShape {
|
||||
fn from_str(s: &str) -> Self {
|
||||
match Some(s) {
|
||||
Some("A") => EncrypedShape::A,
|
||||
Some("B") => EncrypedShape::B,
|
||||
Some("C") => EncrypedShape::C,
|
||||
Some("X") => EncrypedShape::X,
|
||||
Some("Y") => EncrypedShape::Y,
|
||||
Some("Z") => EncrypedShape::Z,
|
||||
_ => panic!(),
|
||||
}
|
||||
}
|
||||
|
||||
fn get_shape(&self) -> Shape {
|
||||
match self {
|
||||
EncrypedShape::A | EncrypedShape::X => Shape::Rock,
|
||||
EncrypedShape::B | EncrypedShape::Y => Shape::Paper,
|
||||
EncrypedShape::C | EncrypedShape::Z => Shape::Scissor,
|
||||
}
|
||||
}
|
||||
|
||||
fn predict_result(&self) -> RpsResult {
|
||||
match self {
|
||||
EncrypedShape::X => RpsResult::Loss,
|
||||
EncrypedShape::Y => RpsResult::Draw,
|
||||
EncrypedShape::Z => RpsResult::Win,
|
||||
EncrypedShape::A | EncrypedShape::B | EncrypedShape::C => panic!(),
|
||||
}
|
||||
}
|
||||
|
||||
fn get_value(&self) -> u32 {
|
||||
self.get_shape().get_value()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
enum RpsResult {
|
||||
Win,
|
||||
Draw,
|
||||
Loss,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct RPSPair {
|
||||
op: EncrypedShape,
|
||||
me: EncrypedShape,
|
||||
}
|
||||
|
||||
impl RPSPair {
|
||||
fn from_vec(v: Vec<&str>) -> Self {
|
||||
RPSPair {
|
||||
op: (EncrypedShape::from_str(v.first().unwrap())),
|
||||
me: (EncrypedShape::from_str(v.last().unwrap())),
|
||||
}
|
||||
}
|
||||
|
||||
fn my_shape_by_predicted_result(&self) -> Shape {
|
||||
match self.op.get_shape() {
|
||||
Shape::Rock => match self.me.predict_result() {
|
||||
RpsResult::Win => Shape::Paper,
|
||||
RpsResult::Draw => Shape::Rock,
|
||||
RpsResult::Loss => Shape::Scissor,
|
||||
},
|
||||
Shape::Paper => match self.me.predict_result() {
|
||||
RpsResult::Win => Shape::Scissor,
|
||||
RpsResult::Draw => Shape::Paper,
|
||||
RpsResult::Loss => Shape::Rock,
|
||||
},
|
||||
Shape::Scissor => match self.me.predict_result() {
|
||||
RpsResult::Win => Shape::Rock,
|
||||
RpsResult::Draw => Shape::Scissor,
|
||||
RpsResult::Loss => Shape::Paper,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn get_points(&self, alt: &bool) -> u32 {
|
||||
let extra_points: u32;
|
||||
let r: RpsResult;
|
||||
let mv: u32;
|
||||
if *alt {
|
||||
r = self.me.predict_result();
|
||||
mv = self.my_shape_by_predicted_result().get_value();
|
||||
} else {
|
||||
r = self.get_result();
|
||||
mv = self.me.get_value();
|
||||
}
|
||||
match r {
|
||||
RpsResult::Win => {
|
||||
extra_points = 6;
|
||||
}
|
||||
RpsResult::Draw => {
|
||||
extra_points = 3;
|
||||
}
|
||||
RpsResult::Loss => {
|
||||
extra_points = 0;
|
||||
}
|
||||
}
|
||||
mv + extra_points
|
||||
}
|
||||
|
||||
fn get_result(&self) -> RpsResult {
|
||||
match self.me.get_shape() {
|
||||
Shape::Rock => match self.op.get_shape() {
|
||||
Shape::Rock => RpsResult::Draw,
|
||||
Shape::Paper => RpsResult::Loss,
|
||||
Shape::Scissor => RpsResult::Win,
|
||||
},
|
||||
Shape::Paper => match self.op.get_shape() {
|
||||
Shape::Rock => RpsResult::Win,
|
||||
Shape::Paper => RpsResult::Draw,
|
||||
Shape::Scissor => RpsResult::Loss,
|
||||
},
|
||||
Shape::Scissor => match self.op.get_shape() {
|
||||
Shape::Rock => RpsResult::Loss,
|
||||
Shape::Paper => RpsResult::Win,
|
||||
Shape::Scissor => RpsResult::Draw,
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn subcmd_day002(file: &Option<PathBuf>, alt: &bool) {
|
||||
if let Ok(lines) = read_lines(file.as_ref().unwrap().as_path()) {
|
||||
let mut my_score: u32 = 0;
|
||||
for line in lines {
|
||||
if let Ok(ln) = line {
|
||||
let pair: RPSPair = RPSPair::from_vec(ln.split(" ").collect::<Vec<&str>>());
|
||||
let my_points: u32 = pair.get_points(alt);
|
||||
let my_value: u32;
|
||||
let my_shape: Shape;
|
||||
let my_result: RpsResult;
|
||||
if *alt {
|
||||
my_shape = pair.my_shape_by_predicted_result();
|
||||
my_value = my_shape.get_value();
|
||||
my_result = pair.me.predict_result();
|
||||
} else {
|
||||
my_shape = pair.me.get_shape();
|
||||
my_value = pair.me.get_value();
|
||||
my_result = pair.get_result();
|
||||
}
|
||||
my_score += my_points;
|
||||
println!(
|
||||
"op: {:?}({:?}), me: {:?}({:?}), result: {:?}, points: {:?}",
|
||||
pair.op.get_shape(),
|
||||
pair.op.get_value(),
|
||||
my_shape,
|
||||
my_value,
|
||||
my_result,
|
||||
my_points
|
||||
);
|
||||
}
|
||||
}
|
||||
println!("My Total Points: {:?}", my_score)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn subcmd_day002_op(file: &Option<PathBuf>, alt: &bool) {
|
||||
if let Ok(lines) = read_lines(file.as_ref().unwrap().as_path()) {
|
||||
let mut my_score: u32 = 0;
|
||||
for line in lines {
|
||||
if let Ok(ln) = line {
|
||||
let pair: RPSPair = RPSPair::from_vec(ln.split(" ").collect::<Vec<&str>>());
|
||||
my_score += pair.get_points(alt);
|
||||
}
|
||||
}
|
||||
println!("My Total Points: {:?}", my_score)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn subcmd_day002_iter(file: &Option<PathBuf>, alt: &bool) {
|
||||
if let Ok(lines) = read_lines(file.as_ref().unwrap().as_path()) {
|
||||
let points:u32 = lines
|
||||
.into_iter()
|
||||
.map(|line| RPSPair::from_vec(line.unwrap().split(" ").collect::<Vec<&str>>()))
|
||||
.map(|pair| pair.get_points(alt))
|
||||
.sum();
|
||||
println!("My Total Points: {:?}", points)
|
||||
}
|
||||
}
|
4
src/cli/commands/day003.rs
Normal file
4
src/cli/commands/day003.rs
Normal file
|
@ -0,0 +1,4 @@
|
|||
use crate::cli::PathBuf;
|
||||
pub fn subcmd_day003(file: &Option<PathBuf>, alt: &bool) {
|
||||
|
||||
}
|
3
src/cli/commands/mod.rs
Normal file
3
src/cli/commands/mod.rs
Normal file
|
@ -0,0 +1,3 @@
|
|||
pub mod day001;
|
||||
pub mod day002;
|
||||
pub mod day003;
|
61
src/cli/mod.rs
Normal file
61
src/cli/mod.rs
Normal file
|
@ -0,0 +1,61 @@
|
|||
mod commands;
|
||||
use clap::{arg, Parser, Subcommand};
|
||||
use commands::day001::*;
|
||||
use commands::day002::*;
|
||||
use commands::day003::*;
|
||||
//use std::ffi::OsString;
|
||||
use std::path::PathBuf;
|
||||
|
||||
#[derive(Parser)]
|
||||
#[command(author, version, about, long_about = None)]
|
||||
#[command(propagate_version = true)]
|
||||
pub struct AdventOfCode2022 {
|
||||
#[command(subcommand)]
|
||||
command: Commands,
|
||||
/// makes the commands more verbose
|
||||
#[arg(short, long)]
|
||||
verbose: bool,
|
||||
}
|
||||
|
||||
#[derive(Subcommand)]
|
||||
pub enum Commands {
|
||||
/// Solution for the first day
|
||||
Day001 {
|
||||
file: Option<PathBuf>,
|
||||
/// option to get more than the biggest value
|
||||
#[arg(short, long, default_value_t = 1)]
|
||||
top: i32,
|
||||
/// calc the sume of all wanted top positions
|
||||
#[arg(long)]
|
||||
total: bool,
|
||||
},
|
||||
/// Solution for the second day
|
||||
Day002 {
|
||||
file: Option<PathBuf>,
|
||||
#[arg(long)]
|
||||
alt: bool,
|
||||
/// select implemetation 0 = nomarl with debug, 1 = only result, 2 iter based
|
||||
#[arg(short, long, default_value_t = 0)]
|
||||
mode: u8,
|
||||
},
|
||||
/// Solution for day 03
|
||||
Day003 {
|
||||
file: Option<PathBuf>,
|
||||
#[arg(long)]
|
||||
alt: bool,
|
||||
},
|
||||
}
|
||||
|
||||
pub fn execute_cli() {
|
||||
let aoc2022 = AdventOfCode2022::parse();
|
||||
match &aoc2022.command {
|
||||
Commands::Day001 { file, top, total } => subcmd_day001(&file, &top, &total),
|
||||
Commands::Day002 { file, alt, mode } => match mode {
|
||||
0 => subcmd_day002(&file, &alt),
|
||||
1 => subcmd_day002_op(&file, &alt),
|
||||
2 => subcmd_day002_iter(&file, &alt),
|
||||
_ => panic!(),
|
||||
},
|
||||
Commands::Day003 { file, alt } => subcmd_day003(&file, &alt),
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue