Unable to load image

ADVENT OF CODE DAY 11: MONKEY BUSINESS :marseymonke:

Those FRICKING monkeys stole your stuff! Get it back! :soycry: :!marseymonke:

(Also, remember to leave a comment if you did something interesting :marseylove:)

35
Jump in the discussion.

No email address required.

me vs snakes vs @everyone

logarithmic scales, each point is the average of three measurements, figures in milliseconds

:#marseydance:

C++chads simply cannot stop winning @drama_enthusiast discuss

![](/images/1670755872127088.webp)

![](/images/16707559142307742.webp)

Jump in the discussion.

No email address required.

aight fruitcake, ready for your :marseyl:

plot these assemblyscript stats:

10 100 1000100001000005000001000000
112985429861
0121086423848
1111085423871

let lsd: i64 = 1;

class Monkey {
  items: Array<i64>;
  n: i64;

  op1: i64;
  op2: i64;
  opAdd: boolean;

  d: i32;
  tM: i32;
  fM: i32;

  constructor (lines: String) {
    const l = lines.split('\n');
    
    this.items = l[1]
      .split(':')[1]
      .split(', ')
      .map((s: string) => i64.parse(s));
    
    const opStr = l[2].split('=')[1].trim() || '';
    const m = opStr.split(' ');
    this.op1 = m[0] === 'old' ? 0 : i64.parse(m[0])||0;
    this.op2 = m[2] === 'old' ? 0 : i64.parse(m[2])||0;
    this.opAdd = m[1] === '+';
    
    this.d = i32.parse(l[3].trim().split(' ')[3]);
    if (lsd % this.d) lsd *= this.d;
    this.tM = i32.parse(l[4].trim().split(' ')[5]);
    this.fM = i32.parse(l[5].trim().split(' ')[5]);
    
    this.n = 0;
  }

  op (old: i64): i64 {
    return this.opAdd ?
        (this.op1 || old) + (this.op2 || old)
      : (this.op1 || old) * (this.op2 || old)
  }

  test (w: i64): i32 {
    return (w % this.d) ? this.fM : this.tM;
  }
}

const monkeys: Array<Monkey> = [];

export function run(input: string, itr: i32): void {
  input.split('\n\n').forEach((lines) => {
    monkeys.push(new Monkey(lines));
  });

  for (let i = 0; i < itr; i++) {
    monkeys.forEach(monk => {
      while (monk.items.length) {
        let w = monk.items.pop()
        const newW = (monk.op(w)%lsd);
        const newM = monk.test(newW);
        monk.n++;
        monkeys[newM].items.push(newW);
      }
    });
  };

  console.log(
    monkeys.map((m:Monkey) => m.n)
      .sort((a,b) => i32(b-a))
      .slice(0,2)
      .reduce((p, n) => p * n, i64(1))
      .toString()
    );
}

here's the calling ts code:

import * as fs from 'fs';
import {run} from '../build/release.js'

const start = Date.now();
const input = fs.readFileSync(process.argv[2], 'utf-8')
run(input, Number(process.argv[3]));
console.log((Date.now() - start));

and the compiled code's actually portable too, u can run it in a web browser.

god i miss closures tho ... not that u'd know what those r ...

Jump in the discussion.

No email address required.

taking double digit millisecond for 10.000 throws :marseysmug2:

![](/images/1670793727086256.webp)

Those are average over 3 runs

Jump in the discussion.

No email address required.

if i hardcode the LCM mod number, it is even faster, the compiler then can actually try to optimize it.

![](/images/16707940227049947.webp)

here is the final code if anyone cares: https://pastebin.com/hxuNpb0v

Jump in the discussion.

No email address required.

couldn't make it that much faster. tried my own push/pop with static arrays, inlined lcd, inlined all my functions, removed error case branching, swapping forEach for whiles, and got the 1M time down to ~700ms.

almost within spitting distance, but i might be running up against a vm tax here.

you running on anything fancy?

Jump in the discussion.

No email address required.

>you running on anything fancy?

no just an old ryzen 5 2600, that boost to like 3.9GHz

Is there a profiler for this kind of code? The visual studio profiler helped a lot in finding the exact lines of codes that take a while to run.

Jump in the discussion.

No email address required.

probably. there are both rust and cpp front ends to wasm, and both have profilers ... but i choose assemblyscript cause i'm a filthy web app programmer, and i dunno about anything easy to plug and play with that. since when do web programmers care about optimization?

but alas ... today's a work day, i have too much non-programming bs to deal with for fun stuff like micro-optimizing code. who gives a shit about that when ur at a multibillion dollar company?

Jump in the discussion.

No email address required.

huh, hardcoding lcd didn't seem to affect my runtime.

but i'm pretty tired maybe i'll take another look at it when it get up.

did i mention my code is actually portable?

Jump in the discussion.

No email address required.

cpu? I ran my benchmark on a 5800X with conservative as frequency governor. Also I'm benchmarking the whole process' time, not only console.log((Date.now() - start)); kinda unfair

Jump in the discussion.

No email address required.

Also I'm benchmarking the whole process' time, not only console.log((Date.now() - start)); kinda unfair

lol, ok there snowflake. i ran the calling code as js instead of ts and timed the process with the shell time built in.

my 1mil count stats are between 890-910ms. not a big diff. if u want to go add 50ms to all the stats, that's fine. maybe i could find a way to run the wasm directly, and shave that, but i care not.

Jump in the discussion.

No email address required.

apple m1 max, just stock. not plugged in, but that prolly didn't matter.

this is using release build, so max compiler optimizations i think. idk, i never used assemblyscript or webassembly before this ... i just installed and darn i'm impressed.

data structure awareness helps. going from a shift() to pop() on my item processing saw 30% reduction in my 1mil count runtime.

Jump in the discussion.

No email address required.

All them words won't bring your pa back.

Jump in the discussion.

No email address required.

Nice work king but,

  • replace std::list with a higher performance datastructure

  • Replace obsolete types for example: long long vs int64_t

  • split that biggest line and make it more readable

  • use avx-512 instructions to further humilate the competition :marseytrollgun:

Jump in the discussion.

No email address required.

Link copied to clipboard
Action successful!
Error, please refresh the page and try again.