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

Teaching and learning resources • Re: Advent of Code 2024

$
0
0
day 9

Code:

pi@pi5-8:~/AoC2024/day09 $ g++ -std=c++23 -O3 day09.cpp pi@pi5-8:~/AoC2024/day09 $ ./a.out Day 9: Disk Fragmenterpart 1   - 6241633730082part 2   - 6265268809555run time - 217.211 ms.pi@pi5-8:~/AoC2024/day09 $ 
source code
After some howling about O(N) list indexing, the run for day 9 yielded

Code:

$ ./day09 -nl1 # Pi 4B at 1500 MHzAdvent of Code 2024 Day 09 Disk FragmenterPart 1 The fragmented checksum is 6242766523059Part 2 The non-fragmented checksum is 6272188244509Total execution time 0.32891 seconds.
from the code

Code:

/*  Advent of Code 2024 Day 09 Disk Fragmenter    Written 2024 by Eric Olson */use IO,Time,List;record extent {    var id,s:int;}proc prdisk(ref bl:list(extent)){    for b in bl {        for i in 0..<b.s {            if b.id<0 {                write(".");            } else {                write(b.id);            }        }    }    writeln();}proc chkdisk(ref bl:list(extent)):int {    var r=0,k=0;    for b in bl {        for i in 0..<b.s {            if b.id>0 {                r+=b.id*k;            }            k+=1;        }    }    return r;}proc dtobl(ref d:bytes):list(extent){    const d0=b"0"[0];    var bl:list(extent);    for i in 0..<d.size {        var s=(d[i]-d0):int;        if i%2==0 {            bl.pushBack(new extent(i/2,s));        } else {            bl.pushBack(new extent(-1,s));        }    }    return bl;}proc work1(ref d:bytes):int {    var bl=dtobl(d);    while bl.last.id<0 {        bl.popBack();    }    var b=bl.popBack();    var i=0;    while i<bl.size {        if bl(i).id<0 {            if bl(i).s<=b.s {                bl(i).id=b.id;                b.s-=bl(i).s;                i+=1;            } else {                bl.insert(i,b);                bl(i+1).s-=b.s;                while bl.last.id<0 {                    bl.popBack();                }                b=bl.popBack();            }        }        i+=1;    }    if b.s>0 {        bl.pushBack(b);    }    return chkdisk(bl);}proc part1(const disk:list(bytes)) {    var p1=0;    for d in disk {        p1+=work1(d);    }    return p1;}proc work2(ref d:bytes):int {    var bl=dtobl(d);    while bl.last.id<0 {        bl.popBack();    }    var i=bl.size-1;    var idmax=bl(i).id;    while i>0 {        if bl(i).id>idmax||bl(i).id<0 {            i-=1;            continue;        }        idmax=bl(i).id;        for j in 0..<i {            if bl(j).id<0 {                if bl(i).s<=bl(j).s {                    bl(j).id=bl(i).id;                    bl(i).id=-1;                    if bl(i).s<bl(j).s {                        bl.insert(j+1,new extent(-1,bl(j).s-bl(i).s));                        i+=1;                        bl(j).s=bl(i).s;                    }                    break;                }            }        }        i-=1;    }    return chkdisk(bl);}proc part2(const disk:list(bytes)) {    var p2=0;    for d in disk {        p2+=work2(d);    }    return p2;}proc dowork(){    var io=open("day09.txt",ioMode.r);    var fp=io.reader(locking=false);    var disk:list(bytes);    var s:bytes;    while fp.readLine(s,stripNewline=true) {        disk.pushBack(s);    }    var p:[1..2]int;    cobegin {        p[1]=part1(disk);        p[2]=part2(disk);    }    writef("Part 1 The fragmented checksum is %i\n",p[1]);    writef("Part 2 The non-fragmented checksum is %i\n",p[2]);}proc main(){    var t:stopwatch;    t.start();    writeln("Advent of Code 2024 Day 09 Disk Fragmenter\n");    dowork();    t.stop();    writeln("\nTotal execution time ",t.elapsed()," seconds.");}
I'm not sure whether list indexing is really O(N) or not. It would be nice if the Chapel documentation explained this a little better.

Statistics: Posted by ejolson — Tue Dec 10, 2024 9:10 pm



Viewing all articles
Browse latest Browse all 5907

Trending Articles