diff options
author | Vincent Ambo <mail@tazj.in> | 2022-12-20T23·59+0300 |
---|---|---|
committer | clbot <clbot@tvl.fyi> | 2022-12-21T00·04+0000 |
commit | 3843b46348510665740dc412ca125a25995dc986 (patch) | |
tree | 9fb617c9428cc65d5705d4636632f9db3478867d /users/tazjin | |
parent | c3c29827ed751282863ae7a096b9380b77c1905b (diff) |
feat(tazjin/aoc2022): at least do day 1 r/5437
this way I can at least claim to not have completely ignored it this year :p Change-Id: I59ab58a05e6bc217e9c6d9fa830d321ddff71d8a Reviewed-on: https://cl.tvl.fyi/c/depot/+/7608 Autosubmit: tazjin <tazjin@tvl.su> Reviewed-by: tazjin <tazjin@tvl.su> Tested-by: BuildkiteCI
Diffstat (limited to 'users/tazjin')
-rw-r--r-- | users/tazjin/aoc2022/day1.rs | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/users/tazjin/aoc2022/day1.rs b/users/tazjin/aoc2022/day1.rs new file mode 100644 index 000000000000..078eb25f037b --- /dev/null +++ b/users/tazjin/aoc2022/day1.rs @@ -0,0 +1,27 @@ +// AoC 2022 - day 1. + +fn sum_elf(elf: &str) -> usize { + elf.lines() + .map(|s| s.parse::<usize>().expect("invalid input")) + .sum() +} + +fn group_by_elf(input: &str) -> Vec<usize> { + input.rsplit("\n\n").map(sum_elf).collect() +} + +fn top_elf(input: &str) -> usize { + group_by_elf(&input).into_iter().max().unwrap() +} + +fn top_n_elves(n: usize, input: &str) -> usize { + let mut by_elf = group_by_elf(input); + by_elf.sort_by(|a, b| b.cmp(a)); // high->low + (by_elf[..n]).iter().sum() +} + +fn main() { + let input = std::fs::read_to_string("input").expect("input should be in file named 'input'"); + println!("top elf: {}", top_elf(&input)); + println!("top 3 elves: {}", top_n_elves(3, &input)); +} |