1
0

Compare commits

...

2 Commits

Author SHA1 Message Date
cede3289dc
day 2 2022-12-23 18:09:51 +00:00
c5d6c5142d
day 1 2022-12-23 18:09:40 +00:00
8 changed files with 4885 additions and 0 deletions

8
day1/Cargo.toml Normal file
View File

@ -0,0 +1,8 @@
[package]
name = "day1"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

2242
day1/input.txt Normal file

File diff suppressed because it is too large Load Diff

14
day1/sample_input.txt Normal file
View File

@ -0,0 +1,14 @@
1000
2000
3000
4000
5000
6000
7000
8000
9000
10000

24
day1/src/main.rs Normal file
View File

@ -0,0 +1,24 @@
fn main() {
const INPUT: &str = include_str!("../input.txt");
let split = INPUT.split("\n");
let mut calories: Vec<u64> = Vec::new();
let mut running_total: u64 = 0;
for s in split {
if s == "" {
calories.push(running_total);
running_total = 0;
} else {
running_total += s.trim().parse::<u64>().expect("Invalid input line: {s}!");
}
}
let max_calories = calories.iter().max().unwrap();
println!("Max calories: {max_calories}");
calories.sort_by(|a, b| b.cmp(a));
let top_three = calories[0] + calories[1] + calories[2];
println!("Sum of top 3: {top_three}");
}

8
day2/Cargo.toml Normal file
View File

@ -0,0 +1,8 @@
[package]
name = "day2"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

2500
day2/input.txt Normal file

File diff suppressed because it is too large Load Diff

3
day2/sample_input.txt Normal file
View File

@ -0,0 +1,3 @@
A Y
B X
C Z

86
day2/src/main.rs Normal file
View File

@ -0,0 +1,86 @@
#[derive(PartialEq)]
#[repr(u8)]
enum Move {
ROCK = 1,
PAPER = 2,
SCISSORS = 3
}
#[repr(u8)]
enum Outcome {
LOSS = 0,
DRAW = 3,
WIN = 6
}
impl TryFrom<char> for Move {
type Error = ();
fn try_from(c: char) -> Result<Self, Self::Error> {
match c {
'A' | 'X' => Ok(Move::ROCK),
'B' | 'Y' => Ok(Move::PAPER),
'C' | 'Z' => Ok(Move::SCISSORS),
_ => Err(()),
}
}
}
impl std::fmt::Display for Move {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Move::ROCK => f.write_str("Rock").unwrap(),
Move::PAPER => f.write_str("Paper").unwrap(),
Move::SCISSORS => f.write_str("Scissors").unwrap(),
};
std::fmt::Result::Ok(())
}
}
fn main() {
const INPUT: &str = include_str!("../input.txt");
let mut your_score: u64 = 0;
let mut their_score: u64 = 0;
let lines = INPUT.split("\n");
for line in lines {
let mut tokens = line.split(' ');
let their_move = tokens.next().unwrap();
let your_move = tokens.next().unwrap();
let their_move = Move::try_from(their_move.chars().next().unwrap()).unwrap();
let your_move = Move::try_from(your_move.chars().next().unwrap()).unwrap();
let outcome: Outcome;
if their_move == your_move {
outcome = Outcome::DRAW
}
else if their_move == Move::ROCK {
outcome = if your_move == Move::PAPER { Outcome::WIN } else { Outcome::LOSS };
}
else if their_move == Move::PAPER {
outcome = if your_move == Move::SCISSORS { Outcome::WIN } else { Outcome::LOSS };
}
else {
outcome = if your_move == Move::ROCK { Outcome::WIN } else { Outcome::LOSS };
}
let outcome: u64 = outcome as u64;
your_score += outcome;
their_score += 6 - (outcome);
let your_move: u64 = your_move as u64;
let their_move: u64 = their_move as u64;
your_score += your_move;
their_score += their_move;
}
println!("Your score: {your_score}");
println!("Their score: {their_score}");
}