Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 8609

Teaching and learning resources • Re: Advent of Code 2024

$
0
0
Here is a solution to day 21.

Code:

$ time chpl --fast --local --launcher smp day21.chpl37.18user 1.16system 0:38.32elapsed 100%CPU (0avgtext+0avgdata 0maxresident)k0inputs+0outputs (0major+0minor)pagefaults 0swaps$ ./day21 -nl1 # Pi 4B at 1500 MHzAdvent of Code 2024 Day 21 Keypad ConundrumPart 1 The three-robot complexity sum is 154208Part 2 The 26-robot complexity sum is 188000493837892Total execution time 0.000696 seconds.
My solution to day 22 isn't as efficient as day 21.

Code:

$ ./day22 -nl1 # Pi 4B at 1500 MHzAdvent of Code 2024 Day 22 Monkey MarketPart 1 The 2000th secret sum is 15303617151Part 2 The most bananas you can get are 1727Total execution time 2211.4 seconds.
The program searches through all 130321 patterns in parallel and regenerates the secret sequence for each search.

Code:

/*  Advent of Code 2024 Day 22 Monkey Market    Written 2025 by Eric Olson */use IO,Time,List;proc mix(x,y:int):int {    return x^y;}proc prune(x:int):int {    return x%16777216;}proc nsecret(x:int):int {    var r=prune(mix(64*x,x));    r=prune(mix(r/32,r));    r=prune(mix(r*2048,r));    return r;}proc part1(ref secret:list(int)):int {    var p1=0:int;    for x in secret {        var r=x;        for i in 1..2000 {            r=nsecret(r);        }        p1+=r;            }    return p1;}proc getscore(const ref secret:list(int),k:int):int {    var r=0:int;    for x in secret {        var dy=x%10;        var z=x,m=0;        for i in 1..2000 {            z=nsecret(z); var dz=z%10;            m=(m*256+dz-dy+9)&0xFFFFFFFF;            dy=dz;            if i>3&&m==k {                r+=dy;                break;            }        }    }    return r;}proc part2(ref secret:list(int)):int {    const xdom={0..18};    var ps:[0..18,0..18]int;    forall v in 0..360 {        var a=v/19,b=v%19;        ps[a,b]=0;        for c in xdom {            for d in xdom {                var k=((a*256+b)*256+c)*256+d;                var r=getscore(secret,k);                if ps[a,b]<r {                    ps[a,b]=r;                }            }        }    }    var p2=0;    for p in ps.domain {        if p2<ps[p] {            p2=ps[p];        }    }    return p2;}proc dowork(){    var io=open("day22.txt",ioMode.r);    var fp=io.reader(locking=false);    var secret:list(int);    var s:bytes;    while fp.readLine(s,stripNewline=true) {        try {            secret.pushBack(s:int);        } catch {            writeln("Unable to convert ",s," to secret!");            exit(1);        }    }    var p1=part1(secret);    var p2=part2(secret);    writeln("Part 1 The 2000th secret sum is ",p1);    writeln("Part 2 The most bananas you can get are ",p2);}proc main(){    var t:stopwatch;    t.start();    writeln("Advent of Code 2024 Day 22 Monkey Market\n");    dowork();    t.stop();    writeln("\nTotal execution time ",t.elapsed()," seconds.");}
On a 128-core/256-thread EPYC server it still doesn't make the 15 second cutoff. I think the optimizer is targeting the wrong processor generation.

Code:

$ # Dual EPYC 7702$ CHPL_RT_NUM_THREADS_PER_LOCALE=MAX_LOGICAL ./day22 -nl1Advent of Code 2024 Day 22 Monkey MarketPart 1 The 2000th secret sum is 15303617151Part 2 The most bananas you can get are 1727Total execution time 27.0774 seconds.
Here in the desert it's a rainy day.

Statistics: Posted by ejolson — Wed Mar 05, 2025 5:04 pm



Viewing all articles
Browse latest Browse all 8609

Trending Articles