summaryrefslogtreecommitdiffstats
path: root/2024/tcl/lib.tcl
blob: 588a5e84fe59b1a817b3dd1d1fe93c093e303558 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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
    }
    grid {
      set input(raw) [string trim [read $fd]]
      set input(lines) [split $input(raw) "\r\n"]
      set input(grid) {}
      foreach line $input(lines) {
        lappend input(grid) [split $line ""]
      }
      set input(h) [llength $input(grid)]
      set input(w) [llength [lindex $input(grid) 0]]
      set input(debug) $input(grid)
    }
  }
}

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
}