Compare commits

...

4 Commits

9 changed files with 86 additions and 38 deletions

View File

@ -3,7 +3,7 @@
version = 3 version = 3
[[package]] [[package]]
name = "day01" name = "aoc2022"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"sorted-vec", "sorted-vec",

View File

@ -1,5 +1,5 @@
[package] [package]
name = "day01" name = "aoc2022"
version = "0.1.0" version = "0.1.0"
edition = "2021" edition = "2021"

7
2022/day02/Cargo.lock generated
View File

@ -1,7 +0,0 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "day02"
version = "0.1.0"

View File

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

View File

@ -1,4 +1,3 @@
use std::env;
use std::io; use std::io;
use std::io::prelude::*; use std::io::prelude::*;
use std::fs::File; use std::fs::File;
@ -9,14 +8,7 @@ fn read_lines(filename: &str) -> io::Result<io::Lines<io::BufReader<File>>> {
Ok(io::BufReader::new(file).lines()) Ok(io::BufReader::new(file).lines())
} }
fn get_step() -> i32 { fn update_max(max: &mut SortedVec<i32>, current: i32) {
for arg in env::args() {
if arg == "2" { return 2 }
}
return 1
}
fn update_max(max: &mut SortedVec<i32>, current: i32, step: i32) {
if max.is_empty() { if max.is_empty() {
max.insert(current); max.insert(current);
return return
@ -24,29 +16,27 @@ fn update_max(max: &mut SortedVec<i32>, current: i32, step: i32) {
if current <= max[0] { return } if current <= max[0] { return }
max.insert(current); max.insert(current);
match step { if max.len() > 3 { max.remove_index(0); }
2 => { if max.len() > 3 { max.remove_index(0); } },
_ => { max.remove_index(0); },
}
} }
fn main() -> Result<(), io::Error> { pub fn execute() -> Result<(), io::Error> {
let step = get_step();
let mut max: SortedVec<i32> = SortedVec::new(); let mut max: SortedVec<i32> = SortedVec::new();
let mut current = 0; let mut current = 0;
for line in read_lines("input.txt")? { for line in read_lines("input/day01.txt")? {
let text = line?; let text = line?;
current = match text.parse::<i32>() { current = match text.parse::<i32>() {
Ok(value) => current + value, Ok(value) => current + value,
Err(_) => { Err(_) => {
update_max(&mut max, current, step); update_max(&mut max, current);
0 0
}, },
} }
} }
println!("{}", max.iter().fold(0i32, |sum, i| sum + (*i as i32))); if let Some(x) = max.iter().last() {
println!("Step 1 solution: {x}");
}
println!("Step 2 solution: {}", max.iter().fold(0i32, |sum, i| sum + (*i as i32)));
Ok(()) Ok(())
} }

View File

@ -11,11 +11,11 @@ fn print_step(step: i8, value: u64) {
println!("Step {} solution: {}", step, value); println!("Step {} solution: {}", step, value);
} }
fn main() -> Result<(), io::Error> { pub fn execute() -> Result<(), io::Error> {
let mut score1: u64 = 0; let mut score1: u64 = 0;
let mut score2: u64 = 0; let mut score2: u64 = 0;
for line in read_lines("input.txt")? { for line in read_lines("input/day02.txt")? {
let text = line?; let text = line?;
let opp = parse_move(text.chars().nth(0).unwrap()); let opp = parse_move(text.chars().nth(0).unwrap());

73
2022/src/main.rs Normal file
View File

@ -0,0 +1,73 @@
use std::env;
use std::io;
const LATEST: u8 = 2;
mod day01;
mod day02;
// mod day03;
// mod day04;
// mod day05;
// mod day06;
// mod day07;
// mod day08;
// mod day09;
// mod day10;
// mod day11;
// mod day12;
// mod day13;
// mod day14;
// mod day15;
// mod day16;
// mod day17;
// mod day18;
// mod day19;
// mod day20;
// mod day21;
// mod day22;
// mod day23;
// mod day24;
// mod day25;
fn main() -> io::Result<()> {
let day = determine_day(LATEST);
match day {
1 => day01::execute(),
2 => day02::execute(),
// 3 => day03::execute(),
// 4 => day04::execute(),
// 5 => day05::execute(),
// 6 => day06::execute(),
// 7 => day07::execute(),
// 8 => day08::execute(),
// 9 => day09::execute(),
// 10 => day10::execute(),
// 11 => day11::execute(),
// 12 => day12::execute(),
// 13 => day13::execute(),
// 14 => day14::execute(),
// 15 => day15::execute(),
// 16 => day16::execute(),
// 17 => day17::execute(),
// 18 => day18::execute(),
// 19 => day19::execute(),
// 20 => day20::execute(),
// 21 => day21::execute(),
// 22 => day22::execute(),
// 23 => day23::execute(),
// 24 => day24::execute(),
// 25 => day25::execute(),
_ => panic!("Couldn't execute day {day}")
}
}
fn determine_day(latest: u8) -> u8 {
let mut day = latest;
for arg in env::args() {
if let Ok(x) = arg.parse::<u8>() {
day = x;
break;
}
}
return day;
}