adventofcode2023/day11/sol.tcl

85 lines
1.7 KiB
Tcl
Executable File

#!/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] }
proc column {i} {
global map
set c {}
foreach row $map {
lappend c [string index $row $i]
}
return [join $c ""]
}
proc solve {map expansion} {
set xdist {}
set ydist {}
foreach row $map {
if {[empty $row]} {
lappend ydist $expansion
} else {
lappend ydist 1
}
}
set W [string length [lindex $map 0]]
for {set i 0} {$i < $W} {incr i} {
set col [column $i]
if {[empty $col]} {
lappend xdist $expansion
} else {
lappend xdist 1
}
}
puts [join $map "\n"]
puts "xdist $xdist"
puts "ydist $ydist"
set points {}
for {set y 0} {$y < [llength $map]} {incr y} {
set row [lindex $map $y]
for {set x 0} {$x < [string length $row]} {incr x} {
if {[string index $row $x] eq "#"} {
lappend points $x $y
}
}
}
puts $points
proc dist {x y u v} {
upvar xdist xdist
upvar ydist ydist
if {$v < $y} { lassign [list $y $v] v y }
return [ladd [concat [lrange $xdist $x+1 $u] [lrange $ydist $y+1 $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]
}
solve $map 2
solve $map 100
solve $map 1000
solve $map 1000000