As 2023 Advent of Code is approaching fast, I thought I’d revisit my 2022 entries, and I realised a good focus would be to post one a day during November. No guarantees as to the quality of the algorithms used, but hopefully people will find the code readable and interesting. If anyone has questions or ideas for improvement, I’d love to hear them.
They were all written in Dart, and I will modify each one to allow it to run online in the browser. Some of the code may look a little odd as I had to inline the functionality from some libraries that I used as DartPad doesn’t support them all.
Anyway, here’s the core of my response to Day 1. The full code can be read and run by following the link above.
int part1(String lines) => totalCalories(lines).first;
int part2(String lines) => totalCalories(lines).take(3).sum;
Iterable totalCalories(String lines) => lines
.split('\n\n')
.map((e) => e.split('\n').map(int.parse).sum)
.sorted(descending);
You must log in or register to comment.