24
votes
Day 1: Calorie Counting
Today's problem description: https://adventofcode.com/2022/day/1
Please post your solutions in your own top-level comment. Here's a template you can copy-paste into your comment to format it nicely, with the code collapsed by default inside an expandable section with syntax highlighting (you can replace python
with any of the "short names" listed in this page of supported languages):
<details>
<summary>Part 1</summary>
```python
Your code here.
```
</details>
Another year of Sheets because I didn't learn shit in any of the previous years. Anyway, I first did this with a vlookup with a filter all wrapped with a QUERY -- but that is ugly. Not that this is much prettier.
both parts
``` =ARRAYFORMULA( BYCOL( LARGE( BYROW( SPLIT(TRANSPOSE(SPLIT(CONCATENATE(A1:A&"|"),"||",FALSE,TRUE)),"|"), LAMBDA(x,SUM(x))), {1,1;1,2;1,3}), LAMBDA(x,SUM(UNIQUE(x))))) ```I'm sure you learned plenty, but perhaps you're just a masochist? ;)
haha. i’ve leaned more spreadsheet stuff. i was hoping to learn a little python or something
I'm using Python again this year, but going for a better balance of concision and readability. Last year I code golfed my solutions, rendering them totally unreadable to present me. I might also attempt a handful of the days in either Haskell or a Scheme, but we'll see if that pans out.
Part 1
Part 2
The main reason (after lack of ability) I can't get into code golf.
I'd try to improve some code and get completely lost trying to decrypt my thinking and not understand it from the code.
Function composition ftw
Santa's little helpers
Slurping data
Barfing result
This would have been so much more elegant in Haskell or using the threading library.
Speedrun
Once I have a solution, I like to make them as fast as I can.
Here's the 1st improvement ~20% elapsed time.
That's... much more elegant than what I did.
From how the story went, I was sure there was going to be something more difficult for part two, so I set it up in a broader way, which then turned out to be unnecessary...
Input parsing
Given that, parts one was actually a simple one-liner.
Part 1
Part two was a bit more complicated, because I have to tell it how to sort the elves, but it's not rocket science.
Part 2
For starting with racket not all that long ago, I'm quite happy about how it turned out.
That looks elegant to me, just a different approach. I usually use iterators and sequences because I think they are easier to reason about when the problem is more complex.
It's a unique thing about AoC, it makes you second guess yourself in part 1 for what's coming in part 2. Sometimes the gamble pays off, sometimes it doesn't.
small tip
The
#:key
selector is used to extract a value for comparison - perfect for this situation:Oh, nice! That does make it much more readable. xD
Thank you so much!
Decided to try out nim literally minutes before I started (I've done like half of nim by example before and no other nim) so it was actually a bit of a struggle. My solution kinda sucks, though.
Both parts
Nice, love to see Nim! A few tips:
var elves: seq[int]
works just as well1..3
instead ofcountup(1, 3)
, or1 ..< 4
import std/[strutils, sequtils]
sequtils
is your friend for Advent of Code - there are so many helpful functions in theresequtils
' functional programming functions: try out the anonymous function syntax instd/sugar
Thank you!! That's super helpful :) I admittedly didn't see this until after I finished day 2, but I'll keep all of that in mind for the rest of the challenges (and if I ever do any non-aoc nim programming)
I am doing these challenges in bash this year.
a+b
Elixir
Doing Elixir again this year. I usually last until the problems get into pathfinding algorithms and such.
I use a project that provides some conveniences for fetching the problem inputs, so each solution I post will always come in the form of a module with two functions,
part1
andpart2
, which receive the input string.Both parts
Working in Clojure this time around, also probably not going to try and stay up all night so much this year.
Parts 1 & 2
Last year I used this to try to learn Rust and ended up giving up around Day 11 (oddly enough, right around my last day of work for the year)
This year I've decided to stick with Python as I'm pretty familiar with the language and I'm hoping it'll keep me going through the end.
Full Solution
I probably could have done it simpler, but my goal isn't to try to find the fanciest solution, just a simple one that would be reasonable to use in a professional setting.
Python, nothing special.
It looks like we landed on a broadly similar approach, although yours is considerably neater I think!
Answer
solution in Ruby
Both parts
I'm on a journey to start writing services in node, so that means learning Javascript. Thankfully coming in new, I can try to avoid all the broke old methods. Advice welcome.
My biggest learning moment was that parseInt() was critical to avoid string concatenation.
Code
I think strict mode is used by default in modules (which node should pick up on since you used
import
)https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode#strict_mode_for_modules
You can double-check this by adding a line like
undefinedVariable = 1;
to the file and seeing if it throws an error (strict mode) or not (sloppy mode).Good to know!
The
elf_number
variable is unnecessary, and I'd usually recommend against using an explicit index variable like that if you don't need it because it's an easy source of errors in more complex programs if it gets out of sync with the list size (as it does after you start popping items from the list) and you keep using it. You can append an item to the end of a list with the push method (elf_list.push(0)
), and access the last item of the list withelf_list[elf_list.length-1]
.I'm giving Haxe another try and using it for this advent of code (at least for now).
I made a comment in early 2021 when I last tried it out and the reasons I gave still hold true.
I think the reason I didn't end up doing much with it last time was mainly due to lacking documentation (in the project itself and third party libraries), so hopefully that's gotten a bit better since then.
Code
Edit: I added a
Common
module with various extensions that I need that aren't included in the standard library.Right now it's a few iterator/math functions.
I went back and changed my solution for this day to be a bit more compact/use the
Common
module.Day1
Common
I had hoped to try out Rust this year, but I haven't gotten around to actually learning much of it so I settled on Python, since my new work uses it a lot and it would be good to brush up. I am also going hard on organizing code, adding at least semi real tests for the provided test cases, etc. Anyway, solution:
Parts 1 and 2
And the
common
file which will hopefully fill out a bit as time goes on:POSIX AWK as usual. Maybe I'll try another language mid-way.
Part 1
Part 2
Rust
Rust
All my Javascript friends do oneliners with map and reduce, so I had to give it a try in python:
Python oneliners
Decided to use rust. Ive gon through rust by example and the rust book, so I 'know' rust, but I dont really feel fully comfortable in it, and I find Advent Of Code a really good way to get comfortable with a language.
Driver
Parser
Part 1
Part 2
Here's my TypeScript solution
Part 1
Part 2
I'm using Rust this year, and hoping to keep it
std
-only throughout the month.Part 1
Part 2