adventofcode2023/day14/sol.tcl

39 lines
715 B
Tcl
Executable File

#!/usr/bin/env tclsh
source ../prelude.tcl
set input stdin
proc roll-left lane {
set prev {}
while {$prev ne $lane} {
set prev $lane
#regsub -all {(O+)(\.+)} $lane {\2\1} lane
regsub -all {(\.+)(O+)} $lane {\2\1} lane
}
return $lane
}
proc calc-load lane {
set N [slen $lane]
set load 0
for {set i 0} {$i < $N} {incr i} {
if {[string index $lane $i] eq "O"} {
incr load [expr {$N - $i}]
}
}
return $load
}
set map {}
while {[gets $input line] >= 0} {
lappend map $line
}
set part1 0
foreach l [transpose $map] {
set r [roll-left $l]
set n [calc-load $r]
puts "$l => $r | $n"
incr part1 $n
}
puts $part1