When we say “undefined behaviour” we really mean it.
(Also, Chivo, the Bitcoin wallet in El Salvador, had a manager* yelling at the guy working his first day to deploy it NOW, so I don’t entirely blame that guy for not spotting the bug that let people buy a bitcoin for $1 and then sell it for thousands.
* fvo “manager” = “shadowy friend of the president, who if he shoots you will suffer no consequences at all”)
Advent of Code day 3:
so basically it’s find the intersection of sets. And if I were using Python or Raku or Rust or Ruby (I think) I’d have a single operator that does that for me. But in PostScript I don’t. 
Already got a comment on the Fediverse “This is hilariously stupid”. Mission accomplished!
3 Likes
I found day 3 part 2 to be simpler than part 1 in a way.
Having fun with list comprehensions in Python.
Would be interested if anyone has a neat trick for going from “letter” to “priority”. I just wrote a function that subtracted the appropriate amount from the ASCII value.
I just used an offset on the ascii code. No neat tricks.
Today I found a bit harder than day 1 and 2. The first part I could still do in a single stream:
Day 3 Part 1
private static int dayThreePart1( Stream<String> input ) {
return input.map( backpack -> backpack.substring( backpack.length() / 2 ).chars().distinct()
.filter( type -> backpack.substring( 0, backpack.length() / 2 ).chars()
.filter( c -> c ==type ).findFirst().isPresent()) //
.map( Scratch::priority ) //ascii offset
.sum() ) //
.reduce( 0, Integer::sum );
}
private static int priority(int type) {
return type < 97 ? type - 64 + 26 : type - 96;
}
edit: there is another way to find characters in strings but in this particular case it is ugly and inelegant
For part 2 I first had to make the groups. Which I had to do in a separate stream. Meh.
It took me three attempts to get part 1 right today. There’s too much character driven streaming going on.
PS: Scratch is the scratchpad in my intelliJ that I am using for this, so basically a kind of class on which I can call method references like that because I am too lazy to do it properly .
3 Likes
Day 4, I’m starting to sense a theme.
So far I feel like I’m writing more code to parse the input than I am to work on it. Probably something I could streamline, but at the moment it just works.
Can’t help but think of C# LINQ queries as I do these puzzles. Don’t know if I want to add a second language in but at least I’d be able to use Visual Studio rather than Notepad++.
3 Likes
If I were writing in practically any other language I’d have used regexps to parse the input. The lack of them may well be what kicks me off this PostScript thing.
The hardest bit of day 4 was
remembering that there is a standard formulation for “do these ranges overlap”, but not what it actually was.
3 Likes
no theme apparent to me.
day 4 was made more difficult because I forgot the python range(x,y) returns the range x to y-1. also by a count = 1 instead of +=1 …
4 Likes
I just meant doing weird things with sets two days in a row.
3 Likes
I had very little spare time today, so I just … did the straight-forward approach.
There are probably some fancy math functions I am overlooking…
part1
private static long dayFourPart1( Stream<String> input )
{
return input
.map( Scratch::parseInput )
.filter( Objects::nonNull ) //somewhere there is either an empty line or something that didn't trigger the matcher
.filter( pair -> pair.foo.contains( pair.bar ) || pair.bar.contains( pair.foo )
).count();
}
...
boolean contains( Section other ) { return start <= other.start && end >= other.end; }
Same for part2 just adapted.
Transforming the input is the major work to do here.
Also java has got better processing these kinds of problems but I think it is not best suited to this task.
My partner said, if he had the time he would solve it all in excel and my guess is he might be faster than me with that so far.
I doubt I can find the time to learn a new language this December. I am just happy that I can solve these things with what I know now and I am using the exercise to get to know some language features better that I can actually use at work. It’s been fun so far.
3 Likes
Python makes day 4 trivial, if you don’t care about efficiency.
for line in line_data:
x, y = line.strip().split(",")
x1, x2 = x.split("-")
x = set(range(int(x1), 1 + int(x2)))
# same for y
if x.issubset(y) or y.issubset(x):
count += 1
second part, just use intersection, and check the size of resulting set
you could do the second split and set conversion in a single line, if you wanted, but that’s getting into the clever for the sake of clever territory.
4 Likes
I’m fairly underwhelmed so far this year. All of this has been extremely trivial so far. It’s making me think about going back and finishing 2021 (I got stuck on Day 16 and never went back to it). 2020 was my first year doing it and so far, it’s been my favorite by far.
Day 4
Part 1 I built the data class and used boolean logic to do it all very trivially.
Part 2 I used range generation into sets for intersection(); could have been disastrous along the lines that Roger mentioned: they could have thrown some very large integers at me… but… they didn’t.
The early ones are usually pretty lightweight, but yes, not super impressed yet.
My favourite that I’ve done was 2019, the series of Intcode problems.
1 Like
…and having just done day 5,
the input parsing was definitely the hard part in PS, but that did at least force me to think about the data structures before I got to the actual manipulation.
3 Likes
Halfway done here. The regex tip from yesterday proved invaluable today I admit. This one helps the initial setup:
".(.)...(.)...(.)...(.)...(.)...(.)...(.)...(.)...(.)."
And here is the key line from my code for part one:
boxes.get( move.to ).push( boxes.get( move.from ).pop() );
I’ll do part 2 later. I need to work for a bit on actual code
3 Likes
Oh, I did go back and do 2019; I guess I confused the two years. The Intcode stuff was definitely my favorite.
2 Likes
I found Day 5 to be particularly rewarding.
Day 5
As usual, I had written out an OO-based approach. To adapt for Part 2, I had to do a trivial rewrite of one method and add an argument where that method was being called.
1 Like
As with the previous, I managed to write something that worked for the example but fell apart with the actual input.
Today it was assuming that they wouldn’t move more than 9 crates at a time which meant having to work out regexes for parsing that bit of the input.
It is quite satisfying when the solution to part two is a very minor tweak to the part one code.
3 Likes
It is, but I couldn’t quite work it out and had to make do with a minor rewrite that doesn’t look quite so cool anymore.
PS: I was definitely tempted today to just NOT parse the first part of the input.
1 Like
I realize now that I should have just transposed the initial layout clockwise.
1 Like