Advent of Code - adventofcode.com

Posted on
Page
of 21
  • Added some (plausibly not very idiomatic, I'm learning the language) Rust solutions to the first 7 days https://gist.github.com/wence-/7d2a722fba29c94149a75beebfd5333c

  • Today I learned about deep copying and why in the fuck things were changing when I didn't them to change.

    https://github.com/TijmenK/AoC2020/blob/main/day8.py

  • Day 8:
    https://docs.google.com/spreadsheets/d/1QLnJ--jc1d18qHiwcO1QXnqyaoG078-09HlYiXvoA_0/edit#gid=1072555748

    Safari killed the tab for using "Significant Memory" three times after I filled out the final sheet where I execute the program 500 times. Chrome didn't do much better. Got there in the end though.

  • Day 8 - last couple of days i had gotten lazy, looked at a few model solutions before writing my own, so today i wrote out my pseudo-code and cracked on without looking at any solutions or googling any functions etc. Result is probably not the neatest code but got the answer correct.

    One question - i couldn't figure out how to recursively operate without the below, which seems kind of stupid given i don't exit with the flag being true. maybe something like putting it all into a recursive function would have worked? i might try re-writing that later.

    flag = False
    accum = 0
    
    # part1
    while not flag:
        if bootlist[line] > 0: break
        if boot[line][0] == 'jmp': line += int(boot[line][1])
        elif boot[line][0] == 'nop': line += 1
        elif boot[line][0] == 'acc':
            accum += int(boot[line][1])
            bootlist[line] = accum
            line += 1
    

    https://github.com/lewfowls/AoC2020/blob/main/day8Code.py

  • Just reading your code - in your part 2 you have 'if line[0] = jmp, then change to nop', have you missed the vice versa as well or have i misunderstood?
    if you've missed the vice versa then i guess you got lucky ;)
    also i see you didn't use deepcopy in the end and instead re-created q_variation each time, any idea what would be quicker?

  • I implented the jmp to nop first, if that didn't give any solutions I would have tried the vice versa, but I got lucky. And according to a StackOverflow question on array copying behaviour the recreation I used is a functional equivalent of deepcopy which is supposed to be faster. But I mainly picked it over deepcopy because I could then see what was going on instead of using a black box function for it.

    But I would not at all use my code as an example, I'm far from an expert. Just barely getting by.

  • I didn't copy anything, I just made a note of the original instruction, overwrote it with the opposite jmp/nop, ran it, then swapped it back.

  • # input reading elided
    
    my %swap=( 'jmp' => 'nop', 'nop' => 'jmp' );
    
    # Part 1:
    my ( $p1_r, $p1_a ) = runcomp();
    print "Part 1: $p1_r $p1_a\n";
    
    for( my $ct=0; $ct < $psize ; $ct++ ) {
            my $old_instr = $ins[$ct];
            if( exists( $swap{$old_instr} ) ) {
                    my $new_instr = $swap{$old_instr};
                    $ins[$ct]=$new_instr;
                    my ( $ret, $acc ) = runcomp();
                    if( $ret eq "bounds" ) {
                            print "Part 2: Replacing $ct ($old_instr) with $new_instr return bounds error with acc=$acc\n";
                    }
                    $ins[$ct]=$old_instr;
            }
    }
    
    # runcomp function elided too
    
  • Ah fair enough.
    I also saw you used enumerate, i need to get better at doing that rather than writing what i think is probably lazy code (and slow)
    Biggest improvement for my solution would be putting it all into a function i think, other than that most of the operational stuff was similar between ours.
    Also a pretty basic programmer myself so tend to try and learn from others code as much as i can, even if it's not the best either!

  • You learned about deepcopy. All is not lost.

  • I didn't modify the code, just changed my if cmd == "jmp" to if cmd == (addr == addrToChange ? "nop" : "jmp") in my, er, VM.

    Since I didn't have anything in the code explicit to nop that's all that's needed.

  • Not the most elegant, but we got there: https://gist.github.com/dmckennell/45bc2e70e2990e43e262ffbcee95f67f

    Couldn't avoid running the program a second time in part 2 when I discover the program is completing successfully following a swap. Would like to have memoized the program result used in my if condition, but not seeing an easy way to manage that with collectFirst.

  • Not really au-fait with Scala but I would have thought you could pattern-match on the result of swapAndExecute? Almost certainly not correct syntax, but something like

        input.zipWithIndex.collectFirst {
          case (instruction, index) 
             swapAndExecute(input, instruction, index) match {
               case LoopResult(acc, Completed) => acc,
               case other => None,
          }
    }.get
    
  • Sadly no - collectFirst takes a partial function (in this case PartialFunction[(Instruction, Int), Int]) which I can't wrangle the input collection into. It's a bit of a 'square peg round hole' situation. I should probably craft something myself for such a case.

    In your code snippet the case would be satisfied on the first instruction and I would then get a NoSuchElementException.

  • Ah, annoying, thanks for the explanation!

  • No worries.

    Just as a side note whilst it has re-entered my mind: I really like reading through your solutions and have got a better impression of 'functional Python' from seeing how you approach the problems.

  • Quite proud of my solution to day 8 part 1, although at about 1 op/s it's going to take a long time to do part 2: https://gist.github.com/rhowe/1059f9da52850f942e7e120d7b11bf80#file-aoc2020day08part1

  • I had to pull my finger out and catch up today.

    Bizarrely also at work today someone came and said 'so, we have this big lump of unsupported Java with no owner still in the firm, could you erm, have a look'

  • Day 9: https://github.com/TijmenK/AoC2020/blob/main/day9.py

    Could reuse a lot of tricks from the last couple of days, it's nice to write code with minimal trial and error fuss. And I'm finally starting to get comfortable with list slicing and list comprehension.

  • Meh, underwhelming day (9) today. Seemed a backward step in terms of difficulty and just going over things already done (such as day 1).

    In the end even the most egregious algorithm would run without trouble. Didn't really require any careful thinking at all.

    Maybe Eric is moving away from:
    a) Make people learn about fundamental/famous CS algorithms in order to solve difficult problems
    to:-
    b) Provide a set of relatively simple problems to allow people to learn all of the different ways that the basic structures can be used, e.g. slicing/etc.

    The previous years seemed to lean heavily on (a) type problems.

    (b) Is definitely useful for people wishing to learn how to program, or learn how to program in a new language...

  • It gave me a confidence boost. All is not lost.

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Advent of Code - adventofcode.com

Posted by Avatar for Drakien @Drakien

Actions