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

Teaching and learning resources • Re: Advent of Code 2023

$
0
0
...
Fortunately, as I'm still on day 8, there is plenty of computing to do.
i sadly haven't done any of the puzzles.

but what surprises me is that ChatGPT was able to solve the first one. because in my opinion machine learning can't do software development. except for "better" boiler plate code? also maintenance or refactoring which needs a real understanding of the problem should be beyond AI?

however AI clearly is pretty impressive and powerful.
I'm on part 2 of day 8 which is looking like the Chinese remainder theorem. Since I'm not looking forward to finding the cycles, maybe I should try ChatGPT on that one as well.
In an exceedingly lucky coincidence, it appears the number of steps needed to arrive at ??Z starting at ??A is the same as the return when leaving ??Z. Also, there are no cycles that pass through more than one type of ??Z ending.

While that makes the solution to the puzzle easier, one does not have the satisfaction of creating a general ghost-stepping algorithm. To avoid coding a more general algorithm I've been tying to discourage the head of quality assurance and barking from making additional tests that don't have this property.

I'm not sure I succeeded, but here are the results for the specialized algorithm:

Code:

julia> include("day08.jl")Advent of Code 2023 Day 8 Haunted WastelandPart 1 The total number of steps is 22411Part 2 The number of ghost steps is 11188774513823Total execution time 5.906414466 seconds.julia> main()Advent of Code 2023 Day 8 Haunted WastelandPart 1 The total number of steps is 22411Part 2 The number of ghost steps is 11188774513823Total execution time 3.521387046 seconds.julia> main()Advent of Code 2023 Day 8 Haunted WastelandPart 1 The total number of steps is 22411Part 2 The number of ghost steps is 11188774513823Total execution time 3.678939884 seconds.
For reference the code is

Code:

#=  Advent of Code 2023 Day 8 Haunted Wasteland    Written 2023 by Eric Olson =#struct DoExit <: Exceptionendfunction gobble(s::String,m::String)::Tuple{String,Bool}    if length(s)<length(m)        return s,false    end    if s[1:length(m)]==m        return s[length(m)+1:end],true    end    return s,falseendfunction wskip(s::String)::String    for i=1:length(s)        if !isspace(s[i])            return s[i:end]        end    end    return ""endfunction vskip(data,i::Int)::Int    while i<=length(data)&&length(data[i])==0        i+=1    end    return iend function getalpha(s::String)::Tuple{String,String}    for i=1:length(s)        if !isletter(s[i])            return s[i:end],s[1:i-1]        end    end    return "",sendfunction getint(s::String)::Tuple{String,Int64}    n=Int64(0)    for i=1:length(s)        if s[i]>='0'&&s[i]<='9'            n=n*10+Int64(s[i]-'0')        else            return s[i:end],n        end    end    return "",nendfunction getelement(s::String)::Vector{String}    b=s    s,a=getalpha(s)    if length(a)==0        println("$b\nUnable to parse node name!")        throw(DoExit())    end    s,f=gobble(s," = (")    if !f        println("$b\nMissing ' = (' in node table!")        throw(DoExit())    end    s,l=getalpha(s)    if length(l)==0        println("$b\nUnable to parse left node name!")        throw(DoExit())    end    s,f=gobble(s,", ")    if !f        println("$b\nMissing ', ' in node table!")        throw(DoExit())    end    s,r=getalpha(s)    if length(l)==0        println("$b\nUnable to parse right node name!")        throw(DoExit())    end    s,f=gobble(s,")")    if !f        println("$b\nMissing ')' in node table!")        throw(DoExit())    end    if length(s)>0        println("$b\nCharacters after node description!")        throw(DoExit())    end    return [a,l,r]endmutable struct Node    s::Vector{String}    l::Union{Node,Nothing}    r::Union{Node,Nothing}endfunction mknavi(s::String)    i::Int=0    navi=let i=i        function navi(j::Int=1)::Char            if j==0                i=0                return s[1]            end            i+=1            if i>length(s)                i=1            end            return s[i]        end    end    return naviendfunction part1(m::Dict{String,Node},navi::Function)::Int    navi(0)    q=m["AAA"]    c=0    while q.s[1]!="ZZZ"        d=navi()        if d=='L'            q=q.l        elseif d=='R'            q=q.r        else            println("Unknown direction $d in navigation!")            throw(DoExit())        end        c+=1    end    return cendfunction part2(m::Dict{String,Node},navi::Function)::Int64    function qstep(q::Node,d::Char)::Node        if d=='L'            return q.l        elseif d=='R'            return q.r        else            println("Unknown direction $d in navigation!")            throw(DoExit())        end    end    function sloop(q::Node)::Tuple{Int,Node}        c=0        while q.s[1][end]!='Z'            d=navi()            q=qstep(q,d)            c+=1        end        return c,q    end    v=Int64[]    for (_,q) in m        if q.s[1][end]=='A'            navi(0)            p=q            c1,p=sloop(p)            c,p=sloop(qstep(p,navi()))            c2=c+1            c,p=sloop(qstep(p,navi()))            c3=c+1            if c1!=c2||c2!=c3                println("Assumption about uniform cycles not met!")                dothrow(DoExit())            end            push!(v,c1)        end    end    return lcm(v)endfunction doinit()    data=[]    open("day08.txt","r") do fp        data=readlines(fp)    end    m=Dict{String,Node}()    nodes=Vector{Node}(undef,length(data)-2)    for i=3:length(data)        v=getelement(data[i])        m[v[1]]=Node(v,nothing,nothing)    end    for (k,p) in m        p.l=m[p.s[2]]        p.r=m[p.s[3]]    end    navi=mknavi(data[1])    p1=part1(m,navi)    p2=part2(m,navi)    println("Part 1 The total number of steps is ",p1)    println("Part 2 The number of ghost steps is ",p2)endfunction main()    t=@elapsed try        println("Advent of Code 2023 Day 8 Haunted Wasteland\n")        doinit()        throw(DoExit())    catch r        if !isa(r,DoExit)            rethrow(r)        end    end    println("\nTotal execution time ",t," seconds.")end main()
The program checks two periods between ??Z states to make sure the length is the same. Even so, there could still be a dataset that tricks it into giving a wrong answer. Convenientlly an lcm function is built in to the Julia base library.

Statistics: Posted by ejolson — Mon Jan 01, 2024 9:09 am



Viewing all articles
Browse latest Browse all 5369

Trending Articles