diff options
Diffstat (limited to '2024')
-rw-r--r-- | 2024/input.01.txt (renamed from 2024/tcl/input.01.txt) | 0 | ||||
-rw-r--r-- | 2024/input.02.txt (renamed from 2024/tcl/input.02.txt) | 0 | ||||
-rw-r--r-- | 2024/input.03.txt (renamed from 2024/tcl/input.03.txt) | 0 | ||||
-rw-r--r-- | 2024/input.04.txt (renamed from 2024/tcl/input.04.txt) | 0 | ||||
-rw-r--r-- | 2024/input.05.txt (renamed from 2024/tcl/input.05.txt) | 0 | ||||
-rw-r--r-- | 2024/input.06.txt (renamed from 2024/tcl/input.06.txt) | 0 | ||||
-rw-r--r-- | 2024/input.07.txt (renamed from 2024/tcl/input.07.txt) | 0 | ||||
-rwxr-xr-x | 2024/tcl/06.tcl | 49 | ||||
-rw-r--r-- | 2024/tcl/lib.tcl | 13 |
9 files changed, 33 insertions, 29 deletions
diff --git a/2024/tcl/input.01.txt b/2024/input.01.txt index cf109b6..cf109b6 100644 --- a/2024/tcl/input.01.txt +++ b/2024/input.01.txt diff --git a/2024/tcl/input.02.txt b/2024/input.02.txt index 4eeead7..4eeead7 100644 --- a/2024/tcl/input.02.txt +++ b/2024/input.02.txt diff --git a/2024/tcl/input.03.txt b/2024/input.03.txt index 1da07fc..1da07fc 100644 --- a/2024/tcl/input.03.txt +++ b/2024/input.03.txt diff --git a/2024/tcl/input.04.txt b/2024/input.04.txt index a6917f0..a6917f0 100644 --- a/2024/tcl/input.04.txt +++ b/2024/input.04.txt diff --git a/2024/tcl/input.05.txt b/2024/input.05.txt index f8e5b0d..f8e5b0d 100644 --- a/2024/tcl/input.05.txt +++ b/2024/input.05.txt diff --git a/2024/tcl/input.06.txt b/2024/input.06.txt index 0cfd1af..0cfd1af 100644 --- a/2024/tcl/input.06.txt +++ b/2024/input.06.txt diff --git a/2024/tcl/input.07.txt b/2024/input.07.txt index 19ed730..19ed730 100644 --- a/2024/tcl/input.07.txt +++ b/2024/input.07.txt diff --git a/2024/tcl/06.tcl b/2024/tcl/06.tcl index fa48061..39f980e 100755 --- a/2024/tcl/06.tcl +++ b/2024/tcl/06.tcl @@ -1,37 +1,30 @@ #!/bin/env tclsh source lib.tcl -setup 6 +setup 6 grid puts {Part 1: Count unique guard coords} -set map(grid) {} -foreach line $input { - lappend map(grid) [split $line ""] -} -set map(h) [llength $map(grid)] -set map(w) [llength [lindex $map(grid) 0]] -set map(visits) {} -set map(debug) $map(grid) -set guard(y) [lsearch $map(grid) *^*] -set guard(x) [lsearch [lindex $map(grid) $guard(y)] ^] +set input(visits) {} +set guard(y) [lsearch $input(grid) *^*] +set guard(x) [lsearch [lindex $input(grid) $guard(y)] ^] set guard(dir) 0 set placed_obstructions {} set rot_map {{-1 0} {0 1} {1 0} {0 -1}} proc step_guard {} { - global guard rot_map map - dict set map(visits) [list $guard(y) $guard(x)] $guard(dir) 1 - lset map(debug) $guard(y) $guard(x) [lindex {^ > v <} $guard(dir)] + global guard rot_map input + dict set input(visits) [list $guard(y) $guard(x)] $guard(dir) 1 + lset input(debug) $guard(y) $guard(x) [lindex {^ > v <} $guard(dir)] lassign [lindex $rot_map $guard(dir)] ydir xdir set ynew [expr {$guard(y) + $ydir}] set xnew [expr {$guard(x) + $xdir}] - if {$ynew < 0 || $ynew >= $map(h) || $xnew < 0 || $xnew >= $map(w)} { return 0 } + if {$ynew < 0 || $ynew >= $input(h) || $xnew < 0 || $xnew >= $input(w)} { return 0 } - if {[lindex $map(grid) $ynew $xnew] == "#"} { + if {[lindex $input(grid) $ynew $xnew] == "#"} { set guard(dir) [expr {($guard(dir) + 1) % 4}] return 1 } @@ -44,45 +37,45 @@ proc step_guard {} { # sigh set potential_loops 0 while {[step_guard]} { - puts -nonewline "Unique coords: [llength [dict keys $map(visits)]]\r" + puts -nonewline "Unique coords: [llength [dict keys $input(visits)]]\r" flush stdout lassign [lindex $rot_map $guard(dir)] ydir xdir set ynew [expr {$guard(y) + $ydir}] set xnew [expr {$guard(x) + $xdir}] - if {$ynew < 0 || $ynew >= $map(h) || $xnew < 0 || $xnew >= $map(w)} continue + if {$ynew < 0 || $ynew >= $input(h) || $xnew < 0 || $xnew >= $input(w)} continue # it took me. 2 hours. to realize i needed the clause after the ||. god dammit. - if {[dict exists $placed_obstructions [list $ynew $xnew]] || [dict exists $map(visits) [list $ynew $xnew]]} { + if {[dict exists $placed_obstructions [list $ynew $xnew]] || [dict exists $input(visits) [list $ynew $xnew]]} { continue } - if {[dict exists $map(visits) [list $guard(y) $guard(x)] [expr {($guard(dir) + 1) % 4}]]} { -# foreach line $map(debug) { + if {[dict exists $input(visits) [list $guard(y) $guard(x)] [expr {($guard(dir) + 1) % 4}]]} { +# foreach line $input(debug) { # puts [join $line ""] # } dict incr placed_obstructions [list $ynew $xnew] 1 incr potential_loops - } elseif {[lindex $map(grid) $ynew $xnew] != "#"} { - set map_backup [array get map] + } elseif {[lindex $input(grid) $ynew $xnew] != "#"} { + set input_backup [array get input] set guard_backup [array get guard] - lset map(grid) $ynew $xnew "#" - lset map(debug) $ynew $xnew "#" + lset input(grid) $ynew $xnew "#" + lset input(debug) $ynew $xnew "#" while {[step_guard]} { - if {[dict exists $map(visits) [list $guard(y) $guard(x)] $guard(dir)]} { + if {[dict exists $input(visits) [list $guard(y) $guard(x)] $guard(dir)]} { dict incr placed_obstructions [list $ynew $xnew] 2 incr potential_loops break } } - array set map $map_backup + array set input $input_backup array set guard $guard_backup } } -puts "Unique coords: [llength [dict keys $map(visits)]]" +puts "Unique coords: [llength [dict keys $input(visits)]]" puts "" puts {Part 2: Number of potential looping obstructions} diff --git a/2024/tcl/lib.tcl b/2024/tcl/lib.tcl index 048bce5..588a5e8 100644 --- a/2024/tcl/lib.tcl +++ b/2024/tcl/lib.tcl @@ -5,7 +5,7 @@ proc setup {n {mode lines}} { set n [string range 0$n end-1 end] upvar input input - set fd [open input.$n.txt] + set fd [open ../input.$n.txt] switch -- $mode { lines { @@ -19,6 +19,17 @@ proc setup {n {mode lines}} { 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) + } } } |