From 255a2911a2206376a85242443a6a7a107d6dbe42 Mon Sep 17 00:00:00 2001 From: Mistivia Date: Mon, 23 Dec 2024 14:23:42 +0800 Subject: day 10 part 2 --- 10/2.tcl | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 10/2.tcl 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 -- cgit v1.0