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

Teaching and learning resources • Re: Advent of Code 2024

$
0
0
Correction was very simple, and not only did it return the correct results (for both part1 and part2), but it also runs 10x faster, than any of the earlier versions.
It's surprisingly easy to make things more complicated than they really are.

Here is my result for day 18.

Code:

$ time chpl --fast --local --launcher smp day18.chpl36.97user 1.28system 0:38.23elapsed 100%CPU (0avgtext+0avgdata 0maxresident)k0inputs+0outputs (0major+0minor)pagefaults 0swaps$ ./day18 -nl1 # Pi 4B at 1500 MHzAdvent of Code 2024 Day 18 RAM RunPart 1 It takes 246 steps to reach the exitPart 2 The blocking block is 22,50Total execution time 0.524505 seconds.
For reference the code is

Code:

/*  Advent of Code 2024 Day 18 RAM Run    Written 2025 by Eric Olson    Dedicated to the dog developer.  */use IO,Time,List;type byte=uint(8);const dirs=[(0,1),(-1,0),(0,-1),(1,0)];proc check(ref i:int,ref s:bytes,const p:bytes):bool {    if s.size<i+p.size {        return false;    }    for k in 0..<p.size {        if s[k+i]!=p[k] {            return false;        }    }    i+=p.size;    return true;}proc scanint(ref i:int,ref s:bytes):int {    const d0=b"0"[0],d9=b"9"[0],minus=b"-"[0];    var r=0,iold=i,m=-1;    while i<s.size {        var d=s[i];        if iold==i&&d==minus {            m=1;        } else if d>=d0 && d<=d9 {            r=r*10-(d-d0);        } else {            break;        }        i+=1;    }    return m*r;}proc getpos(ref s:bytes):(int,int) {    var i=0;    var iold=i;    var x=scanint(i,s);    if iold==i {        writeln("Can't find x coordinate in ",s);        exit(1);    }    if !check(i,s,b",") {        writeln("Can't find separator in ",s);        exit(1);    }    iold=i;    var y=scanint(i,s);    if iold==i {        writeln("Can't find y coordinate in ",s);        exit(1);    }    return (x,y);}proc relax(ref board:[]int):int {    var r=0;    for p in board.domain {        for dp in dirs {            var q=p+dp;            if board.domain.contains(q) {                if board[q]<0 {                    continue;                }                if board[q]+1<board[p] {                    board[p]=board[q]+1;                    r+=1;                }            }        }    }    return r;}proc findpath(ref corrupt:list((int,int)),M,N,cm:int):int {    var board:[0..M,0..N]int;    for (i,j) in {0..M,0..N} {        board[i,j]=(M+1)*(N+1);    }    var c=0;    for p in corrupt {        c+=1;        if c>cm {            break;        }        board[p]=-1;    }    board[0,0]=0;    while relax(board) {};    if board[M,N]<(M+1)*(N+1) {        return board[M,N];    }    return max(int);}proc part1(ref corrupt:list((int,int)),M,N,cm:int):int {    return findpath(corrupt,M,N,cm);}proc part2(ref corrupt:list((int,int)),M,N,cm:int):(int,int) {    var a=cm; var b=corrupt.size;    var cold=0,c=(a+b)/2;    while cold!=c {        var fc=findpath(corrupt,M,N,c);        if fc==max(int) {            b=c;        } else {            a=c;        }        cold=c;        c=(a+b)/2;    }    return corrupt(b-1);}proc dowork(){    var io=open("day18.txt",ioMode.r);    var fp=io.reader(locking=false);    var corrupt:list((int,int));    var s:bytes;    while fp.readLine(s,stripNewline=true) {        var (x,y)=getpos(s);        corrupt.pushBack((x,y));    }    var M=0,N=0;    for (x,y) in corrupt {        if M<x {            M=x;        }        if N<y {            N=y;        }    }    var cm=1024;    if M<10 {        cm=12;    }    var p1=part1(corrupt,M,N,cm);    writef("Part 1 It takes %i steps to reach the exit\n",p1);    var p2=part2(corrupt,M,N,cm);    writef("Part 2 The blocking block is %i,%i\n",p2(0),p2(1));}proc main(){    var t:stopwatch;    t.start();    writeln("Advent of Code 2024 Day 18 RAM Run\n");    dowork();    t.stop();    writeln("\nTotal execution time ",t.elapsed()," seconds.");}
Day 18 is another puzzle that appears best to do with Dijkstra's algorithm but was instead done by relaxing an array. Part 2 was done by bisection. I miss Fido.

Statistics: Posted by ejolson — Fri Feb 21, 2025 2:11 am



Viewing all articles
Browse latest Browse all 8041

Trending Articles