Compare commits
3 Commits
108ef47eb5
...
7dde03f46f
| Author | SHA1 | Date | |
|---|---|---|---|
| 7dde03f46f | |||
| 9c3b97a521 | |||
| 4c040d67b3 |
1
.gitignore
vendored
1
.gitignore
vendored
@ -1 +1,2 @@
|
||||
/target
|
||||
/input
|
||||
|
||||
@ -1,7 +1,8 @@
|
||||
use aoc22::day1;
|
||||
use aoc22::util;
|
||||
|
||||
fn main() {
|
||||
let elves = day1::read_elves();
|
||||
let elves = day1::parse_elves(&util::parse_input());
|
||||
|
||||
let max = elves.iter().max().unwrap();
|
||||
println!("The elf with the most calories is carrying {} cal", max);
|
||||
|
||||
@ -4,7 +4,7 @@ use aoc22::util;
|
||||
const N_ELVES: usize = 3;
|
||||
|
||||
fn main() {
|
||||
let elves = day1::read_elves();
|
||||
let elves = day1::parse_elves(&util::parse_input());
|
||||
|
||||
let max_vals = util::max_n(elves.as_slice(), N_ELVES).unwrap();
|
||||
println!(
|
||||
|
||||
32
src/bin/d2p1.rs
Normal file
32
src/bin/d2p1.rs
Normal file
@ -0,0 +1,32 @@
|
||||
use aoc22::day2;
|
||||
use aoc22::util;
|
||||
|
||||
fn parse_choice(choice_char: char) -> day2::Choice {
|
||||
match choice_char {
|
||||
'A' => day2::Choice::Rock,
|
||||
'B' => day2::Choice::Paper,
|
||||
'C' => day2::Choice::Scissors,
|
||||
'X' => day2::Choice::Rock,
|
||||
'Y' => day2::Choice::Paper,
|
||||
'Z' => day2::Choice::Scissors,
|
||||
_ => panic!("Unknown choice {}", choice_char),
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_round(round: &(char, char)) -> day2::Round {
|
||||
day2::Round {
|
||||
opponent: parse_choice(round.0),
|
||||
own: parse_choice(round.1),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn main() {
|
||||
let rounds = day2::parse_rounds(&util::parse_input());
|
||||
|
||||
let score = rounds
|
||||
.iter()
|
||||
.map(parse_round)
|
||||
.map(|round| round.score())
|
||||
.sum::<u32>();
|
||||
println!("Total score is {}", score);
|
||||
}
|
||||
47
src/bin/d2p2.rs
Normal file
47
src/bin/d2p2.rs
Normal file
@ -0,0 +1,47 @@
|
||||
use aoc22::day2::{self, Choice, Round, RoundResult};
|
||||
use aoc22::util;
|
||||
|
||||
fn parse_choice(choice_char: char) -> Choice {
|
||||
match choice_char {
|
||||
'A' => Choice::Rock,
|
||||
'B' => Choice::Paper,
|
||||
'C' => Choice::Scissors,
|
||||
_ => panic!("Unknown choice {}", choice_char),
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_result(result_char: char) -> RoundResult {
|
||||
match result_char {
|
||||
'X' => RoundResult::Loss,
|
||||
'Y' => RoundResult::Draw,
|
||||
'Z' => RoundResult::Win,
|
||||
_ => panic!("Unknown result {}", result_char),
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_round(round: &(char, char)) -> Round {
|
||||
let opponent = parse_choice(round.0);
|
||||
let result = parse_result(round.1);
|
||||
let (win, loss) = match opponent {
|
||||
Choice::Rock => (Choice::Paper, Choice::Scissors),
|
||||
Choice::Paper => (Choice::Scissors, Choice::Rock),
|
||||
Choice::Scissors => (Choice::Rock, Choice::Paper),
|
||||
};
|
||||
let own = match result {
|
||||
RoundResult::Win => win,
|
||||
RoundResult::Draw => opponent,
|
||||
RoundResult::Loss => loss,
|
||||
};
|
||||
Round { opponent, own }
|
||||
}
|
||||
|
||||
pub fn main() {
|
||||
let rounds = day2::parse_rounds(&util::parse_input());
|
||||
|
||||
let score = rounds
|
||||
.iter()
|
||||
.map(parse_round)
|
||||
.map(|round| round.score())
|
||||
.sum::<u32>();
|
||||
println!("Total score is {}", score);
|
||||
}
|
||||
19
src/day1.rs
19
src/day1.rs
@ -1,24 +1,13 @@
|
||||
use std::io;
|
||||
|
||||
pub fn read_elves() -> Vec<u32> {
|
||||
println!("What's the calorie list?");
|
||||
let lines = io::stdin().lines();
|
||||
let mut was_empty = false;
|
||||
pub fn parse_elves(input: &String) -> Vec<u32> {
|
||||
let lines = input.lines();
|
||||
let mut elves = vec![0];
|
||||
for line in lines {
|
||||
let line_s = line.unwrap();
|
||||
if line_s.is_empty() {
|
||||
if was_empty {
|
||||
break;
|
||||
}
|
||||
if line.is_empty() {
|
||||
elves.push(0);
|
||||
was_empty = true;
|
||||
continue;
|
||||
} else {
|
||||
was_empty = false;
|
||||
}
|
||||
|
||||
let calories: u32 = line_s.parse().expect("Wanted a number");
|
||||
let calories: u32 = line.parse().expect("Wanted a number");
|
||||
let prev_calories = elves.pop().unwrap();
|
||||
elves.push(prev_calories + calories);
|
||||
}
|
||||
|
||||
72
src/day2.rs
Normal file
72
src/day2.rs
Normal file
@ -0,0 +1,72 @@
|
||||
#[derive(PartialEq, Debug, Clone, Copy)]
|
||||
pub enum Choice {
|
||||
Rock,
|
||||
Paper,
|
||||
Scissors,
|
||||
}
|
||||
|
||||
impl Choice {
|
||||
pub fn score(&self) -> u32 {
|
||||
match self {
|
||||
Choice::Rock => 1,
|
||||
Choice::Paper => 2,
|
||||
Choice::Scissors => 3,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct Round {
|
||||
pub opponent: Choice,
|
||||
pub own: Choice,
|
||||
}
|
||||
|
||||
pub enum RoundResult {
|
||||
Win,
|
||||
Draw,
|
||||
Loss,
|
||||
}
|
||||
|
||||
impl Round {
|
||||
pub fn result(&self) -> RoundResult {
|
||||
let win = match self.own {
|
||||
Choice::Rock => Choice::Scissors,
|
||||
Choice::Paper => Choice::Rock,
|
||||
Choice::Scissors => Choice::Paper,
|
||||
};
|
||||
|
||||
if self.opponent == win {
|
||||
RoundResult::Win
|
||||
} else if self.opponent == self.own {
|
||||
RoundResult::Draw
|
||||
} else {
|
||||
RoundResult::Loss
|
||||
}
|
||||
}
|
||||
|
||||
pub fn score(&self) -> u32 {
|
||||
let choice_score = self.own.score();
|
||||
let result_score = match self.result() {
|
||||
RoundResult::Win => 6,
|
||||
RoundResult::Draw => 3,
|
||||
RoundResult::Loss => 0,
|
||||
};
|
||||
|
||||
choice_score + result_score
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse_rounds(input: &String) -> Vec<(char, char)> {
|
||||
let lines = input.lines();
|
||||
let mut rounds = Vec::new();
|
||||
|
||||
for line in lines {
|
||||
let (opponent, own) = line.split_once(' ').unwrap();
|
||||
rounds.push((
|
||||
opponent.chars().next().unwrap(),
|
||||
own.chars().next().unwrap(),
|
||||
));
|
||||
}
|
||||
|
||||
rounds
|
||||
}
|
||||
@ -1,2 +1,3 @@
|
||||
pub mod day1;
|
||||
pub mod day2;
|
||||
pub mod util;
|
||||
|
||||
13
src/util.rs
13
src/util.rs
@ -1,3 +1,16 @@
|
||||
use std::env;
|
||||
use std::fs;
|
||||
|
||||
pub fn parse_input() -> String {
|
||||
let args: Vec<String> = env::args().collect();
|
||||
let input_path = args
|
||||
.get(1)
|
||||
.expect(format!("Usage: {} INPUT-FILE", args.get(0).unwrap()).as_str());
|
||||
|
||||
fs::read_to_string(input_path)
|
||||
.expect(format!("Couldn't read input file {}", input_path).as_str())
|
||||
}
|
||||
|
||||
pub fn max_n<T: Ord + Copy>(slice: &[T], n: usize) -> Result<Vec<T>, ()> {
|
||||
if n == 0 || n > slice.len() {
|
||||
return Err(());
|
||||
|
||||
Reference in New Issue
Block a user