It’s linear with the size of the value of the largest input. That’s not at all the same as linear on the number of inputs, but I’m not sure what it is. And it’s got all sorts of fun race conditions.
I am excited for Advent of Code to start again!
Because I am still not as relaxed as I had hoped to be by now, I’ll be mostly doing it in Java and maybe a bit of python on the side. I recruited some of my local friends as well
Advent of Code, it’s the happiest time of the year…
Mind you I started doing this in 2018 and I think this is the hardest day 1 I’ve ever seen.
it is hard. it took me a good little while to do part1 (but I am also coding some “frameworky” stuff on the side, because last year I kept messing with things so much I lost track of already solved problems and I want to prevent that. Also I have begun automation of the download of the input (somewhat) and storage of test input and to my dismay discovered/remembered that test inputs might be different for each part.
Also I solved part 1 in a way that doesn’t help me much with part 2
A character stream filtering by ascii numbers because those were so useful last year is not useful to filter for words
But I am happy. This is the most Christmassy thing ever somehow. (I do plan on buying a chocolaty calendar later today didn’t get one before because of the cold)
I generally write separate programs for the two parts. For this one, part 1 was character by character, part 2 used a regex (hash of strings to values), tweaked after my first submission to step to the character after start of first match to look for the next one.
Apparently the first task is to check whether the stars are right.
As I recall, this doesn’t turn out well for anyone.
Not sure I’ll be playing along this year but I’ll be watching this thread to see how it goes
I did mostly the same.
But it took me a couple of attempts as there are overlaps. My first attempt was replacement and then reusing part 1.
Yes, a lot of people seem to have gone for replacement, but I assumed I’d be building a regex parser, so my lookup hash ends up containing both ("two" -> 2)
and ("2" -> 2)
.
That was a fun Day 1. I wasn’t expecting quite so many wrinkles, but I managed both parts in about 30 minutes (part 1 was about 5 minutes… part 2 revealed some lazy stream processing that I needed to deal with before actually dealing with the overlapping numbers, and then, of course, there was the issue where I didn’t notice first and last digit could be the only single digit provided, and I tried to protect against it in the part2 because I wondered if it was interfering with the results)
Day 2 was pretty easy except of course for the input parsing not fitting in a singular regex at least not with my regex skills.
Day 2 ‘Cube Conundrum’ Part 0: result=8 success=true time=19ms
Day 2 ‘Cube Conundrum’ Part 0: result=2600 success=true time=9ms
Day 2 ‘Cube Conundrum’ Part 1: result=2286 success=true time=4ms
Day 2 ‘Cube Conundrum’ Part 1: result=86036 success=true time=4ms
Part 1 took me a little while because of the input structure and my failure to get a regex working–I do have a regex somewhere in there but not for a whole line of input. The actual problem was almost trivial.
Part 2 was just writing a single giant stream expression (I can’t help it, it’s so much fun to see if it is possible):
int power() {
return Arrays.stream( Color.values() )
.mapToInt( color -> turns.stream().mapToInt( t -> t.stones().getOrDefault( color, 0 ) ).max().orElse( 0 ) )
.reduce( ( a, b ) -> a * b ).orElse( 0 );
}
This year–so far–three of my local friends are participating as well.
Yeah, Perl would happily do a multiple match and capture. But as it turned out the parsing problem was easier than I’d expected; you don’t need a level of structure for the individual turns (red = 5 green = 3), just the individual doublets of (number, colour) so you don’t need to care about whether a doublet ends with a comma, a semicolon, or something else. I haggled on this for quite a while; I don’t know the Rust regex library very well.
I should have learned nom. I should still learn nom.
I suspect what you call “stream expression” I call “functional-ish programming”. Big fan of map and filter; I don’t use reduce as often but (as here) it’s sometimes useful.
You are right. I made useless extra effort. Ah well… it happens.
Java Streams are a relatively recent addition that allow for more functionalish programming. Lambdas are also a more recentish addition without those no streams. We really like the streams API at work as well. I use it whenever I can and I am miffed when some obscure reason forces me to resort to good old loops.
I had the solution before. But now it is shorter for both parts:
boolean isPossible() {
return maxStones.entrySet().stream().allMatch( e -> e.getValue()<=e.getKey().stones );
}
int power() {
return maxStones.values().stream().reduce( (a,b)-> a*b ).orElse( 0 );
}
I’m quite new to Scala, but it feels as if it’s a language with a message for Java people converting to it: “Functional programming is great! But while you should be doing everything functionally, you may not want to make that leap all at once, so we’ll grudgingly let you do it iteratively as well.” I suspect that Java has by now caught up with its functional capabilities…
(Kotlin’s message is more “no more semicolons, this is a fun language to write in”.)
I accidentally optimized for Day 2 part 2. Normally I don’t even try, because I’m always wrong about which twist part 2 will take (and often, I am not able to anticipate whatsoever)
I feel like Day 2 was actually a touch easier than Day 1 this year.
Either I am really still so sick, that my brain isn’t working at all or are we getting started with maps and matrixes on day 3? Wasn’t last year like the first week all easy going?
I am probably overlooking some very smart way to do this but I have already imported my Matrix class and it’s helpers from last year because I can’t figure out how else to process this properly.
Day 3, I did the method that works best for me: read the problem on my phone when I first wake up, then go back to sleep. By the time I get to a keyboard I’ve got some data structures in mind.
I redid day 2 with nom to try to learn that, and I was all fired up to apply it to a new problem, but I didn’t think it was the right tool here.
Scan for numbers with regexes, because find_iter
will give me match objects with start, end and content.
Scan for symbols, well I guess I could use another regex, but I just iterated over the chars. Map(Set) for coordinates.
(All coordinates are +1 because Rust array indices are an unsigned type.)
Then iterate over the number list scanning the box round it.
Part 2, symbols are now only “*” but they each get their own ID. The Map(Set) is now a Map(Map). I keep the symbol scan and stick each number on that symbol ID’s private list, then hoik out the lists with length 2.
So this took me too long because I kept thinking: I should find a more elegant solution. But in Java when it comes to matrix style inputs… the most elegant way to deal with it is copying my Matrix class and its associates from last year.
I got so confused by coordinate manipulations last year all the time.
Actually today, once I finished programming everything, I had only one minor mistake (forgot to reset a tracking variable between loops) in the first test input everything else was correct on first try.
But this looks like a “week-end” problem to me. But maybe I let myself be distracted by last year’s experience.
Anyway, my solution.
The Meme Thang…
My feeling so far is that there’s a lot more parsiness than in previous years, and some of it (day 1) quite fiddly.
well my matrix class has a method for that…
anyway having groups of digits be numbers is just plain evil–for day 3. feel free to quote me out of context.