Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
07391a83db | |||
5c63fdf294 | |||
f95bd1267f |
2
LICENSE
2
LICENSE
@ -1,4 +1,4 @@
|
||||
Copyright (c) <year> <owner>
|
||||
Copyright (c) 2022, apio
|
||||
|
||||
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
||||
|
||||
|
2251
src/input.txt
Normal file
2251
src/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
49
src/main.rs
49
src/main.rs
@ -1,3 +1,48 @@
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
// https://adventofcode.com/2022/day/1
|
||||
|
||||
const DATA: &'static str = include_str!("input.txt");
|
||||
|
||||
fn parse_calories_per_elf(elf: &str) -> i32 {
|
||||
let mut elf_sum = 0;
|
||||
for food_item in elf.split("\n")
|
||||
{
|
||||
elf_sum += food_item.parse().unwrap_or(0);
|
||||
}
|
||||
elf_sum
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
fn part1() {
|
||||
let mut highest_sum = 0;
|
||||
for elf in DATA.split("\n\n")
|
||||
{
|
||||
let elf_sum = parse_calories_per_elf(elf);
|
||||
if elf_sum > highest_sum {
|
||||
highest_sum = elf_sum;
|
||||
}
|
||||
}
|
||||
println!("The elf carrying the most calories carries {} calories", highest_sum);
|
||||
}
|
||||
|
||||
fn part2() {
|
||||
let mut elves: Vec<i32> = Vec::new();
|
||||
|
||||
for elf in DATA.split("\n\n")
|
||||
{
|
||||
let elf_sum = parse_calories_per_elf(elf);
|
||||
elves.push(elf_sum);
|
||||
}
|
||||
|
||||
elves.sort();
|
||||
|
||||
// Get the 3 elves carrying more calories
|
||||
let slice = &elves[elves.len()-3 ..];
|
||||
|
||||
println!("The 3 elves carrying the most calories carry {} calories", slice.iter().sum::<i32>());
|
||||
}
|
||||
|
||||
fn main()
|
||||
{
|
||||
//part1();
|
||||
part2();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user