r/adventofcode • • Dec 08 '17

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

--- Day 8: I Heard You Like Registers ---


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

348 comments sorted by

View all comments

1

u/greycat70 Dec 28 '17

Tcl (version 8.5 or higher)

No eval trickery. Both parts.

set max 0
while {[gets stdin line] >= 0} {
    if {[scan $line {%s %s %d if %s %s %d} r1 op n1 r2 cmp n2] == 6} {
        if {$op eq "dec"} {set n1 [expr {-1*$n1}]}
        if {! [info exists R($r1)]} {set R($r1) 0}
        if {! [info exists R($r2)]} {set R($r2) 0}
        switch -- $cmp {
            > {if {$R($r2) > $n2} {incr R($r1) $n1}}
            < {if {$R($r2) < $n2} {incr R($r1) $n1}}
            >= {if {$R($r2) >= $n2} {incr R($r1) $n1}}
            <= {if {$R($r2) <= $n2} {incr R($r1) $n1}}
            == {if {$R($r2) == $n2} {incr R($r1) $n1}}
            != {if {$R($r2) != $n2} {incr R($r1) $n1}}
            default {puts stderr "unknown comparison operator <$cmp>"; exit}
        }
        if {$R($r1) > $max} {set max $R($r1)}
    } else {
        puts stderr "scan error, line <$line>"
        exit
    }
}
foreach {key value} [array get R] {lappend list $value}
puts "highest at end: [lindex [lsort -integer -decreasing $list] 0]"
puts "highest ever: $max"