[TASK] Solved Day 2
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
<component name="ProjectRunConfigurationManager">
|
<component name="ProjectRunConfigurationManager">
|
||||||
<configuration default="false" name="Run-aoc2022-bench" type="CargoCommandRunConfiguration" factoryName="Cargo Command">
|
<configuration default="false" name="Run-aoc2022-bench" type="CargoCommandRunConfiguration" factoryName="Cargo Command">
|
||||||
<option name="command" value="run --package advent-of-code-2022-rust --bin advent-of-code-2022-rust --release -- -b" />
|
<option name="command" value="run --package advent-of-code-2022-rust --bin advent-of-code-2022-rust --release -- -b 1000" />
|
||||||
<option name="workingDirectory" value="file://$PROJECT_DIR$" />
|
<option name="workingDirectory" value="file://$PROJECT_DIR$" />
|
||||||
<option name="channel" value="DEFAULT" />
|
<option name="channel" value="DEFAULT" />
|
||||||
<option name="requiredFeatures" value="true" />
|
<option name="requiredFeatures" value="true" />
|
||||||
2500
input/day2.txt
Normal file
2500
input/day2.txt
Normal file
File diff suppressed because it is too large
Load Diff
3
input/day2_example.txt
Normal file
3
input/day2_example.txt
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
A Y
|
||||||
|
B X
|
||||||
|
C Z
|
||||||
95
src/day2.rs
95
src/day2.rs
@@ -1,20 +1,100 @@
|
|||||||
use std::cmp::Reverse;
|
|
||||||
|
|
||||||
use crate::day_solver::DaySolver;
|
use crate::day_solver::DaySolver;
|
||||||
|
|
||||||
use super::util;
|
use super::util;
|
||||||
|
|
||||||
|
const ROCK: u8 = 0;
|
||||||
|
const PAPER: u8 = 1;
|
||||||
|
const SCISSORS: u8 = 2;
|
||||||
|
|
||||||
|
#[derive(Copy, Clone, PartialEq, Debug)]
|
||||||
|
struct RockPaperScissorsGame {
|
||||||
|
elf: char,
|
||||||
|
you: char
|
||||||
|
}
|
||||||
|
|
||||||
|
impl RockPaperScissorsGame {
|
||||||
|
|
||||||
|
fn elf_choice(&self) -> u8 {
|
||||||
|
return (self.elf as u8) - ('A' as u8);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn you_choice_part1(&self) -> u8 {
|
||||||
|
return (self.you as u8) - ('X' as u8);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn choice_score(choice: u8) -> u64 {
|
||||||
|
return (choice + 1) as u64;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn calc_score_part1(&self) -> u64 {
|
||||||
|
return RockPaperScissorsGame::calc_score(self.you_choice_part1(), self.elf_choice());
|
||||||
|
}
|
||||||
|
|
||||||
|
fn calc_score(you_choice:u8, elf_choice: u8) -> u64 {
|
||||||
|
|
||||||
|
let choice_score = RockPaperScissorsGame::choice_score(you_choice);
|
||||||
|
return if you_choice == elf_choice {
|
||||||
|
// tie
|
||||||
|
choice_score + 3
|
||||||
|
} else if (you_choice == ROCK && elf_choice == SCISSORS) ||
|
||||||
|
(you_choice == PAPER && elf_choice == ROCK) ||
|
||||||
|
(you_choice == SCISSORS && elf_choice == PAPER)
|
||||||
|
{
|
||||||
|
// you win
|
||||||
|
choice_score + 6
|
||||||
|
} else {
|
||||||
|
// you lose
|
||||||
|
choice_score
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn calc_score_part2(&self) -> u64 {
|
||||||
|
|
||||||
|
let you_choice = if self.you == 'X' {
|
||||||
|
// We need to lose
|
||||||
|
match self.elf_choice() {
|
||||||
|
ROCK => SCISSORS,
|
||||||
|
PAPER => ROCK,
|
||||||
|
SCISSORS => PAPER,
|
||||||
|
_ => panic!("Invalid choice: {}", self.elf_choice())
|
||||||
|
}
|
||||||
|
} else if self.you == 'Y' {
|
||||||
|
// We need a tie
|
||||||
|
self.elf_choice()
|
||||||
|
} else {
|
||||||
|
// We need to win
|
||||||
|
match self.elf_choice() {
|
||||||
|
ROCK => PAPER,
|
||||||
|
PAPER => SCISSORS,
|
||||||
|
SCISSORS => ROCK,
|
||||||
|
_ => panic!("Invalid choice: {}", self.elf_choice())
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return RockPaperScissorsGame::calc_score(you_choice, self.elf_choice());
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub struct Day2 {
|
pub struct Day2 {
|
||||||
|
games: Vec<RockPaperScissorsGame>
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Day2 {
|
impl Day2 {
|
||||||
|
|
||||||
pub fn create() -> Self {
|
pub fn create() -> Self {
|
||||||
// let lines = util::read_file("input/dayX_example.txt");
|
// let lines = util::read_file("input/day2_example.txt");
|
||||||
let lines = util::read_file("input/dayX.txt");
|
let lines = util::read_file("input/day2.txt");
|
||||||
|
|
||||||
// Put the input into the day struct
|
// Put the input into the day struct
|
||||||
return Day2 {}
|
return Day2 {
|
||||||
|
games: lines.iter().map(|s|
|
||||||
|
RockPaperScissorsGame {
|
||||||
|
elf: s.chars().next().unwrap(),
|
||||||
|
you: s.chars().nth(2).unwrap() })
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -22,10 +102,11 @@ impl DaySolver for Day2 {
|
|||||||
|
|
||||||
|
|
||||||
fn solve_part1(&mut self) -> String {
|
fn solve_part1(&mut self) -> String {
|
||||||
return 0.to_string();
|
|
||||||
|
return self.games.iter().map(|g| g.calc_score_part1()).sum::<u64>().to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn solve_part2(&mut self) -> String {
|
fn solve_part2(&mut self) -> String {
|
||||||
return 0.to_string();
|
return self.games.iter().map(|g| g.calc_score_part2()).sum::<u64>().to_string()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user