Day 1 solution

This commit is contained in:
Savanni D'Gerinel 2022-12-01 09:46:04 -05:00
parent 683d7ef7a3
commit 0340f23360
4 changed files with 2355 additions and 36 deletions

View File

@ -17,11 +17,11 @@
},
"nixpkgs": {
"locked": {
"lastModified": 1669418739,
"narHash": "sha256-T86oFvcUIRwHWBWUt7WjaP4BP/3lDGbv5AppQSI1FkI=",
"lastModified": 1669764884,
"narHash": "sha256-1qWR/5+WtqxSedrFbUbM3zPMO7Ec2CGWaxtK4z4DdvY=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "695b3515251873e0a7e2021add4bba643c56cde3",
"rev": "0244e143dc943bcf661fdaf581f01eb0f5000fcf",
"type": "github"
},
"original": {
@ -52,7 +52,7 @@
"nixpkgs": "nixpkgs_2"
},
"locked": {
"narHash": "sha256-xMOq9ifVo63/aBA1VIHQQ4riuRJGyup7F1orjekAwpk=",
"narHash": "sha256-/ar+cbAKAxd2Ng9b7EhrIMz9CP353RbmLecvyOidyUM=",
"type": "tarball",
"url": "https://github.com/oxalica/rust-overlay/archive/master.tar.gz"
},

File diff suppressed because it is too large Load Diff

View File

@ -1,25 +1,100 @@
const PART1: &str = include_str!("../data/day1a.txt");
pub fn part1() -> String {
let lines = PART1.lines();
let depths: Vec<i32> = lines.map(|l| l.parse::<i32>().unwrap()).collect();
let depths_a = depths.iter();
let depths_b = depths.iter().skip(1);
let depth_cmp = depths_a.zip(depths_b);
let increases = depth_cmp.fold(0, |cur, (a, b)| {
println!("{} < {}? {}", a, b, a < b);
if a < b {
cur + 1
} else {
cur
}
});
format!("{}", increases)
format!("{}", max_calories(sum_calories(input_data(PART1))).1)
}
pub fn part2() -> String {
"no response".to_owned()
format!("{}", max_three_calories(sum_calories(input_data(PART1))))
}
fn input_data(data: &str) -> Vec<Vec<u32>> {
data.lines()
.collect::<Vec<&str>>()
.split(|l| *l == "")
.map(|v| {
v.into_iter()
.map(|element| element.parse::<u32>().unwrap())
.collect::<Vec<u32>>()
})
.collect::<Vec<Vec<u32>>>()
}
fn sum_calories(food_lists: Vec<Vec<u32>>) -> Vec<(usize, u32)> {
food_lists
.into_iter()
.map(|list| list.into_iter().fold(0, |cur, val| cur + val))
.enumerate()
.collect::<Vec<(usize, u32)>>()
}
fn elf_with_most_calories(batch_calories: Vec<(usize, u32)>) -> usize {
let (idx, _) = max_calories(batch_calories);
// Elves are 1-indexed
return idx + 1;
}
fn max_calories(mut batch_calories: Vec<(usize, u32)>) -> (usize, u32) {
batch_calories.sort_by(|(_, val1), (_, val2)| val1.cmp(val2));
batch_calories.reverse();
batch_calories[0]
}
fn max_three_calories(mut batch_calories: Vec<(usize, u32)>) -> u32 {
batch_calories.sort_by(|(_, val1), (_, val2)| val1.cmp(val2));
batch_calories.reverse();
batch_calories
.into_iter()
.take(3)
.fold(0, |cur, (_, val)| cur + val)
}
#[cfg(test)]
mod test {
use super::*;
const TEST_DATA1: &str = "1000
2000
3000
4000
5000
6000
7000
8000
9000
10000";
#[test]
fn find_most_elf() {
let input = input_data(TEST_DATA1);
let food_batches = sum_calories(input);
assert_eq!(
food_batches,
vec![6000, 4000, 11000, 24000, 10000]
.into_iter()
.enumerate()
.collect::<Vec<(usize, u32)>>()
);
assert_eq!(elf_with_most_calories(food_batches.clone()), 4);
assert_eq!(max_calories(food_batches), (3, 24000));
}
#[test]
fn batch_with_one_element() {
let input = input_data("50057");
let food_batches = sum_calories(input);
assert_eq!(food_batches, vec![(0, 50057)]);
}
#[test]
fn find_max_three() {
assert_eq!(
max_three_calories(sum_calories(input_data(TEST_DATA1))),
45000
);
}
}

View File

@ -2,13 +2,12 @@ mod day1;
fn main() {
let day = std::env::args().skip(1).next();
println!("day: {:?}", day);
let result = match day.as_ref().map(|v| v.as_ref()) {
Some("day1a") => day1::part1(),
Some("day1b") => day1::part2(),
Some("1a") => day1::part1(),
Some("1b") => day1::part2(),
_ => panic!("unrecognized day"),
};
println!("{:?} Result: {}", day, result);
println!("[{}] {}", day.unwrap(), result);
}