moved code around so each year is a package.
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
[package]
|
||||
name="aoc2022"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
core = { path = "../core" }
|
||||
@@ -0,0 +1,24 @@
|
||||
use core::read_data;
|
||||
|
||||
fn main() {
|
||||
let binding = read_data("2022_01_data.txt");
|
||||
let lines = binding.lines();
|
||||
let mut working_elf_count = 0;
|
||||
let mut most_carried = 0;
|
||||
|
||||
for line in lines {
|
||||
if line.trim() == "" {
|
||||
// time to record this as a total
|
||||
if working_elf_count > most_carried {
|
||||
most_carried = working_elf_count;
|
||||
}
|
||||
working_elf_count = 0;
|
||||
} else {
|
||||
let next_value: u32 = line.trim().parse().unwrap();
|
||||
working_elf_count += next_value;
|
||||
println!("Read {next_value} / {working_elf_count}");
|
||||
}
|
||||
}
|
||||
println!("Most carried = {most_carried}");
|
||||
}
|
||||
// 64929
|
||||
@@ -0,0 +1,51 @@
|
||||
use core::read_data;
|
||||
|
||||
fn main() {
|
||||
let binding = read_data("2022_01_data.txt");
|
||||
let lines = binding.lines();
|
||||
let mut working_elf_count = 0;
|
||||
let mut most_carried = 0;
|
||||
let mut second_most_carried = 0;
|
||||
let mut third_most_carried = 0;
|
||||
|
||||
for line in lines {
|
||||
if line.trim().is_empty() {
|
||||
// record this elf
|
||||
if working_elf_count > most_carried {
|
||||
third_most_carried = second_most_carried;
|
||||
second_most_carried = most_carried;
|
||||
most_carried = working_elf_count;
|
||||
} else if working_elf_count > second_most_carried {
|
||||
third_most_carried = second_most_carried;
|
||||
second_most_carried = working_elf_count;
|
||||
} else if working_elf_count > third_most_carried {
|
||||
third_most_carried = working_elf_count;
|
||||
}
|
||||
working_elf_count = 0;
|
||||
} else {
|
||||
let next_value: u32 = line.trim().parse().unwrap();
|
||||
working_elf_count += next_value;
|
||||
}
|
||||
}
|
||||
|
||||
// flush last elf in case file doesn't end with blank line
|
||||
if working_elf_count > 0 {
|
||||
if working_elf_count > most_carried {
|
||||
third_most_carried = second_most_carried;
|
||||
second_most_carried = most_carried;
|
||||
most_carried = working_elf_count;
|
||||
} else if working_elf_count > second_most_carried {
|
||||
third_most_carried = second_most_carried;
|
||||
second_most_carried = working_elf_count;
|
||||
} else if working_elf_count > third_most_carried {
|
||||
third_most_carried = working_elf_count;
|
||||
}
|
||||
}
|
||||
|
||||
println!(
|
||||
"Most carried = {most_carried} + {second_most_carried} + {third_most_carried} = {}",
|
||||
most_carried + second_most_carried + third_most_carried
|
||||
);
|
||||
}
|
||||
// 193697
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
use core::read_data;
|
||||
use crate::RPSOutcome::{Loss, Tie, Win};
|
||||
use crate::RPSPlays::{Paper, Rock, Scissors};
|
||||
|
||||
enum RPSOutcome {
|
||||
Win,
|
||||
Loss,
|
||||
Tie
|
||||
}
|
||||
|
||||
enum RPSPlays {
|
||||
Rock,
|
||||
Paper,
|
||||
Scissors
|
||||
}
|
||||
|
||||
fn str_to_rps(input: &str) -> RPSPlays {
|
||||
match input {
|
||||
"A" | "X" => { Rock },
|
||||
"B" | "Y" => { Paper },
|
||||
"C" | "Z" => { Scissors },
|
||||
_ => { unreachable!("Invalid Conversion"); }
|
||||
}
|
||||
}
|
||||
|
||||
fn play_to_score(input: &str) -> u32 {
|
||||
match str_to_rps(input) {
|
||||
Rock => 1,
|
||||
Paper => 2,
|
||||
Scissors => 3
|
||||
}
|
||||
}
|
||||
|
||||
fn did_i_win(them: &str, me: &str) -> RPSOutcome {
|
||||
match (str_to_rps(them), str_to_rps(me)) {
|
||||
(Rock, Rock) | (Paper, Paper) | (Scissors, Scissors) => Tie,
|
||||
(Rock, Paper) | (Paper, Scissors) | (Scissors, Rock) => Win,
|
||||
(Rock, Scissors) | (Paper, Rock) | (Scissors, Paper) => Loss,
|
||||
_ => { unreachable!("Invalid Game"); }
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut running_score = 0;
|
||||
let binding = read_data("2022_02_data.txt");
|
||||
let lines = binding.lines();
|
||||
for line in lines {
|
||||
let mut this_round = 0;
|
||||
let (them, me) = line.split_once(" ").unwrap();
|
||||
this_round = play_to_score(me);
|
||||
match did_i_win(them, me) {
|
||||
Win => {
|
||||
this_round += 6;
|
||||
}
|
||||
Loss => {
|
||||
// nothing.
|
||||
}
|
||||
Tie => {
|
||||
this_round += 3;
|
||||
}
|
||||
}
|
||||
running_score += this_round;
|
||||
}
|
||||
println!("Final = {running_score}");
|
||||
}
|
||||
// 14531
|
||||
@@ -0,0 +1,117 @@
|
||||
use core::read_data;
|
||||
use crate::RPSOutcome::{Loss, Tie, Win};
|
||||
use crate::RPSPlays::{Paper, Rock, Scissors};
|
||||
|
||||
#[derive(Debug)]
|
||||
enum RPSOutcome {
|
||||
Win,
|
||||
Loss,
|
||||
Tie,
|
||||
}
|
||||
#[derive(Debug, Clone)]
|
||||
enum RPSPlays {
|
||||
Rock,
|
||||
Paper,
|
||||
Scissors,
|
||||
}
|
||||
|
||||
fn str_to_rps(input: &str) -> RPSPlays {
|
||||
println!("STR_TO_RPS -> {input}");
|
||||
match input {
|
||||
"A" => { Rock }
|
||||
"B" => { Paper }
|
||||
"C" => { Scissors }
|
||||
_ => { unreachable!("Invalid Conversion"); }
|
||||
}
|
||||
}
|
||||
|
||||
fn play_to_score(input: &str) -> u32 {
|
||||
match str_to_rps(input) {
|
||||
Rock => 1,
|
||||
Paper => 2,
|
||||
Scissors => 3
|
||||
}
|
||||
}
|
||||
|
||||
fn need_to_loose(them: RPSPlays) -> RPSPlays {
|
||||
match them {
|
||||
Rock => { Scissors }
|
||||
Paper => { Rock }
|
||||
Scissors => { Paper }
|
||||
}
|
||||
}
|
||||
|
||||
fn need_to_tie(them: RPSPlays) -> RPSPlays {
|
||||
them
|
||||
}
|
||||
|
||||
fn need_to_win(them: RPSPlays) -> RPSPlays {
|
||||
let result = match them {
|
||||
Rock => { Paper }
|
||||
Paper => { Scissors }
|
||||
Scissors => { Rock }
|
||||
};
|
||||
println!("Checking on {them:?} to win / {result:?}");
|
||||
result
|
||||
}
|
||||
|
||||
fn str_to_desired_outcome(input: &str) -> RPSOutcome {
|
||||
match input {
|
||||
"X" => { Loss }
|
||||
"Y" => { Tie }
|
||||
"Z" => { Win }
|
||||
_ => { unreachable!("Invalid Requested outcome {}", input); }
|
||||
}
|
||||
}
|
||||
|
||||
fn win_loose_tie_score(them: RPSPlays, me: RPSPlays) -> u32 {
|
||||
match (them, me) {
|
||||
(Rock, Rock) | (Paper, Paper) | (Scissors, Scissors) => 3,
|
||||
(Rock, Scissors) | (Paper, Rock) | (Scissors, Paper) => 0,
|
||||
_ => 6
|
||||
}
|
||||
}
|
||||
|
||||
fn points_for_play(to_score: RPSPlays) -> u32 {
|
||||
match to_score {
|
||||
Rock => { 1 }
|
||||
Paper => { 2 }
|
||||
Scissors => { 3 }
|
||||
}
|
||||
}
|
||||
|
||||
fn score_game(them: RPSPlays, me: RPSPlays) -> u32 {
|
||||
win_loose_tie_score(them, me.clone()) + points_for_play(me)
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let binding = read_data("2022_02_data.txt");
|
||||
let mut running_total = 0;
|
||||
let mut goal = Win;
|
||||
let lines = binding.lines();
|
||||
for line in lines {
|
||||
println!("__________STARTING TO PROCESS ||[{line}]||");
|
||||
let (them, me) = line.split_once(" ").unwrap();
|
||||
let them_rps = str_to_rps(them);
|
||||
println!("MATCHED OUT TO ||{:?}||{:?}", them_rps, str_to_desired_outcome(me));
|
||||
let my_play = match str_to_desired_outcome(me) {
|
||||
Win => {
|
||||
need_to_win(them_rps.clone())
|
||||
}
|
||||
Loss => {
|
||||
goal = Loss;
|
||||
need_to_loose(them_rps.clone())
|
||||
}
|
||||
Tie => {
|
||||
goal = Tie;
|
||||
need_to_tie(them_rps.clone())
|
||||
}
|
||||
};
|
||||
let game_score = score_game(them_rps.clone(), my_play.clone());
|
||||
println!("They play {:?} I play {:?} to {:?}", them_rps, my_play, goal);
|
||||
println!("SCORE = {}", game_score);
|
||||
running_total += game_score;
|
||||
}
|
||||
println!("Total = {running_total}");
|
||||
}
|
||||
// 11258
|
||||
@@ -0,0 +1,43 @@
|
||||
use std::collections::HashSet;
|
||||
use std::io::BufRead;
|
||||
use core::read_data;
|
||||
|
||||
|
||||
fn char_to_value(input: char) -> u32 {
|
||||
let mut return_value = 0;
|
||||
if input.is_ascii_alphabetic() {
|
||||
let as_integer: u8 = input.to_string().bytes().collect::<Vec<_>>()[0];
|
||||
if as_integer >= 97 && as_integer <= 122 {
|
||||
return_value = as_integer - 96;
|
||||
} else {
|
||||
return_value = as_integer - 38; // 64 - 26
|
||||
}
|
||||
}
|
||||
return_value as u32
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut running_total = 0;
|
||||
let binding = read_data("2022_03_data.txt");
|
||||
let lines = binding.lines();
|
||||
|
||||
for line in lines {
|
||||
let line_len = line.len();
|
||||
let (bag1, bag2) = line.split_at(line_len / 2);
|
||||
println!("||{line}|| became ||{bag1}|| and ||{bag2}||");
|
||||
let (mut working_bag1, mut working_bag2) = (HashSet::new(), HashSet::new());
|
||||
for current_char in bag1.chars() {
|
||||
working_bag1.insert(current_char);
|
||||
}
|
||||
for current_char in bag2.chars() {
|
||||
working_bag2.insert(current_char);
|
||||
}
|
||||
|
||||
let duplicates: char = working_bag1.intersection(&working_bag2).cloned().collect::<Vec<_>>()[0];
|
||||
|
||||
println!("DUPES = {}", duplicates);
|
||||
running_total += char_to_value(duplicates);
|
||||
}
|
||||
println!("Final = {running_total}");
|
||||
}
|
||||
// 7848 too low
|
||||
@@ -0,0 +1,10 @@
|
||||
use core::read_data;
|
||||
|
||||
fn main() {
|
||||
let binding = read_data("2022_03_data.txt");
|
||||
let lines: Vec<&str> = binding.lines().collect();
|
||||
for chunk in lines.chunks(3) {
|
||||
println!("{:?}", chunk);
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user