What are you coding?

It’s Advent of Code time again, the most magical time of the year!

I just did #1. In PostScript.

The first step is admitting you have a problem.

4 Likes

My mornings just got less productive!

1 Like

I did some math homework in college in postscript, because the most powerful computer I had access to was the laser printer. I have recovered a lot since then.

I did it in python, because I work in it, and am already set up. I expect that I will be tossing what I did today, because it’s not really extensible.
I did think about what the next questions are, but put off using some sensible data structure until then.

2 Likes

Hmm, for me it’s a bit early to speculate on what useful data structures will be. Not every year has an Intcode.

2 Likes

Back in October I had to figure out why one of our in-house programs was printing an incorrect label for one of our products. Turned out that a new customer number had been added, and the program did not have it, so I added it. Or, more appropriately, I swapped what appeared to be an incorrect number with the new one. Issue solved.

Fast forward to today and find out the other number was still in use, and it was now printing the wrong labels. So I added the customer number to the code, but also needed to update multiple lines of code for it to function correctly. Recomplied, reinstalled on the three computers that use the program, done.

Nope, now not only was the wrong template being used, but the data on it was all jumbled. Checked the code again and saw I missed adding it to the section that picks the template. Fixed that and tried again. Looked good.

Then realized the data shown was the defaults for the template, not the correctly pulled data from the database. Go back to the code again and see I missed one more area where the fields are populated from the database. Third time’s the charm, finally working.

And I get to do this again later this month when we add a new customer number who needs this same label template. I probably won’t have Covid then, so I hope to do it correctly in one go when the time comes.

5 Likes

I am glad I didn’t spend time on an elf object.

2 Likes

Can you build some tests for this?

1 Like

I did it in Java because… that’s what I do.

It took me some time to figure out how to do it in 1 single stream though.

(omitting the file operation I needed to read in the input)
    //admittedly I stole the idea of the atomic integer counter from the internet,
    //not that I didn't know it exists... 
    static AtomicInteger current = new AtomicInteger(0);
    private static int dayOne(Stream<String> input)
    {
        return input
            .map( line -> line.isEmpty() ? "skip"+current.incrementAndGet(): current.get()+"-"+line )
            .filter( l -> l.contains( "-" ) )
            .collect(Collectors.toMap( Scratch::splitKey, Scratch::splitValue, Integer::sum ))
            .values().stream().sorted(Comparator.reverseOrder()).limit( 1 ).reduce( 0, Integer::sum );
    }

    private static String splitKey( String line){
        return line.split( "-" )[0];
    }

    private static Integer splitValue( String line){
        return Integer.parseInt( line.split( "-" )[1]);
    }

I was unable to do it without the split hack though and that makes me a sad little elf.

2 Likes

Not done this before so going to give it a go with Python.

Is elegance required or is it OK if I just get the right answer? :joy:

3 Likes

Despite my late high school and early college ambitions, coding is not really my forte, so probably not. :slight_smile:

As the only person with any coding experience at my company (to my knowledge anyway), I am the one tapped to attempt alterations to our in-house programs. The original designer of these left the company a year or so before I started, and a contractor was brought in to update and modify some of them shortly thereafter, and I worked with him for a bit.

So, I middle through as best I can. Luckily this doesn’t happen very often.

image

4 Likes

I gave the link to my other techie friends as well.
And one asked “I did it in XSL, does that count?”
Obviously if you have the solution it counts.

3 Likes

Nobody has to see your code but you. I mean, if you did it by hand on an abacus that would be 100% valid.

3 Likes

and it would be an impressive feat to get that finished.

2 Likes

Not undergoing a pull request is somewhat liberating, but is going to result in messy Python :smiley:

3 Likes

I feel like I always spend time in the first part trying to optimize what I think the second part is going to ask me to do instead… and I’m pretty much always wrong.

Day 1 spoiler

I could have just made a list of sums, but I made an Elf object so that I could track the individual caloric quantities.
But, the result was that I got to sum some elves, and that’s not something one gets to do often.

Day 2 spoiler

I went out of my way to make the [A-BX-Z] decoding modular so that I could dynamically provide it in part 2. But nooooooo, once again part 2 just asks me to change the direction of the logic entirely (but I still got to reuse like 90% of my code).

I’m basically just doing all of these in Python3 with full-blown classes. Why? Because I like classes (I think they are neat) and I’m not a professional programmer so I don’t have to worry about OO going out of style.

3 Likes

I am curious to see how my XSL friend is going to fare on day 2.

Part 1 I tried to solve by converting to ASCII codes and %3… by the time Part 2 arrived, I figured out something a little shorter but it involved a piece of digital paper and a pen because I can’t do this stuff in my head.

Day 2 Part 2
Stream<String> input = Files.lines( Paths.get( "somefile.txt" ) 
int score = input.map( line -> List.of( "fnord", 
   "B X", "C X", "A X", 
   "A Y", "B Y", "C Y", 
   "C Z", "A Z", "B Z" ).indexOf( line ) )
  .reduce( 0, Integer::sum);
3 Likes

Day 2 part 2

Precalculated hash table? I don’t really get the way the JRE likes to work with streams (I’ve been playing with Kotlin).

(I’ve just checked the admin menu, and PostScript is not one of the auto-highlightable languages.)

First I build the hash table, since any “A X” will have the same score, then I read the lines and add them up.

strjoin and readlines are from my PostScript libraries so I shan’t include them here.

/scoretab << (A) 1 (B) 2 (C) 3 (X) 1 (Y) 2 (Z) 3 >> def

/scores 9 dict def

[ (X) (Y) (Z) ] {
    /state exch def
    [ (A) (B) (C) ] {
        /them exch def
        % the score for lose/draw/win
        scoretab state get 1 sub 3 mul
        % the score for what I actually played to get that result
        scoretab state get 1 add scoretab them get add 1 sub 3 mod 1 add add
        scores exch [ them state ] ( ) strjoin exch put
    } forall
} forall

/score 0 def

(input) readlines {
    scores exch get
    score add /score exch def
} forall

score ==
3 Likes

Even with spoilerblur I can tell that’s postscript.

3 Likes

Is that a question about my code?

If yes… while trying to figure out if I could find „einen geschlossenen Ausdruck“ (a closed formula?) to represent the puzzle, I noticed that for each combination of letters there is a distinct value from 1 to 9 and I simply made a list with each combo in that place. It was too easy not to do that. So yes a precalculated table. Not really fancy or elegant but certainly short. It‘s a type of „abacussing“ the problem.

Streams are great fun and if the inputs continue this way I am sure to learn a lot more about them which is neat because I can use that for work. If anyone wants a more in-depth explanation of the streams, I can post one.

4 Likes

4 Likes