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

Teaching and learning resources • Re: Advent of Code 2024

$
0
0
With three kittens working in parallel, a parallel solution to day 24 should be possible. I think computing the loss function over all wire swaps is a task that could easily be distributed across a large number of cores.
Rather than parallelizing day 24 the three black kittens spent the whole day focused on gophers. Since it's already Easter, I went on to day 25 and finished the Advent of Code.

Code:

$ ./day25 -nl1 # Pi 4B at 1500 MHzAdvent of Code 2024 Day 25 Code ChroniclePart 1 There are 3495 unique lock/key pairs.Total execution time 0.005945 seconds.
The solution in Chapel was

Code:

/*  Advent of Code 2024 Day 25 Code Chronicle    Written 2025 by Eric Olson */use IO,Time,List;record qspec {    var p:[0..<5]int;    var lock:bool;}proc decode(inputs:list(bytes)):qspec {    var r:qspec;    if inputs[0]==b"#####" {        r.lock=true;    } else {        r.lock=false;    }    for i in 0..<inputs.size {        var s=inputs[i];        for i in 0..<s.size {            if s[i]==b"#"[0] {                r.p[i]+=1;            }        }    }    return r;}proc part1(ref keys:list([0..<5]int),        ref locks:list([0..<5]int),mlen:int):int {    var p1=0;    for k in 0..<keys.size {        for l in 0..<locks.size {            proc fits():bool {                for i in 0..<5 {                    if keys[k][i]+locks[l][i]>mlen {                        return false;                    }                }                return true;            }            if fits() {                p1+=1;            }        }    }    return p1;}proc dowork(){    var io=open("day25.txt",ioMode.r);    var fp=io.reader(locking=false);    var inputs:list(bytes);    var s:bytes;    var keys:list([0..<5]int);    var locks:list([0..<5]int);    var mlen=0;    proc getkey(){        if mlen<inputs.size {            mlen=inputs.size;        }        var r=decode(inputs);        if r.lock {            locks.pushBack(r.p);        } else {            keys.pushBack(r.p);        }        inputs.clear();    }    while fp.readLine(s,stripNewline=true) {        if s.size==0 {            getkey();        } else {            inputs.pushBack(s);        }    }    if inputs.size>0 {        getkey();    }    var p1=part1(keys,locks,mlen);    writeln("Part 1 There are ",p1," unique lock/key pairs.");}proc main(){    var t:stopwatch;    t.start();    writeln("Advent of Code 2024 Day 25 Code Chronicle\n");    dowork();    t.stop();    writeln("\nTotal execution time ",t.elapsed()," seconds.");}
The code is much shorter than it started out because I gave up on performing a kd-tree fast neighborhood search and simply looped over all possible keys and locks.

Statistics: Posted by ejolson — Thu Apr 24, 2025 5:01 am



Viewing all articles
Browse latest Browse all 8609

Trending Articles