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

Teaching and learning resources • Re: Advent of Code 2023

$
0
0
My plan is to create reachability counts for any number of steps starting from the sidewalks at each of the four corners and sides then paste them together.

Fido yawned and growled don't wake me up when that fails. I wasn't planning to.
It seems to work but needs some optimizations. As the timings for the Pi Zero are still in progress, here are the results for a Ryzen 4650 desktop.

Code:

julia> include("day21.jl") # Ryzen 4650Advent of Code 2023 Day 21 Step CounterPart 1 There are 3682 reachable garden plots.Part 2 On the infinite grid 609012263058042 are reachable.Total execution time 274.843657134 seconds.julia> main()Advent of Code 2023 Day 21 Step CounterPart 1 There are 3682 reachable garden plots.Part 2 On the infinite grid 609012263058042 are reachable.Total execution time 275.157613274 seconds.
For reference the code is

Code:

#=  Advent of Code 2023 Day 21 Step Counter    Written 2024 by Eric Olson =#struct DoExit <: Exceptionendfunction pcount(A::Matrix{Char})::Int64    M,N=size(A)    s=0    for i=1:M        for j=1:N            if A[i,j]=='O'                s+=1            end        end    end        return sendfunction pclear(A::Matrix{Char})::Matrix{Char}    M,N=size(A)    B=Matrix{Char}(undef,M,N)    for j=1:N        for i=1:M            if A[i,j]=='O'                B[i,j]='.'            else                B[i,j]=A[i,j]            end        end    end    return Bendfunction walk(A::Matrix{Char})::Matrix{Char}    B=pclear(A)    M,N=size(B)    function gohere(i::Int,j::Int)        if j>N return end        if j<1 return end        if i>M return end        if i<1 return end        if B[i,j]=='.'            B[i,j]='O'        end    end    for i=1:M        for j=1:N            if A[i,j]=='O'                gohere(i,j+1)                gohere(i-1,j)                gohere(i,j-1)                gohere(i+1,j)            end        end    end    return Bendfunction StoO(c::Char)::Char    if c=='S'        return 'O'    end    return cendfunction part1(data::Vector{String})::Int64    M=length(data)    N=length(data[1])    A=[StoO(data[i][j]) for i=1:M,j=1:N]    for i=1:64        A=walk(A)    end    return pcount(A)endfunction reachable(A::Matrix{Char})::Vector{Int64}    saturated=false    i=1; c0=1; c1=0; c2=0    z=Int64[]    while !saturated        push!(z,c0)        A=walk(A); i+=1        c2=c1; c1=c0; c0=pcount(A)        if c0==c2            break        end    end    return zendfunction getpop(z::Vector{Int64},n::Int)::Int64    N=length(z)    if n<=N        return z[n]    else        m=(n+1-N)%2        return z[N-1+m]    endend#=  4  3  2    5  9  1    6  7  8  =#function toentry(i::Int,j::Int)::Int    if i==0        if j>0            return 1        elseif j<0            return 5        end        return 9    elseif i<0        if j>0            return 2        elseif j==0            return 3        end        return 4    end    if j<0        return 6    elseif j==0        return 7    end    return 8endfunction part2(data::Vector{String})::Int64    M=length(data)    N=length(data[1])    A=[StoO(data[i][j]) for i=1:M,j=1:N]    Z=Vector{Vector{Int64}}(undef,9)    Z[9]=reachable(A)    M2=(M+1)÷2; N2=(N+1)÷2    P=[[M2,N],[1,N],[1,N2],[1,1],[M2,1],[M,1],[M,N2],[M,N]]    for d=1:8        A=pclear(A)        A[P[d][1],P[d][2]]='O'        Z[d]=reachable(A)    end    s=26501365    sM=s÷M+1    sN=s÷N+1    t::Int64=0    for i=-sM:sM        for j=-sN:sN            c=s+1            if i>0                c+=-M2-M*(i-1)            elseif i<0                c+=-M2+M*(i+1)            end            if j>0                c+=-N2-N*(j-1)            elseif j<0                c+=-N2+N*(j+1)            end            if c>0                d=toentry(i,j)                t+=getpop(Z[d],c)            end        end    end    return tendfunction doinit()    data=String[]    open("day21.txt","r") do fp        data=readlines(fp)    end    p1=part1(data)    p2=part2(data)    println("Part 1 There are ",p1," reachable garden plots.")    println("Part 2 On the infinite grid ",p2," are reachable.")endfunction main()    t=@elapsed try        println("Advent of Code 2023 Day 21 Step Counter\n")        doinit()        throw(DoExit())    catch r        if !isa(r,DoExit)            rethrow(r)        end    end    println("\nTotal execution time ",t," seconds.")end main()
The slowness comes from the loops

Code:

    for i=-sM:sM        for j=-sN:sN
which lead to 40925694601 iterations since sM and sN are both 202301. There should be a way to reduce this so only the boundary of the reachable area is considered. For now I'm claiming that the slow algorithm makes a better test of Julia on the Pi Zero.

Statistics: Posted by ejolson — Mon Jan 29, 2024 7:26 am



Viewing all articles
Browse latest Browse all 5424

Trending Articles