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

Teaching and learning resources • Re: Advent of Code 2025

$
0
0
I'll be surprised if that kitten does anything other than complicate the code.
Here is Day 05 running on a Pi 5.

Code:

$ ./day05-goAdvent of Code Day 05 Cafeteria (go)Part 1 There are 640 fresh ingredientsPart 2 There are 365804144481581 IDs considered freshTotal execution time 0.00179995 seconds.
For reference the code is

Code:

/*  Advent of Code 2025 Day 05 Cafeteria (go)    Written December 2025 by Eric Olson */package mainimport("fmt"; "os"; "time"; "bufio"; "sort")var tictime time.Timefunc tic(){    tictime=time.Now()}func toc() float64 {    now:=time.Now()    elapsed:=now.Sub(tictime)    return elapsed.Seconds()}type abspec struct {    a,b int64}func fscanf(fp *bufio.Reader,s string,a...interface{})(int,error){    r,err:=fmt.Fscanf(fp,s,a...)    if err!=nil&&err.Error()=="newline in format does not match input" {        fp.UnreadRune()    }    return r,err}var idrange=make([]abspec,0,128)var ids=make([]int64,0,128)func doinit(fn string){    fd,err:=os.Open(fn)    if err!=nil {        fmt.Fprintf(os.Stderr,"Unable to open %s for input!\n",fn)        os.Exit(1)    }    defer fd.Close()    fp:=bufio.NewReader(fd)    idrange=idrange[:0]    for {        if _,err:=fscanf(fp,"\n"); err==nil {            break;        }        var a,b int64        r,err:=fscanf(fp,"%d-%d\n",&a,&b)        if r!=2||err!=nil {             fmt.Fprintf(os.Stderr,"ID range parse error!\n")            os.Exit(1)        }        idrange=append(idrange,abspec{a,b})    }    ids=ids[:0]    for {        var x int64        r,err:=fscanf(fp,"%d\n",&x)        if r!=1||err!=nil {            break        }        ids=append(ids,x)    }    return}func isvalid(x int64)bool {    for _,z:=range idrange {        if z.a<=x&&x<=z.b { return true }    }    return false}func part1()int {    p1:=0;     for _,x:=range ids {        if isvalid(x) { p1++ }    }    return p1}func rorder(i,j int)bool {    return idrange[i].a<idrange[j].a}func part2()int64 {    sort.Slice(idrange,rorder)    idmerge:=make([]abspec,0,len(idrange))    idmerge=append(idmerge,idrange[0])    for i:=1;i<len(idrange);i++ {        p:=&idmerge[len(idmerge)-1]        z:=&idrange[i]        if z.a>p.b {            idmerge=append(idmerge,*z)        } else {            p.b=max(z.b,p.b)        }    }    p2:=int64(0)    for _,z:=range idmerge {        p2+=z.b-z.a+1    }    return p2}func dowork(){    doinit("day05.txt")    p1:=part1()    p2:=part2()    fmt.Printf("Part 1 There are %d fresh ingredients\n",p1)    fmt.Printf("Part 2 There are %d IDs considered fresh\n",p2)}func main(){    fmt.Printf("Advent of Code Day 05 Cafeteria (go)\n\n")    tic()    dowork()    t:=toc()    fmt.Printf("\nTotal execution time %.6g seconds.\n",t)    os.Exit(0)}
I asked Scratchy for help with the fprintf function. I have a suspicion that routine is designed to sink my code quality and maintainability score at the code review, but never mind.

Statistics: Posted by ejolson — Wed Dec 10, 2025 5:43 am



Viewing all articles
Browse latest Browse all 8621

Trending Articles