day 11 part 2

main
magical 2023-12-12 04:56:27 +00:00
parent b7062a7801
commit e8a6d55fa8
1 changed files with 56 additions and 46 deletions

View File

@ -8,17 +8,6 @@ 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
@ -30,45 +19,66 @@ proc column {i} {
}
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
proc solve {map expansion} {
set xdist {}
set ydist {}
foreach row $map {
if {[empty $row]} {
lappend ydist $expansion
} else {
lappend ydist 1
}
}
}
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]
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
}
}
#lappend mins $d
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]
}
puts $mins
puts [ladd $mins]
solve $map 2
solve $map 100
solve $map 1000
solve $map 1000000