adventofcode2023/day13/sol.tcl

59 lines
1.3 KiB
Tcl
Executable File

#!/usr/bin/env tclsh
source ../prelude.tcl
set input stdin
set patterns [read $input]
set patterns [string trim $patterns]
#puts "$patterns"
set patterns [string map {"\n\n" "|"} $patterns]
#puts "$patterns"
set patterns [split $patterns "|"]
#puts "$patterns"
set patterns [lmap p $patterns {split $p "\n"}]
puts "$patterns"
#puts "$patterns"
proc reflect pattern {
set total 0
foreach pat [list $pattern [transpose $pattern]] factor {100 1} {
#puts "reflect $pat"
for {set i 1} {$i < [llength $pat]} {incr i} {
set ok 1
for {set j 0} {$i + $j < [llength $pat] && $i-$j-1 >= 0} {incr j} {
if {[lindex $pat $i+$j] ne [lindex $pat [expr {$i-$j-1}]]} {
set ok 0
break
}
}
if {$ok} {
#puts "$i $factor"
incr total [expr {$i * $factor}]
}
}
}
return $total
}
proc column {lst i} {
set c {}
foreach row $lst {
lappend c [string index $row $i]
}
return [join $c ""]
}
proc transpose pat {
set C [string length [lindex $pat 0]]
set out {}
for {set i 0} {$i < $C} {incr i} {
lappend out [column $pat $i]
}
return $out
}
foreach pat $patterns {
puts "[reflect $pat] <| $pat"
}
puts [ladd [lmap pat $patterns {reflect $pat}]]