Day 1 Solution

This commit is contained in:
Brandon Scott 2022-12-02 14:19:49 -06:00
parent 15565328f8
commit 850a13b6fc
6 changed files with 2313 additions and 0 deletions

14
rust/.gitignore vendored Normal file
View File

@ -0,0 +1,14 @@
# Generated by Cargo
# will have compiled files and executables
debug/
target/
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Cargo.lock
# These are backup files generated by rustfmt
**/*.rs.bk
# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb

8
rust/Cargo.toml Normal file
View File

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

8
rust/make.sh Executable file
View File

@ -0,0 +1,8 @@
#!/bin/bash
DIR="$(dirname "$0")"
if cargo "$@"; then
[ -d "$DIR/target/debug" ] && cp -r "$DIR/resources" "$DIR/target/debug/resources"
[ -d "$DIR/target/release" ] && cp -r "$DIR/resources" "$DIR/target/release/resources"
fi

2242
rust/resources/day1input.txt Normal file

File diff suppressed because it is too large Load Diff

36
rust/src/day1.rs Normal file
View File

@ -0,0 +1,36 @@
use std::fs::File;
use std::io::BufReader;
use std::io::BufRead;
pub fn run() {
let file = File::open("resources/day1input.txt").expect("Input file not found.");
let reader = BufReader::new(file);
let mut calorie_totals: Vec<i64> = Vec::new();
let mut current_elf_calories = 0;
for line in reader.lines() {
let calories = line.unwrap().parse::<i64>().unwrap_or(0);
println!("Input calories: {:?}", calories);
if calories == 0 {
println!("Adding to current elf total: {:?}", current_elf_calories);
calorie_totals.push(current_elf_calories);
current_elf_calories = 0;
} else {
current_elf_calories += calories;
}
}
if current_elf_calories > 0 {
println!("Adding to current elf total: {:?}", current_elf_calories);
calorie_totals.push(current_elf_calories);
current_elf_calories = 0;
}
calorie_totals.sort();
calorie_totals.reverse();
assert!(calorie_totals.len() >= 3);
println!("Day 1 Part A Answer: {}", calorie_totals[0]);
println!("Day 1 Part B Answer: {}", calorie_totals[0] + calorie_totals[1] + calorie_totals[2]);
}

5
rust/src/main.rs Normal file
View File

@ -0,0 +1,5 @@
mod day1;
fn main() {
day1::run();
}