Compare commits

...

4 Commits

Author SHA1 Message Date
sejo b8849d003c day 15 stats, thanks alderwick! 2021-12-15 16:32:48 -06:00
sejo 5817e83f76 day 15, part 2 - thanks again alderwick! 2021-12-15 16:31:38 -06:00
sejo a875a494b6 alderwick's algorithm is the solution 2021-12-15 15:57:45 -06:00
sejo e0aae3b1bd day 15, thanks alderwick! 2021-12-15 15:57:08 -06:00
4 changed files with 84 additions and 0 deletions

32
15/15.awk Normal file
View File

@ -0,0 +1,32 @@
# algorithm proposed by alderwick :)
BEGIN{ FS = "" }
function p(x,y){ return x","y }
{ for(i=1;i<=NF;i++) r[p(i,NR)] = $i }
END{
s[p(1,1)] = 0
target = 1
while(!(p(NF,NR) in s)){
print "target:",target
delete ns
for(i in s){
split(i,arr,",")
x = arr[1]; y=arr[2]
cs = s[p(x,y)]
if(cs<target-9) continue # too far from target score
if(x>1 && r[p(x-1,y)]+cs==target && s[p(x-1,y)]=="" )
ns[p(x-1,y)] = target # left
if(x<NF && r[p(x+1,y)]+cs==target && s[p(x+1,y)]=="" )
ns[p(x+1,y)] = target #right
if(y>1 && r[p(x,y-1)]+cs==target && s[p(x,y-1)]=="" )
ns[p(x,y-1)] = target # up
if(y<NR && r[p(x,y+1)]+cs==target && s[p(x,y+1)]=="" )
ns[p(x,y+1)] = target # down
}
for(i in ns) s[i] = ns[i]
target++
}
print s[p(NF,NR)]
}

37
15/15_2.awk Normal file
View File

@ -0,0 +1,37 @@
# algorithm proposed by alderwick :)
BEGIN{ FS = "" }
function p(x,y){ return x","y }
function risk(x,y){
x--; y--; # zero based
return (r[p(x%NF+1,y%NF+1)] + int(x/NF) + int(y/NR)-1)%9 + 1
}
{ for(i=1;i<=NF;i++) r[p(i,NR)] = $i }
END{
s[p(1,1)] = 0
target = 1
while(!(p(NF*5,NR*5) in s)){
if(target%100==0) print "target:",target
delete ns
for(i in s){
split(i,arr,",")
x = arr[1]; y=arr[2]
cs = s[p(x,y)]
if(cs<target-9) continue # too far from target score
if(x>1 && risk(x-1,y)+cs==target && s[p(x-1,y)]=="" )
ns[p(x-1,y)] = target # left
if(x<NF*5 && risk(x+1,y)+cs==target && s[p(x+1,y)]=="" )
ns[p(x+1,y)] = target #right
if(y>1 && risk(x,y-1)+cs==target && s[p(x,y-1)]=="" )
ns[p(x,y-1)] = target # up
if(y<NR*5 && risk(x,y+1)+cs==target && s[p(x,y+1)]=="" )
ns[p(x,y+1)] = target # down
}
for(i in ns) s[i] = ns[i]
target++
}
print s[p(NF*5,NR*5)]
}

14
15/Makefile Normal file
View File

@ -0,0 +1,14 @@
1t:
awk -f 15.awk test
1i:
awk -f 15.awk input
2t:
awk -f 15_2.awk test
2i:
awk -f 15_2.awk input
paste:
xclip -sel c -o > test

View File

@ -27,6 +27,7 @@ awk -c -f 01.awk inputfile
```
-------Part 1-------- -------Part 2--------
Day Time Rank Score Time Rank Score
15 16:52:00 24420 0 17:30:02 20656 0 --- thanks alderwick for your help!
14 00:27:53 4878 0 01:07:47 3311 0
13 02:02:33 9507 0 02:19:53 9225 0 --- started late
12 01:57:59 7241 0 02:16:49 6447 0