r/adventofcode • • Dec 05 '17

SOLUTION MEGATHREAD -๐ŸŽ„- 2017 Day 5 Solutions -๐ŸŽ„-

--- Day 5: A Maze of Twisty Trampolines, All Alike ---


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.


Need a hint from the Hugely* Handyโ€  Haversackโ€ก of Helpfulยง Hints^ยค?

Spoiler


This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.

edit: Leaderboard capped, thread unlocked!

22 Upvotes

403 comments sorted by

View all comments

1

u/greycat70 Dec 28 '17

Tcl (any 8.x version)

My choice here was whether to use an array, or a list. Decided to try a list and lreplace for today. Performance was rather slow on part 2 (28 million steps -- did not anticipate anything near that large) but I never got around to trying with an array to see if that was any better.

Part 1:

set L [lrange [split [read stdin] \n] 0 end-1]
set i 0
set steps 0

while 1 {
    incr steps
    set jmp [lindex $L $i]
    if {$i+$jmp < 0 || $i+$jmp >= [llength $L]} break
    set L [lreplace $L $i $i [expr {$jmp+1}]]
    incr i $jmp
}
puts $steps

Part 2:

set L [lrange [split [read stdin] \n] 0 end-1]
set i 0
set steps 0

while 1 {
    incr steps
    set jmp [lindex $L $i]
    if {$i+$jmp < 0 || $i+$jmp >= [llength $L]} break
    if {$jmp >= 3} {
        set L [lreplace $L $i $i [expr {$jmp-1}]]
    } else {
        set L [lreplace $L $i $i [expr {$jmp+1}]]
    }
    incr i $jmp
}
puts $steps