#!/usr/bin/env tclsh set adapters [split [read stdin]] while {[lindex $adapters end] eq ""} {set adapters [lrange $adapters 0 end-1]} set adapters [lsort -integer $adapters] set adapters [linsert $adapters 0 0] lappend adapters [expr {3 + [lindex $adapters end]}] foreach i {1 2 3} {set n($i) 0} for {set i 0} {$i < [llength $adapters] - 1} {incr i} { set delta [expr {[lindex $adapters $i+1] - [lindex $adapters $i]}] lappend deltas $delta incr n($delta) } # parray n puts "deltas: $deltas" # There are no deltas of 2 for some reason. # A delta of 3 gives no choice -- there is just one path. # A delta of 1 followed by a delta of 3 also gives no choice. # But consecutive 1s give a choice. With 2 1s in a row, there are 2 choices. # For 3 1s in a row, 4 choices. For 4 1s, 7 choices. array set map {1 1 2 2 3 4 4 7} # For all of our deltas, count the number of consecutive 1s. set prev 3 set len 0 foreach d $deltas { if {$d == 1} { incr len } elseif {$d == 3 && $prev == 1} { lappend ones $len set len 0 } set prev $d } puts "ones: $ones" # There are no sequences of consecutive 1s greater than 4, so lucky us. set count 1 foreach i $ones {set count [expr {$count * $map($i)}]} puts $count