diff --git a/.gitignore b/.gitignore index d5b75b4..87e021d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ -/target +target/ input +testinput diff --git a/day01/Cargo.lock b/day01/Cargo.lock new file mode 100644 index 0000000..683c0b9 --- /dev/null +++ b/day01/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "day01" +version = "0.1.0" diff --git a/day01/Cargo.toml b/day01/Cargo.toml new file mode 100644 index 0000000..5a61072 --- /dev/null +++ b/day01/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "day01" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/day01/src/main.rs b/day01/src/main.rs new file mode 100644 index 0000000..f323724 --- /dev/null +++ b/day01/src/main.rs @@ -0,0 +1,30 @@ +use std::io::{self, BufRead}; + +fn main() { + let lines = io::stdin().lock().lines().map(|l| l.unwrap()); + let mut sum = 0; + let mut max = [0, 0, 0]; + + for line in lines { + if let Ok(num) = line.parse::() { + sum += num; + } else { + match (sum > max[0], sum > max[1], sum > max[2]) { + (_, _, true) => { + max.rotate_left(1); + max[2] = sum; + } + (_, true, false) => { + max[0] = max[1]; + max[1] = sum; + } + (true, false, false) => { + max[0] = sum; + } + _ => (), + } + sum = 0; + } + } + println!("P1: {} P2: {}", max[2], max.iter().sum::()); +} diff --git a/fetchinput.sh b/fetchinput.sh index 7160b8f..5589903 100755 --- a/fetchinput.sh +++ b/fetchinput.sh @@ -2,7 +2,7 @@ # AOC_SESSION_COOKIE="samplecookie" ./fetchinput.sh 13 # defaults to current day unless specified day is passed in -DAY=`date +%d` +DAY=$(printf "%01d" `date +%d`) ! [ "$1" = "" ] && printf "%d" $1 >/dev/null 2>&1 && DAY=$1 [ "$AOC_SESSION_COOKIE" = "" ] && echo "\$AOC_SESSION_COOKIE not set" && exit 1