1.tcl 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. set fp [open "input"]
  2. gets $fp line
  3. close $fp
  4. set line_len [string length $line]
  5. set layout {}
  6. set is_file 1
  7. set file_no 0
  8. for {set i 0} {$i < $line_len} {incr i} {
  9. set num [string index $line $i]
  10. if {$is_file} {
  11. for {set j 0} {$j < $num} {incr j} {
  12. lappend layout $file_no
  13. }
  14. incr file_no
  15. } else {
  16. for {set j 0} {$j < $num} {incr j} {
  17. lappend layout -1
  18. }
  19. }
  20. set is_file [expr {! $is_file}]
  21. }
  22. set lp 0
  23. set rp [expr {[llength $layout] - 1}]
  24. proc swap {lst a b} {
  25. upvar $lst uplst
  26. set t [lindex $uplst $b]
  27. lset uplst $b [lindex $uplst $a]
  28. lset uplst $a $t
  29. }
  30. while {$lp < $rp} {
  31. if {[lindex $layout $lp] != -1} {
  32. incr lp
  33. continue
  34. }
  35. if {[lindex $layout $rp] == -1} {
  36. incr rp -1
  37. continue
  38. }
  39. swap layout $lp $rp
  40. }
  41. set checksum 0
  42. set i 0
  43. while {1} {
  44. if {([lindex $layout $i] == -1) || ($i > [llength $layout])} {
  45. break
  46. }
  47. incr checksum [expr {[lindex $layout $i] * $i}]
  48. incr i
  49. }
  50. puts $checksum