diff options
Diffstat (limited to '2024/tcl/04.tcl')
-rwxr-xr-x | 2024/tcl/04.tcl | 60 |
1 files changed, 18 insertions, 42 deletions
diff --git a/2024/tcl/04.tcl b/2024/tcl/04.tcl index 2e1742f..db00b11 100755 --- a/2024/tcl/04.tcl +++ b/2024/tcl/04.tcl @@ -7,57 +7,33 @@ puts {Part 1: XMAS word search count} set height [llength $input] set width [string length [lindex $input 0]] -set w2 [expr {$height * 2}] -set w3 [expr {$height * 3}] -set input [join $input ""] + +set i1 $input +set input {} +foreach line $i1 { lappend input [split $line ""] } set xmas_hits 0 +set x_mas_hits 0 +for {set y 0} {$y < $height} {incr y} { + for {set x 0} {$x < $width} {incr x} { + if {$x > 2 && [lrindex $input [list $y $x] [list $y $x-1] [list $y $x-2] [list $y $x-3]] in {{X M A S} {S A M X}}} {incr xmas_hits} -for {set i 0} {$i < [string length $input]} {incr i} { - set start $xmas_hits - if {$width - ($i % $width) > 3 && [sindex $input $i $i+1 $i+2 $i+3] == "XMAS"} { - puts $i - incr xmas_hits - } - if {$i % $width > 3 && [sindex $input $i $i-1 $i-2 $i-3] == "XMAS"} { - puts $i - incr xmas_hits - } + if {$y > 2 && [lrindex $input [list $y $x] [list $y-1 $x] [list $y-2 $x] [list $y-3 $x]] in {{X M A S} {S A M X}}} {incr xmas_hits} - if {$height - ($i / $width % $height) > 3 && [sindex $input $i $i+$width $i+$w2 $i+$w3] == "XMAS"} { - puts $i - incr xmas_hits - } - if {$i / $width % $height > 3 && [sindex $input $i $i-$width $i-$w2 $i-$w3] == "XMAS"} { - puts $i - incr xmas_hits - } + if {$x < ($width - 3) && $y > 2 && [lrindex $input [list $y $x] [list $y-1 $x+1] [list $y-2 $x+2] [list $y-3 $x+3]] in {{X M A S} {S A M X}}} {incr xmas_hits} + if {$x > 2 && $y > 2 && [lrindex $input [list $y $x] [list $y-1 $x-1] [list $y-2 $x-2] [list $y-3 $x-3]] in {{X M A S} {S A M X}}} {incr xmas_hits} - # down-right - if {$height - ($i / $width % $height) > 3 && $width - ($i % $width) > 3 && [sindex $input $i [expr {$i+$width+1}] [expr {$i+$w2+2}] [expr {$i+$w3+3}]] == "XMAS"} { - puts $i - incr xmas_hits - } - # down-left - if {$height - ($i / $width % $height) > 3 && $i % $width > 3 && [sindex $input $i [expr {$i+$width-1}] [expr {$i+$w2-2}] [expr {$i+$w3-3}]] == "XMAS"} { - puts $i - incr xmas_hits - } - # up-right - if {$i / $width % $height > 3 && $width - ($i % $width) > 3 && [sindex $input $i [expr {$i-$width+1}] [expr {$i-$w2+2}] [expr {$i-$w3+3}]] == "XMAS"} { - puts $i - incr xmas_hits - } - # up-left - if {$i / $width % $height > 3 && $i % $width > 3 && [sindex $input $i [expr {$i-$width-1}] [expr {$i-$w2-2}] [expr {$i-$w3-3}]] == "XMAS"} { - puts $i - incr xmas_hits + # ABC + # DEF + # GHI + # -> A C E G I + if {$x > 1 && $y > 1 && [lrindex $input [list $y-2 $x-2] [list $y-2 $x] [list $y-1 $x-1] [list $y $x-2] [list $y $x]] in {{M M A S S} {M S A M S} {S S A M M} {S M A S M}}} {incr x_mas_hits} } } puts "Count: $xmas_hits" puts "" -puts {Part 2: } +puts {Part 2: X-MAS pattern search count} -puts "2: $" +puts "Count: $x_mas_hits" |