summaryrefslogblamecommitdiffstats
path: root/2024/tcl/lib.tcl
blob: 048bce57a89b17046ccc4971213ee9b1852d2426 (plain) (tree)
1
2
3
4
5
6
7
                             





                                               
































                                                       
 







                                       






                                      
proc setup {n {mode lines}} {
  puts "Advent of Code 2024 day $n Solution"
  puts "<https://adventofcode.com/2024/day/$n>"
  puts ""

  set n [string range 0$n end-1 end]
  upvar input input
  set fd [open input.$n.txt]

  switch -- $mode {
    lines {
      set input [split [string trim [read $fd]] "\r\n"]
      close $fd
    }
    data {
      set input [read $fd]
      close $fd
    }
    fd {
      set input $fd
    }
  }
}

proc regroup {pat {var input}} {
  upvar $var list
  set list2 {}
  set cur {}
  foreach el $list {
    if {[string match $pat $el]} {
      if {$cur != {}} {
        lappend list2 $cur
        set cur {}
      }
    } else {
      lappend cur $el
    }
  }
  lappend list2 $cur
  set list $list2
}

proc sindex {str args} {
  set ret {}
  foreach arg $args {
    append ret [string index $str $arg]
  }
  return $ret
}
proc lrindex {list args} {
  set ret {}
  foreach arg $args {
    lappend ret [lindex $list {*}$arg]
  }
  return $ret
}