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

Teaching and learning resources • Re: Advent of Code 2024

$
0
0
day 11

Code:

pi@pi5-8:~/AoC2024/day11 $ g++ -O3 day11.cpp pi@pi5-8:~/AoC2024/day11 $ ./a.out Day 11: Plutonian Pebblespart 1   - 203457part 2   - 241394363462435run time - 73.971 ms.pi@pi5-8:~/AoC2024/day11 $
source code
Day 11 in the Chapel programming language gives

Code:

$ ./day11 -nl1 # Pi 4B at 1500 MhzAdvent of Code 2024 Day 11 Plutonian PebblesPart 1 After 25 blinks there will be 183484 stonesPart 2 After 75 blinks there will be 218817038947400 stonesTotal execution time 0.299169 seconds.
The code was

Code:

/*  Advent of Code 2024 Day 11 Plutonian Pebbles    Written 2024 by Eric Olson */use IO,Time,List;proc blink(n:int):[]int {    if n==0 {        return [1];    }    var d=n:bytes;    var q=d.size/2,r=d.size%2;    if r%2==0 {        return [d[0..<q]:int,d[q..<d.size]:int];    }    return [n*2024];}proc prstones(ref stone:[]int) {    for n in stone.domain {        if stone[n]>0 {            write(n);            if stone[n]>1 {                write("x",stone[n]);            }            write(" ");        }    }    writeln();}proc blink25(ref ndom:domain(int,parSafe=false),ref stone:[ndom]int):int{    var xdom:domain(int,parSafe=false)=ndom;    var xtone:[xdom]int;    for k in 0..<25 {        for n in ndom {            var r=stone[n];            if r>0 {                for m in blink(n) {                    if !xdom.contains(m) {                        xdom+=m;                    }                    xtone[m]+=r;                }            }        }        ndom=xdom;        stone=xtone;        xtone=0;    }    var c=0;    for n in ndom {        c+=stone[n];    }    return c;}proc works(ref s:bytes):(int,int) {    var sv=s.split();    var v:[0..<sv.size]int;    for i in v.domain {        v[i]=sv(i):int;    }    var ndom:domain(int,parSafe=false);    var stone:[ndom]int;    for n in v {        if !ndom.contains(n) {            ndom+=n;        }        stone[n]+=1;    }    var p1=blink25(ndom,stone);    blink25(ndom,stone);    var p2=blink25(ndom,stone);    return (p1,p2);}proc parts(ref pluto:list(bytes)):(int,int) {    var p1=0,p2=0;    for s in pluto {        var (q1,q2)=works(s);        p1+=q1; p2+=q2;    }    return (p1,p2);}proc dowork(){    var io=open("day11.txt",ioMode.r);    var fp=io.reader(locking=false);    var pluto:list(bytes);    var s:bytes;    while fp.readLine(s,stripNewline=true) {        pluto.pushBack(s);    }    var (p1,p2)=parts(pluto);    writef("Part 1 After 25 blinks there will be %i stones\n",p1);    writef("Part 2 After 75 blinks there will be %i stones\n",p2);}proc main(){    var t:stopwatch;    t.start();    writeln("Advent of Code 2024 Day 11 Plutonian Pebbles\n");    dowork();    t.stop();    writeln("\nTotal execution time ",t.elapsed()," seconds.");}
One I realized that each stone evolves independently of it's neighbors it was natural to use use associative arrays to keep track of how many stones of each type. After that I had a myriad of troubles because both arrays originally shared the same key table. Adding stone types to the next generation affected the iteration over the current stones and as a result some stones were mysteriously getting lost.

Eventually I figured this out and replaced

Code:

    var xtone:[ndom]int;
with

Code:

    var xdom:domain(int,parSafe=false)=ndom;    var xtone:[xdom]int;
so the domain for the next generation was independent of the current one. The possibility of such a problem seems unique with Chapel, as I haven't seen associative arrays that can share the same key table in other languages.

As a frame of reference, here is a run of the C++ code on the Pi 4B.

Code:

$ g++ -std=gnu++23 -O3 -o lurk11 lurk11.cpp$ ./lurk11 # Pi 4B at 1500 MHzDay 11: Plutonian Pebblespart 1   - 183484part 2   - 218817038947400run time - 221.535 ms.
The algorithm, however, is different. The C++ code uses associative arrays for memoization rather than for tallying up the number of each stone type.

Statistics: Posted by ejolson — Wed Dec 11, 2024 7:04 pm



Viewing all articles
Browse latest Browse all 5190

Trending Articles