day 1
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
target
|
||||||
7
Cargo.lock
generated
Normal file
7
Cargo.lock
generated
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 3
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "aoc2022"
|
||||||
|
version = "0.1.0"
|
||||||
8
Cargo.toml
Normal file
8
Cargo.toml
Normal 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]
|
||||||
2250
inputs/day1.txt
Normal file
2250
inputs/day1.txt
Normal file
File diff suppressed because it is too large
Load Diff
7
src/bin/day01_1.rs
Normal file
7
src/bin/day01_1.rs
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
use std::fs;
|
||||||
|
use aoc2022::day01::process_part_1;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let file = fs::read_to_string("./inputs/day1.txt").unwrap();
|
||||||
|
println!("{}", process_part_1(&file));
|
||||||
|
}
|
||||||
7
src/bin/day01_2.rs
Normal file
7
src/bin/day01_2.rs
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
use std::fs;
|
||||||
|
use aoc2022::day01::process_part_2;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let file = fs::read_to_string("./input.txt").unwrap();
|
||||||
|
println!("{}", process_part_2(&file));
|
||||||
|
}
|
||||||
47
src/day01.rs
Normal file
47
src/day01.rs
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
pub fn process_part_1(input: &str) -> u32 {
|
||||||
|
let result = input
|
||||||
|
.split("\n\n")
|
||||||
|
.map(|part| part.lines().map(|snack| snack.parse::<u32>().unwrap()).sum::<u32>())
|
||||||
|
.max()
|
||||||
|
.unwrap();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn process_part_2(input: &str) -> u32 {
|
||||||
|
let mut calories = input
|
||||||
|
.split("\n\n")
|
||||||
|
.map(|part| part.lines().map(|snack| snack.parse::<u32>().unwrap()).sum::<u32>())
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
calories.sort_by(|a, b| b.cmp(a));
|
||||||
|
calories.iter().take(3).sum()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
const INPUT: &str = "1000
|
||||||
|
2000
|
||||||
|
3000
|
||||||
|
|
||||||
|
4000
|
||||||
|
|
||||||
|
5000
|
||||||
|
6000
|
||||||
|
|
||||||
|
7000
|
||||||
|
8000
|
||||||
|
9000
|
||||||
|
|
||||||
|
10000";
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn day1() {
|
||||||
|
assert_eq!(process_part_1(INPUT), 24000);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn day2() {
|
||||||
|
assert_eq!(process_part_2(INPUT), 45000);
|
||||||
|
}
|
||||||
|
}
|
||||||
1
src/lib.rs
Normal file
1
src/lib.rs
Normal file
@@ -0,0 +1 @@
|
|||||||
|
pub mod day01;
|
||||||
Reference in New Issue
Block a user