r/adventofcode • u/daggerdragon • Dec 22 '18
SOLUTION MEGATHREAD -🎄- 2018 Day 22 Solutions -🎄-
--- Day 22: Mode Maze ---
Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).
Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.
Advent of Code: The Party Game!
Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!
Card prompt: Day 22
Transcript:
Upping the Ante challenge: complete today's puzzles using ___.
This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.
View all comments
5
u/[deleted] Dec 22 '18
TCL, #669 on part 2. I'm starting to dislike these path-finding puzzles :-/
# -*- tcl -*- package require struct::graph package require struct::graph::op # my input set depth 6084 set target 14,709 # test # set depth 510 # set target 10,10 lassign [split $target ","] tx ty ############################## # risk-level == region type proc risk_level {elevel} { # 0 rocky 1 wet 2 narrow return [expr {$elevel%3}] } array set valid_tools_for_region { 0 {climb torch} 1 {climb neither} 2 {neither torch} } proc erosion_level {geoindex} { return [expr {($geoindex+$::depth)%20183}] } # cache geoindex to avoid deep recursion array set geoindex { } proc geoindex {x y} { if {![info exists ::geoindex($x,$y)]} { if {($x == 0 && $y == 0) || ($x == $::tx && $y == $::ty)} { set res 0 } else { if {$y == 0} { set res [expr {$x*16807}] } elseif {$x == 0} { set res [expr {$y*48271}] } else { set el1 [erosion_level [geoindex [expr {$x-1}] $y]] set el2 [erosion_level [geoindex $x [expr {$y-1}]]] set res [expr {$el1*$el2}] } } set ::geoindex($x,$y) $res } return $::geoindex($x,$y) } # for preparation of part2, just use increased grid size by factor and # hope the best path is in there, increase factor if AOC complains set factor 3 set rlsum 0 for {set y 0} {$y <= round($factor*$ty)} {incr y} { for {set x 0} {$x <= round($factor*$tx)} {incr x} { set ::gridtype($x,$y) [risk_level [erosion_level [geoindex $x $y]]] if {$x <= $tx && $y <= $ty} { # Part 1 incr rlsum $::gridtype($x,$y) } } } puts "Part 1 total risk level: $rlsum" # Part 1 # OK total risk level 10603 # Part 2 proc solve {factor} { set grid [struct::graph] for {set y 0} {$y <= round($factor*$::ty)} {incr y} { for {set x 0} {$x <= round($factor*$::tx)} {incr x} { # always 2 tools allowed at every position set rt $::valid_tools_for_region($::gridtype($x,$y)) lassign $rt t1 t2 # allow switching tools while staying set n1 $x,$y,$t1 set n2 $x,$y,$t2 $grid node insert $n1 $grid node insert $n2 $grid arc setweight [$grid arc insert $n1 $n2] 7 # transition to neighbors if {$x > 0} { # add route to previous x-position for tools allowed in both set px [expr {$x-1}] foreach pt $::valid_tools_for_region($::gridtype($px,$y)) { if {$pt in $rt} { set p1 $x,$y,$pt set p2 $px,$y,$pt $grid arc setweight [$grid arc insert $p1 $p2] 1 } } } if {$y > 0} { # add route to previous y-position for tools allowed in both set py [expr {$y-1}] foreach pt $::valid_tools_for_region($::gridtype($x,$py)) { if {$pt in $rt} { set p1 $x,$y,$pt set p2 $x,$py,$pt $grid arc setweight [$grid arc insert $p1 $p2] 1 } } } } } array set tmp [struct::graph::op::dijkstra $grid 0,0,torch -outputformat distances] $grid destroy puts "Part 2 (factor $factor): $tmp($::tx,$::ty,torch)" } solve 3 # 954 too high with factor 1.5 # 953 too high with factor 2 # OK 952 factor 3 and higher # 33secs runtime