adventofcode2023/day11/sol.tcl

85 lines
1.7 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] }
proc column {i} {
global map
set c {}
foreach row $map {
lappend c [string index $row $i]
}
return [join $c ""]
}
2023-12-12 04:56:27 +00:00
proc solve {map expansion} {
set xdist {}
set ydist {}
foreach row $map {
if {[empty $row]} {
lappend ydist $expansion
} else {
lappend ydist 1
}
}
2023-12-11 05:31:14 +00:00
2023-12-12 04:56:27 +00:00
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
2023-12-11 05:31:14 +00:00
}
}
2023-12-12 04:56:27 +00:00
puts [join $map "\n"]
2023-12-11 05:31:14 +00:00
2023-12-12 04:56:27 +00:00
puts "xdist $xdist"
puts "ydist $ydist"
2023-12-11 05:31:14 +00:00
2023-12-12 04:56:27 +00:00
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] ]]
}
2023-12-11 05:31:14 +00:00
2023-12-12 04:56:27 +00:00
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]
}
2023-12-11 05:31:14 +00:00
}
2023-12-12 04:56:27 +00:00
#lappend mins $d
2023-12-11 05:31:14 +00:00
}
2023-12-12 04:56:27 +00:00
puts $mins
puts [ladd $mins]
2023-12-11 05:31:14 +00:00
}
2023-12-12 04:56:27 +00:00
solve $map 2
solve $map 100
solve $map 1000
solve $map 1000000