Copy from github.com with solutions for days 1+2
This commit is contained in:
8
2022/day01-part1/Cargo.toml
Normal file
8
2022/day01-part1/Cargo.toml
Normal file
@@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "day01-part1"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
36
2022/day01-part1/src/main.rs
Normal file
36
2022/day01-part1/src/main.rs
Normal file
@@ -0,0 +1,36 @@
|
||||
fn main() {
|
||||
// Use command line arguments to specify the input filename.
|
||||
let args: Vec<String> = std::env::args().collect();
|
||||
if args.len() < 2 {
|
||||
panic!("Usage: ./main <input-file>\nNo input file provided. Exiting.");
|
||||
}
|
||||
|
||||
// Next, read the contents of the input file into a string for easier processing.
|
||||
let input = std::fs::read_to_string(&args[1]).expect("Error opening file");
|
||||
// Line-by-line processing is easiest.
|
||||
let mut input = input.lines();
|
||||
|
||||
// --- TASK BEGIN ---
|
||||
|
||||
// Keep track of the largest calorie-count and the "current" calorie-count.
|
||||
let mut max_cals = 0u32;
|
||||
let mut cals = 0u32;
|
||||
|
||||
// Iterate line-by-line.
|
||||
while let Some(line) = input.next() {
|
||||
// println!("Line is: {line}");
|
||||
match line.parse::<u32>() {
|
||||
Ok(num) => cals += num,
|
||||
Err(_) => {
|
||||
// println!("{cals}");
|
||||
max_cals = std::cmp::max(max_cals, cals);
|
||||
cals = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
// println!("{cals}");
|
||||
// Don't forget to check the very last block.
|
||||
max_cals = std::cmp::max(max_cals, cals);
|
||||
|
||||
println!("Maximum calories: {max_cals}");
|
||||
}
|
||||
8
2022/day01-part2/Cargo.toml
Normal file
8
2022/day01-part2/Cargo.toml
Normal file
@@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "day01-part2"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
51
2022/day01-part2/src/main.rs
Normal file
51
2022/day01-part2/src/main.rs
Normal file
@@ -0,0 +1,51 @@
|
||||
fn main() {
|
||||
// Use command line arguments to specify the input filename.
|
||||
let args: Vec<String> = std::env::args().collect();
|
||||
if args.len() < 2 {
|
||||
panic!("Usage: ./main <input-file>\nNo input file provided. Exiting.");
|
||||
}
|
||||
|
||||
// Next, read the contents of the input file into a string for easier processing.
|
||||
let input = std::fs::read_to_string(&args[1]).expect("Error opening file");
|
||||
// Line-by-line processing is easiest.
|
||||
let mut input = input.lines();
|
||||
|
||||
// --- TASK BEGIN ---
|
||||
|
||||
// Keep track of the largest calorie-count and the "current" calorie-count.
|
||||
let mut max_cals = [0u32, 0, 0];
|
||||
let mut cals = 0u32;
|
||||
|
||||
// Iterate line-by-line.
|
||||
while let Some(line) = input.next() {
|
||||
match line.parse::<u32>() {
|
||||
Ok(num) => cals += num,
|
||||
Err(_) => {
|
||||
update_cals(cals, &mut max_cals);
|
||||
cals = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
// Don't forget to check the very last block.
|
||||
update_cals(cals, &mut max_cals);
|
||||
|
||||
println!("Maximum Calories");
|
||||
for (i, mc) in max_cals.into_iter().enumerate() {
|
||||
println!(" No. {} : {}", i + 1, mc);
|
||||
}
|
||||
|
||||
println!("Total Calories: {}", max_cals.iter().sum::<u32>())
|
||||
}
|
||||
|
||||
fn update_cals(cals: u32, max_cals: &mut [u32; 3]) {
|
||||
if cals > max_cals[0] {
|
||||
max_cals[2] = max_cals[1];
|
||||
max_cals[1] = max_cals[0];
|
||||
max_cals[0] = cals;
|
||||
} else if cals > max_cals[1] {
|
||||
max_cals[2] = max_cals[1];
|
||||
max_cals[1] = cals;
|
||||
} else if cals > max_cals[2] {
|
||||
max_cals[2] = cals;
|
||||
}
|
||||
}
|
||||
8
2022/day02-part1/Cargo.toml
Normal file
8
2022/day02-part1/Cargo.toml
Normal file
@@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "day02-part1"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
67
2022/day02-part1/src/main.rs
Normal file
67
2022/day02-part1/src/main.rs
Normal file
@@ -0,0 +1,67 @@
|
||||
// Use a custom type to identify the different shapes that can be used in the game.
|
||||
#[derive(Copy, Clone)]
|
||||
enum Shape {
|
||||
Rock,
|
||||
Paper,
|
||||
Scissors,
|
||||
}
|
||||
|
||||
// Allows us to use the shapes without the Shape:: prefix.
|
||||
use Shape::*;
|
||||
|
||||
fn main() {
|
||||
// Use command line arguments to specify the input filename.
|
||||
let args: Vec<String> = std::env::args().collect();
|
||||
if args.len() < 2 {
|
||||
panic!("Usage: ./main <input-file>\nNo input file provided. Exiting.");
|
||||
}
|
||||
|
||||
// Next, read the contents of the input file into a string for easier processing.
|
||||
let input = std::fs::read_to_string(&args[1]).expect("Error opening file");
|
||||
// Line-by-line processing is easiest.
|
||||
let mut input = input.lines();
|
||||
|
||||
// --- TASK BEGIN ---
|
||||
let mut total_score = 0;
|
||||
|
||||
while let Some(line) = input.next() {
|
||||
// Translate the line's first character into its respective shape.
|
||||
let opponent_shape = match line.chars().nth(0) {
|
||||
Some('A') => Rock,
|
||||
Some('B') => Paper,
|
||||
Some('C') => Scissors,
|
||||
_ => { panic!("Unexpected left character."); }
|
||||
};
|
||||
|
||||
// Translate the line's second character into its respective shape.
|
||||
let player_shape = match line.chars().nth(2) {
|
||||
Some('X') => Rock,
|
||||
Some('Y') => Paper,
|
||||
Some('Z') => Scissors,
|
||||
_ => { panic!("Unexpected right character."); }
|
||||
};
|
||||
|
||||
// Add the score for the matchup (win/loss/draw) to the total score.
|
||||
total_score += match (player_shape, opponent_shape) {
|
||||
(Rock , Rock ) => 3,
|
||||
(Rock , Paper ) => 0,
|
||||
(Rock , Scissors) => 6,
|
||||
(Paper , Rock ) => 6,
|
||||
(Paper , Paper ) => 3,
|
||||
(Paper , Scissors) => 0,
|
||||
(Scissors, Rock ) => 0,
|
||||
(Scissors, Paper ) => 6,
|
||||
(Scissors, Scissors) => 3,
|
||||
};
|
||||
|
||||
// Add the score of the player's shape to the total score.
|
||||
total_score += match player_shape {
|
||||
Rock => 1,
|
||||
Paper => 2,
|
||||
Scissors => 3,
|
||||
};
|
||||
}
|
||||
|
||||
println!("Total score: {}", total_score);
|
||||
}
|
||||
|
||||
8
2022/day02-part2/Cargo.toml
Normal file
8
2022/day02-part2/Cargo.toml
Normal file
@@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "day02-part2"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
88
2022/day02-part2/src/main.rs
Normal file
88
2022/day02-part2/src/main.rs
Normal file
@@ -0,0 +1,88 @@
|
||||
// Use a custom type to identify the different shapes that can be used in the game.
|
||||
#[derive(Copy, Clone)]
|
||||
enum Shape {
|
||||
Rock,
|
||||
Paper,
|
||||
Scissors,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
enum Outcome {
|
||||
Loss,
|
||||
Draw,
|
||||
Win,
|
||||
}
|
||||
|
||||
// Allows us to use the shapes without the Shape:: prefix.
|
||||
use Shape::*;
|
||||
use Outcome::*;
|
||||
|
||||
fn main() {
|
||||
// Use command line arguments to specify the input filename.
|
||||
let args: Vec<String> = std::env::args().collect();
|
||||
if args.len() < 2 {
|
||||
panic!("Usage: ./main <input-file>\nNo input file provided. Exiting.");
|
||||
}
|
||||
|
||||
// Next, read the contents of the input file into a string for easier processing.
|
||||
let input = std::fs::read_to_string(&args[1]).expect("Error opening file");
|
||||
// Line-by-line processing is easiest.
|
||||
let mut input = input.lines();
|
||||
|
||||
// --- TASK BEGIN ---
|
||||
let mut total_score = 0;
|
||||
|
||||
while let Some(line) = input.next() {
|
||||
// Translate the line's first character into its respective shape.
|
||||
let opponent_shape = match line.chars().nth(0) {
|
||||
Some('A') => Rock,
|
||||
Some('B') => Paper,
|
||||
Some('C') => Scissors,
|
||||
_ => { panic!("Unexpected left character."); }
|
||||
};
|
||||
|
||||
// Translate the line's second character into its respective shape.
|
||||
let player_outcome = match line.chars().nth(2) {
|
||||
Some('X') => Loss,
|
||||
Some('Y') => Draw,
|
||||
Some('Z') => Win,
|
||||
_ => { panic!("Unexpected right character."); }
|
||||
};
|
||||
|
||||
// Determine the player_shape from the predetermined outcome.
|
||||
let player_shape = match (player_outcome, opponent_shape) {
|
||||
(Loss, Rock ) => Scissors,
|
||||
(Loss, Paper ) => Rock,
|
||||
(Loss, Scissors) => Paper,
|
||||
(Draw, Rock ) => Rock,
|
||||
(Draw, Paper ) => Paper,
|
||||
(Draw, Scissors) => Scissors,
|
||||
(Win , Rock ) => Paper,
|
||||
(Win , Paper ) => Scissors,
|
||||
(Win , Scissors) => Rock,
|
||||
};
|
||||
|
||||
// Add the score for the matchup (win/loss/draw) to the total score.
|
||||
total_score += match (player_shape, opponent_shape) {
|
||||
(Rock , Rock ) => 3,
|
||||
(Rock , Paper ) => 0,
|
||||
(Rock , Scissors) => 6,
|
||||
(Paper , Rock ) => 6,
|
||||
(Paper , Paper ) => 3,
|
||||
(Paper , Scissors) => 0,
|
||||
(Scissors, Rock ) => 0,
|
||||
(Scissors, Paper ) => 6,
|
||||
(Scissors, Scissors) => 3,
|
||||
};
|
||||
|
||||
// Add the score of the player's shape to the total score.
|
||||
total_score += match player_shape {
|
||||
Rock => 1,
|
||||
Paper => 2,
|
||||
Scissors => 3,
|
||||
};
|
||||
}
|
||||
|
||||
println!("Total score: {}", total_score);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user