Skip to content

Instantly share code, notes, and snippets.

@bokenator
Created December 4, 2022 05:36
Show Gist options
  • Save bokenator/066f32ab5c5c64e0acc534bbfce5b6d9 to your computer and use it in GitHub Desktop.
Save bokenator/066f32ab5c5c64e0acc534bbfce5b6d9 to your computer and use it in GitHub Desktop.
use crate::common::{ get_input };
pub async fn part1() -> i32 {
let input = get_input("https://adventofcode.com/2022/day/1/input").await
.split("\n")
.collect::<Vec<&str>>().iter()
.map(|v| v.parse::<i32>().or::<i32>(Ok(0)).unwrap())
.collect::<Vec<i32>>();
let grouped_input = {
input.iter()
.fold(Vec::new(), |mut acc, &v| {
if v == 0 || acc.is_empty() {
acc.push(Vec::new())
}
acc.last_mut().unwrap().push(v);
acc
})
};
let grouped_sum = grouped_input.iter()
.map(|group| group.iter().sum::<i32>())
.collect::<Vec<i32>>();
let sorted_sum = {
let mut sorted_sum = grouped_sum.clone();
sorted_sum.sort();
sorted_sum.into_iter().rev().collect::<Vec<i32>>()
};
sorted_sum[0]
}
pub async fn part2() -> i32 {
let input = get_input("https://adventofcode.com/2022/day/1/input").await
.split("\n")
.collect::<Vec<&str>>().iter()
.map(|v| v.parse::<i32>().or::<i32>(Ok(0)).unwrap())
.collect::<Vec<i32>>();
let grouped_input = {
input.iter()
.fold(Vec::new(), |mut acc, &v| {
if v == 0 || acc.is_empty() {
acc.push(Vec::new())
}
acc.last_mut().unwrap().push(v);
acc
})
};
let grouped_sum = grouped_input.iter()
.map(|group| group.iter().sum::<i32>())
.collect::<Vec<i32>>();
let sorted_sum = {
let mut sorted_sum = grouped_sum.clone();
sorted_sum.sort();
sorted_sum.into_iter().rev().collect::<Vec<i32>>()
};
sorted_sum[0] + sorted_sum[1] + sorted_sum[2]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment