adventofcode

Happy Advent 2020! Please put answers in the pinned threads or create one if it does not exist yet. | https://github.com/adventofcode-clojurians/adventofcode-clojurians | Join the private leaderboard with code 217019-4a55b8eb
ihabunek 2017-12-23T06:39:54.000033Z

ah, optimizing assembler, never thought i'd be doing that 😄

dyankowsky 2017-12-23T07:20:13.000030Z

Whew!

dyankowsky 2017-12-23T07:24:04.000030Z

I'll be curious to hear how other people decided to approach part2

ihabunek 2017-12-23T07:26:45.000011Z

did you solve it?

dyankowsky 2017-12-23T07:26:49.000046Z

yeah

ihabunek 2017-12-23T07:26:59.000019Z

ah, i'm feeling very stupid right about now

dyankowsky 2017-12-23T07:27:15.000084Z

it's really hard to see what it's doing

ihabunek 2017-12-23T07:27:31.000064Z

i see the loops but i'm unsure how to go about removing them

dyankowsky 2017-12-23T07:28:03.000018Z

you might need to figure out what the loops are doing

dyankowsky 2017-12-23T07:28:09.000016Z

or rather why they exist

ihabunek 2017-12-23T07:29:10.000016Z

so would a middle-out approach work here? start with the innermost loop and try to remove it

ihabunek 2017-12-23T07:29:44.000028Z

heh, middle-out, accidental silicon valley reference

dyankowsky 2017-12-23T07:29:48.000002Z

heh

dyankowsky 2017-12-23T07:30:07.000072Z

just make sure you get that weissman score way up

dyankowsky 2017-12-23T07:30:27.000070Z

I think it's worth analyzing the loops from the middle out

dyankowsky 2017-12-23T07:35:11.000043Z

I think it's also worth looking at how the registers are used and, most importantly, at what points their values no longer matter

ihabunek 2017-12-23T07:43:02.000057Z

yeah, trying to eliminate registers one by one

mikelis 2017-12-23T08:40:24.000021Z

i rewrote the assembly to C, figured out what it does and found the answer by hand

mikelis 2017-12-23T08:54:25.000005Z

I’d be really curious to see others’ inputs, but at least in my case, no amount of compiler optimization would help because the algorithm itself is fundamentally flawed

ihabunek 2017-12-23T09:45:18.000045Z

i rewrote it in python and got the wrong answer 😄

ihabunek 2017-12-23T09:45:25.000087Z

i must have made a mistake along the way

erwin 2017-12-23T09:49:21.000022Z

I am just running the interpreter for some time now, but I don't think that is going to work 🤓

ihabunek 2017-12-23T09:53:41.000049Z

it makes 3 nested loops for my input, with 100k iterations each, so i don't think brute force is viable 🙂

ihabunek 2017-12-23T10:44:06.000110Z

i got it, i had an off-by-one twice 😄

ihabunek 2017-12-23T10:44:15.000010Z

off-by-two you could say

ihabunek 2017-12-23T10:45:48.000027Z

today was actually fun, although it included very little programming

mikelis 2017-12-23T13:01:20.000075Z

For fun I compiled the C code and let it for half an hour but it didn’t finish. Replacing a small subroutine with one that has better algorithmic complexity let it finish instantly

mikelis 2017-12-23T13:02:14.000063Z

Did your input program also do ... ? spoilers inside

mikelis 2017-12-23T13:02:31.000075Z

Count the number of non-primes in a range?

mikelis 2017-12-23T13:02:52.000058Z

By using a really bad prime checking function

ihabunek 2017-12-23T13:07:48.000018Z

yes

ihabunek 2017-12-23T13:07:55.000013Z

@mfikes i'm guessing everyones did

ihabunek 2017-12-23T13:07:59.000013Z

with different bounds

ihabunek 2017-12-23T13:09:41.000019Z

it should be easy to fix the assembler code if you were allowed to use mod like in day 18

ihabunek 2017-12-23T13:13:18.000063Z

here's my translation of asm to python:

h = 0

for b in range(106700, 123700 + 1, 17):
    f = 1
    for d in range(2, b):
        for e in range(2, b):
            if d * e == b:
                f = 0
    h += f

orestis 2017-12-23T13:42:25.000077Z

Ah, stupid jnz; I thought I was counting the number of primes, not of non-primes.

orestis 2017-12-23T13:43:35.000066Z

Come on, 1 minute limit 🙂

ihabunek 2017-12-23T13:43:39.000046Z

i did the same...

orestis 2017-12-23T13:44:03.000062Z

5 minutes now. D’oh!

ihabunek 2017-12-23T13:44:08.000027Z

and then i missed the fact that the outer range should be inclusive

orestis 2017-12-23T13:45:10.000035Z

I have a list of primes from my bounds, I hope I can rely on them 🙂

orestis 2017-12-23T13:49:19.000014Z

Hah, I had the inclusive list problem also; it’s 1001 numbers, not 1000.

orestis 2017-12-23T14:02:19.000192Z

I wonder what’s the O complexity for this; we have O(N N/2 M), where N ~= 100k and M 1000/17?

mikelis 2017-12-23T14:14:04.000059Z

I also had the off by one error initially when solving by hand :)

ihabunek 2017-12-23T15:43:53.000072Z

as they say, two most difficult problems in programming are naming things, cache invalidation and off-by-one errors 🙂

bhauman 2017-12-23T16:39:07.000039Z

yeah program analysis for the holidays

bhauman 2017-12-23T16:39:36.000049Z

its funny how the heavy the resistance to actually just analyzing the program is

bhauman 2017-12-23T16:41:25.000061Z

I just wanted to find an obvious countdown, but nope

bhauman 2017-12-23T16:45:15.000064Z

the sub -1 got me quite a few times

mfikes 2017-12-23T16:47:25.000035Z

No spoilers here. I like @bhauman’s inline analysis. I ended up with this crap

bhauman 2017-12-23T16:47:40.000079Z

that looks fun too

mfikes 2017-12-23T16:48:07.000056Z

I wanted to optimize it but. I had already spent too much time once I saw what it was doing

bhauman 2017-12-23T16:49:26.000021Z

yeah, its tough with that instruction set

bhauman 2017-12-23T16:49:54.000007Z

and you have to change all the jnz offsets, blah

bhauman 2017-12-23T16:50:27.000059Z

and we have christmas stuff to do

mfikes 2017-12-23T16:51:04.000093Z

Yeah, with respect to the instruction set, I was thinking along the lines of re-adding an instruction that was in the day 18 computer

bhauman 2017-12-23T16:51:23.000036Z

I know the one

mfikes 2017-12-23T16:51:35.000016Z

But, f-it. Too much time spent on this one 🙂

👍 1
ihabunek 2017-12-23T17:35:42.000134Z

i had the same but digital 🙂 cleaned up version:

set b 106700     ; starting value for b
           set c 123700     ; ending value for b
    +----->set f 1          ; iterate b from 106700 to 123700, step 17
    |      set d 2
    | +--->set e 2          ; iterate d from 2 to b - 1
    | | +->set g d          ; iterate e from 2 to b - 1
    | | |  mul g e
    | | |  sub g b
    | | |  jnz g 2 ---+
    | | |  set f 0    |      ; set f = 0 if d * e == b
    | | |  sub e -1 <-+
    | | |  set g e
    | | |  sub g b
    | | +--jnz g -8          ; jump unless e == b
    | |    sub d -1
    | |    set g d
    | |    sub g b
    | +----jnz g -13         ; jump unless d == b
    |      jnz f 2 --+
    |      sub h -1  |       ; increment h if f == 0
    |      set g b <-+
    |      sub g c
    |      jnz g 2  ---+
    |      jnz 1 3     | ; exit
    |      sub b -17 <-+
    +------jnz 1 -23

ihabunek 2017-12-24T11:55:37.000039Z

@fellshard i think everyone had the off-by-one problem in this one, i had two 😄

fellshard 2017-12-23T18:14:29.000050Z

total = 0
curr = 108100 to 125100 (exc.) by 17
  flag = 1
  d = 2 to curr (inc.)
    e = 2 to curr (inc.)
      flag = 0 if d*e = curr
  total++ if flag = 0

fellshard 2017-12-23T18:14:43.000036Z

in loose pseudo; tl;dr, best I can tell, count all non-primes between two numbers

fellshard 2017-12-23T18:24:42.000075Z

I'm still missing something critical, though

fellshard 2017-12-23T18:48:09.000072Z

D'oh. Off-by-one.

mikelis 2017-12-23T18:52:00.000051Z

I was hoping that the task would be to programmatically optimize the algorithm

mikelis 2017-12-23T18:53:20.000050Z

@mfikes looks like we even had the same inputs

borkdude 2017-12-23T19:24:12.000016Z

Asking for a little help: is it the intention of the puzzle to optimize the assembly or just calculate the answer once you know what it’s doing?

2017-12-23T19:35:51.000060Z

today was good to get the average time per day spent on AoC to grow exponentially 😕

bhauman 2017-12-23T19:39:43.000043Z

@borkdude understand the alg and get the answer

2017-12-23T19:40:08.000043Z

@borkdude I ran the optimized assembly until it finished for the answer

borkdude 2017-12-23T19:40:09.000041Z

ok, because I find it really difficult to get the algorithm right with only these instructions…

bhauman 2017-12-23T19:40:20.000008Z

even if you optimize the code it will run really slowly

borkdude 2017-12-23T19:40:24.000056Z

in sane computing time

borkdude 2017-12-23T19:40:32.000035Z

@thegeez How long did yours run?

borkdude 2017-12-23T19:41:08.000052Z

I optimized one instruction: once you find something to be true, jump ahead so the loops are short circuited

bhauman 2017-12-23T19:41:12.000017Z

people have mentioned adding the mod instruction back

bhauman 2017-12-23T19:41:33.000011Z

yeah that would help but still slow

borkdude 2017-12-23T19:41:34.000038Z

yeah, you could

2017-12-23T19:41:47.000025Z

I replaced loops with clojure loops that were smarter, that finished in 2 seconds

2017-12-23T19:43:07.000017Z

Not sure if that counts as optimized assembly or a calculation of the answer, perhaps both

borkdude 2017-12-23T19:44:05.000050Z

Earlier today I was thinking, maybe I could write it in C and let the C compiler optimize the assembly, but then I didn’t know yet what it was doing. Knowing this, it will probably surely not work.

erwin 2017-12-23T19:49:57.000014Z

When you understand the instructions enough to write the C code it is faster to just calculate the answer and be done with it

borkdude 2017-12-23T19:50:58.000065Z

I know, but … fun … adding the mod instruction back like @bhauman could surely work, but I’m kind of done with it.

erwin 2017-12-23T19:54:53.000002Z

you could add mod and remove one loop and shortcut the algorithm, but than you could also just use isProbablePrime from BigInteger and run your algorithm a couple of rounds to find the values for b and c.

borkdude 2017-12-23T19:55:11.000070Z

yup

erwin 2017-12-23T19:56:04.000003Z

the theme for the last couple assignments is, do the easy thing and make it ridiculous for part 2.

borkdude 2017-12-23T19:56:13.000077Z

I guess so yeah

borkdude 2017-12-23T20:22:29.000042Z

fwiw, my code: https://github.com/borkdude/aoc2017/blob/master/src/day23.clj