aboutsummaryrefslogtreecommitdiff
path: root/10
diff options
context:
space:
mode:
authorMistivia <i@mistivia.com>2024-12-23 14:23:42 +0800
committerMistivia <i@mistivia.com>2024-12-23 14:23:42 +0800
commit255a2911a2206376a85242443a6a7a107d6dbe42 (patch)
treebd002b26ecf00adb713f790fe8fe68587256569f /10
parent304d71df0590b579382cb49cb585d9629b750247 (diff)
day 10 part 2
Diffstat (limited to '10')
-rw-r--r--10/2.tcl56
1 files changed, 56 insertions, 0 deletions
diff --git a/10/2.tcl b/10/2.tcl
new file mode 100644
index 0000000..fecbe7f
--- /dev/null
+++ b/10/2.tcl
@@ -0,0 +1,56 @@
+set fp [open "input" r]
+set line {}
+while {[gets $fp line] >= 0} {
+ lappend lines $line
+}
+close $fp
+
+set height [llength $lines]
+set width [string length [lindex $lines 0]]
+
+proc out_of_range {x y} {
+ global height width
+ if {$x < 0 || $x >= $width} {
+ return 1
+ }
+ if {$y < 0 || $y >= $height} {
+ return 1
+ }
+ return 0
+}
+
+proc char_at {x y} {
+ global lines
+ if {[out_of_range $x $y]} {
+ return -1
+ }
+ return [string index [lindex $lines $y] $x]
+}
+
+proc find_path {x y expect} {
+ global path_cache
+ if {[out_of_range $x $y]} {
+ return 0
+ }
+ if {$expect != [char_at $x $y]} {
+ return 0
+ }
+ if {$expect == 9} {
+ return 1
+ }
+ set next [expr {$expect + 1}]
+ set ret [expr {
+ [find_path [expr {$x + 1}] $y $next]
+ + [find_path [expr {$x - 1}] $y $next]
+ + [find_path $x [expr {$y + 1}] $next]
+ + [find_path $x [expr {$y - 1}] $next]
+ }]
+}
+
+set sum 0
+for {set x 0} {$x < $width} {incr x} {
+ for {set y 0} {$y < $height} {incr y} {
+ incr sum [find_path $x $y 0]
+ }
+}
+puts $sum