adventofcode2023/day11/sol.tcl

75 lines
1.3 KiB
Tcl
Raw Normal View History

2023-12-11 05:31:14 +00:00
#!/usr/bin/env tclsh
source ../prelude.tcl
set input stdin
set map [split [string trim [read $input]] "\n"]
puts $map
proc empty {row} { return [regexp {^\.+$} $row] }
set map' {}
foreach row $map {
if {[empty $row]} {
lappend map' "${row}"
}
lappend map' $row
}
set map ${map'}
puts $map
proc column {i} {
global map
set c {}
foreach row $map {
lappend c [string index $row $i]
}
return [join $c ""]
}
set map' {}
set W [string length [lindex $map 0]]
for {set i 0} {$i < $W} {incr i} {
set col [column $i]
if {[empty $col]} {
lappend map' $col
}
lappend map' $col
}
set map ${map'}
puts [join $map "\n"]
set points {}
for {set x 0} {$x < [llength $map]} {incr x} {
set col [lindex $map $x]
for {set y 0} {$y < [string length $col]} {incr y} {
if {[string index $col $y] eq "#"} {
lappend points $x $y
}
}
}
puts $points
proc dist {x y u v} {
return [expr {abs($x-$u) + abs($y-$v)}]
}
set mins {}
foreach {x y} $points {
#set d 100000
foreach {u v} $points {
if {$u > $x || ($u == $x && $v > $y)} {
#set d [min $d [dist $x $y $u $v]]
lappend mins [dist $x $y $u $v]
}
}
#lappend mins $d
}
puts $mins
puts [ladd $mins]