This commit is contained in:
2022-12-02 20:52:02 +01:00
parent a1807826ae
commit d1d89991ab
6 changed files with 2633 additions and 2 deletions

2500
inputs/day2.txt Normal file

File diff suppressed because it is too large Load Diff

7
src/bin/day02_1.rs Normal file
View File

@@ -0,0 +1,7 @@
use aoc2022::day02::process_part_1;
use std::fs;
fn main() {
let file = fs::read_to_string("./inputs/day2.txt").unwrap();
println!("{}", process_part_1(&file));
}

7
src/bin/day02_2.rs Normal file
View File

@@ -0,0 +1,7 @@
use aoc2022::day02::process_part_2;
use std::fs;
fn main() {
let file = fs::read_to_string("./inputs/day2.txt").unwrap();
println!("{}", process_part_2(&file));
}

View File

@@ -1,7 +1,11 @@
pub fn process_part_1(input: &str) -> u32 {
let result = input
.split("\n\n")
.map(|part| part.lines().map(|snack| snack.parse::<u32>().unwrap()).sum::<u32>())
.map(|part| {
part.lines()
.map(|snack| snack.parse::<u32>().unwrap())
.sum::<u32>()
})
.max()
.unwrap();
return result;
@@ -10,7 +14,11 @@ pub fn process_part_1(input: &str) -> u32 {
pub fn process_part_2(input: &str) -> u32 {
let mut calories = input
.split("\n\n")
.map(|part| part.lines().map(|snack| snack.parse::<u32>().unwrap()).sum::<u32>())
.map(|part| {
part.lines()
.map(|snack| snack.parse::<u32>().unwrap())
.sum::<u32>()
})
.collect::<Vec<_>>();
calories.sort_by(|a, b| b.cmp(a));
calories.iter().take(3).sum()

108
src/day02.rs Normal file
View File

@@ -0,0 +1,108 @@
use std::cmp::Ordering;
use std::str::FromStr;
#[derive(Debug, PartialEq, Copy, Clone)]
enum Move {
Rock = 1,
Paper = 2,
Scissors = 3,
}
impl FromStr for Move {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"A" | "X" => Ok(Move::Rock),
"B" | "Y" => Ok(Move::Paper),
"C" | "Z" => Ok(Move::Scissors),
_ => Err("Invalid move".to_string()),
}
}
}
impl PartialOrd for Move {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
if self == &Move::Rock && other == &Move::Scissors {
return Some(Ordering::Greater);
} else if self == &Move::Scissors && other == &Move::Rock {
return Some(Ordering::Less);
}
Some((*self as u8).cmp(&(*other as u8)))
}
}
pub fn process_part_1(input: &str) -> u32 {
let result = input
.lines()
.map(|line| {
let moves = line
.split(" ")
.map(|s| s.parse::<Move>().unwrap())
.collect::<Vec<_>>();
match moves[1].partial_cmp(&moves[0]) {
Some(Ordering::Greater) => 6 + moves[1] as u32,
Some(Ordering::Equal) => 3 + moves[1] as u32,
Some(Ordering::Less) => moves[1] as u32,
None => panic!("Invalid move"),
}
})
.sum();
return result;
}
pub fn process_part_2(input: &str) -> u32 {
let result = input
.lines()
.map(|line| {
let moves = line.split(" ").collect::<Vec<_>>();
let opponent_move: Move = moves[0].parse().unwrap();
match moves[1] {
"X" => {
// We need to lose
match opponent_move {
Move::Rock => Move::Scissors as u32,
Move::Paper => Move::Rock as u32,
Move::Scissors => Move::Paper as u32,
}
}
"Y" => {
// We play the same as the opponent
3 + opponent_move as u32
}
"Z" => {
// We need to win
let response = match opponent_move {
Move::Rock => Move::Paper,
Move::Paper => Move::Scissors,
Move::Scissors => Move::Rock,
};
6 + response as u32
}
_ => panic!("Invalid move!"),
}
})
.sum();
return result;
}
#[cfg(test)]
mod tests {
use super::*;
const INPUT: &str = "A Y
B X
C Z";
#[test]
fn day1() {
assert_eq!(process_part_1(INPUT), 15);
}
#[test]
fn day2() {
assert_eq!(process_part_2(INPUT), 12);
}
}

View File

@@ -1 +1,2 @@
pub mod day01;
pub mod day02;