summaryrefslogtreecommitdiffstats
path: root/2024/tcl/04.tcl
blob: 2e1742fe2810086b64eb28194c1af2fd4917eb94 (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
#!/bin/env tclsh

source lib.tcl
setup 4

puts {Part 1: XMAS word search count}

set height [llength $input]
set width [string length [lindex $input 0]]
set w2 [expr {$height * 2}]
set w3 [expr {$height * 3}]
set input [join $input ""]

set xmas_hits 0

for {set i 0} {$i < [string length $input]} {incr i} {
  set start $xmas_hits
  if {$width - ($i % $width) > 3 && [sindex $input $i $i+1 $i+2 $i+3] == "XMAS"} {
    puts $i
    incr xmas_hits
  }
  if {$i % $width > 3 && [sindex $input $i $i-1 $i-2 $i-3] == "XMAS"} {
    puts $i
    incr xmas_hits
  }

  if {$height - ($i / $width % $height) > 3 && [sindex $input $i $i+$width $i+$w2 $i+$w3] == "XMAS"} {
    puts $i
    incr xmas_hits
  }
  if {$i / $width % $height > 3 && [sindex $input $i $i-$width $i-$w2 $i-$w3] == "XMAS"} {
    puts $i
    incr xmas_hits
  }

  # down-right
  if {$height - ($i / $width % $height) > 3 && $width - ($i % $width) > 3 && [sindex $input $i [expr {$i+$width+1}] [expr {$i+$w2+2}] [expr {$i+$w3+3}]] == "XMAS"} {
    puts $i
    incr xmas_hits
  }
  # down-left
  if {$height - ($i / $width % $height) > 3 && $i % $width > 3 && [sindex $input $i [expr {$i+$width-1}] [expr {$i+$w2-2}] [expr {$i+$w3-3}]] == "XMAS"} {
    puts $i
    incr xmas_hits
  }
  # up-right
  if {$i / $width % $height > 3 && $width - ($i % $width) > 3 && [sindex $input $i [expr {$i-$width+1}] [expr {$i-$w2+2}] [expr {$i-$w3+3}]] == "XMAS"} {
    puts $i
    incr xmas_hits
  }
  # up-left
  if {$i / $width % $height > 3 && $i % $width > 3 && [sindex $input $i [expr {$i-$width-1}] [expr {$i-$w2-2}] [expr {$i-$w3-3}]] == "XMAS"} {
    puts $i
    incr xmas_hits
  }
}

puts "Count: $xmas_hits"

puts ""
puts {Part 2: }

puts "2: $"