diff options
author | Aleteoryx <alyx@aleteoryx.me> | 2024-12-07 15:22:26 -0500 |
---|---|---|
committer | Aleteoryx <alyx@aleteoryx.me> | 2024-12-07 15:22:26 -0500 |
commit | f62e9e8045844195febe990ddc0f049d1bc45902 (patch) | |
tree | f9108604b6f4a94a67cd1868efab40fc2860ae73 | |
parent | 92dd4115528a495a2acf3958de52d5511fc3108a (diff) | |
download | adventofcode-f62e9e8045844195febe990ddc0f049d1bc45902.tar.gz adventofcode-f62e9e8045844195febe990ddc0f049d1bc45902.tar.bz2 adventofcode-f62e9e8045844195febe990ddc0f049d1bc45902.zip |
2024.6, finally
-rwxr-xr-x | 2024/tcl/06.tcl | 90 | ||||
-rw-r--r-- | 2024/tcl/input.06.txt | 130 |
2 files changed, 220 insertions, 0 deletions
diff --git a/2024/tcl/06.tcl b/2024/tcl/06.tcl new file mode 100755 index 0000000..fa48061 --- /dev/null +++ b/2024/tcl/06.tcl @@ -0,0 +1,90 @@ +#!/bin/env tclsh + +source lib.tcl +setup 6 + +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 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)] + + 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 {[lindex $map(grid) $ynew $xnew] == "#"} { + set guard(dir) [expr {($guard(dir) + 1) % 4}] + return 1 + } + + set guard(y) $ynew + set guard(x) $xnew + return 1 +} + +# sigh +set potential_loops 0 +while {[step_guard]} { + puts -nonewline "Unique coords: [llength [dict keys $map(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 + + # 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]]} { + continue + } + if {[dict exists $map(visits) [list $guard(y) $guard(x)] [expr {($guard(dir) + 1) % 4}]]} { +# foreach line $map(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] + set guard_backup [array get guard] + + lset map(grid) $ynew $xnew "#" + lset map(debug) $ynew $xnew "#" + + while {[step_guard]} { + if {[dict exists $map(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 guard $guard_backup + } +} + +puts "Unique coords: [llength [dict keys $map(visits)]]" + +puts "" +puts {Part 2: Number of potential looping obstructions} + +puts "Potential count: [llength [dict keys $placed_obstructions]]" diff --git a/2024/tcl/input.06.txt b/2024/tcl/input.06.txt new file mode 100644 index 0000000..0cfd1af --- /dev/null +++ b/2024/tcl/input.06.txt @@ -0,0 +1,130 @@ +.............#........................................................#...#...........................................#........... +.....#...............................#.........................................#...#...............#........##...............#.#.. +..................................#..................#.......................................#...#................................ +.........................................#................#....#..............................................#................#.# +........#.........................................................#...........#.......#..............................#............ +........................#.............................#.......................#.......#.......................#........#.........# +................................#.......#............................................#...........................................# +...........#........#...#..#...........................#..................#.............#.......#..........................#...... +............................................#........#.......#..#......#..............................#..#................##...... +............#..#...#......#.........................................##.......#..........#.......#...................#............. +.....................................................................................#.....#...................................... +.......................................................#................#.....................................#................... +...........#.....#.#....................................#.......#.#...........#................#.......#.................#........ +............#.............#....................#....#............................#.........................................#...... +.#..........#......#................#........................#............#..#..............#..................................... +..................#....#...........................................#.....#.....#.........................................#.#...... +.............#..............#..#...........................#..#..........#...............................#........................ +.................................................................................................#................................ +.......................................................#..............#..............................................#..#......... +......#............................#.......................#................................................................#..... +..........#.........#..................#....#....#...#......#.........#.................................#........................# +..#...#............................#............#.......###.................................#.............................#...#... +.................#....#...................#........................................................................#..........#... +....#..........................#......................................................................#.....#......#.............. +.#........................................................................#..........#............#..................#............ +...................##.#.......................#....#.#........................................#.........##........................ +#...........................................................#..........#........#.#..................................#............ +.......#...#.........................................#.......#........#...............##..#....................................... +......................#....#...............................#......................................................#..#............ +......#.....................................................#...............................#......................#..#........... +.........................#.#...................................................................................................... +.......................................#...............#...................#................#..........#.#......................#. +.......#......#...#...........................#.........................#...............#..#...............#.............##....... +.....#..#..#..................#............#......................................................#...#.........................#. +#.#...............#...........#................#...................................................#..........#................... +..........................................................................#...............................................#....... +.........#.......#..........................#.##.............................#..##...#.......................................#.... +....#...............#..........#........................................#.....#.........................................#......... +...........#................................................#....##...................................................#..#........ +........................#........#...........#...#............................................#......................##........... +......#........................................#...#.............................................#................................ +#.....#......#..................................#.............#..#...............................#................................ +..................#......................#..........#...................#............................................#....#....... +.................#.............................................................................#.......#.......................... +......#........................................................................................#...#....................#......... +....#......................#..................##..................#.......#.......................#...........#.........#..#...... +............................................................#......##.............................#.#.....#............#.......... +..............#.......................#...^.....................................#.....................................#........... +..............#...##......#..#.....#..........................................................................#.......#.........#. +.#................................................................#........##......#....#.......#................................. +....#.....................#........#...........#.............................................#........#................#......#..# +...................#.............#.........#.........................#...........#..................#...................#....#.... +.#...............................................#.....#..#...#................................................................... +........................##............#.....................................#........#..................................#......... +..............#................................#.....................................#...................................#........ +...........#..........#...........#........#...................#..#..........................#.................................... +.#..............................................................#....#.......#.....................................#.............. +.....................#........#......#................#.............#........................#.......................#........#... +.........................#..............#...#....#..................#............................................................. +..#............................##.........................................................#...........................#.#..#...... +.....#............#...##........#..........#....#......#................................#.................#...............#....#.. +.....#...............#.............................#............#......#....#........................................#............ +...#..........#.........#.....................##.......#.....#...................#................................................ +.................................#...........................................#.................#...........................#...... +..........................................................#........................#.......#.................................#...# +..#......#......................................#...#..#....................#...........#....#.............................#...... +..#........#.............#......................#...#.....#....#........................#..#...................................... +.........#...........#.......................................................................#.........#....#...#................. +..................................#....#.............##....#.......#.........#.........................................#.......... +....#.................................................#.......#...........#.......................................##.....#........ +..............................#.........................................................#.....#........................#.......... +...#........#...........#..#..................................................#..............#.............................#...... +................................................#...............................................................#.....#..#.....#.. +...............#.........................#..#...................#....................................#................#........... +............................#...........................................................................................#......... +.....#.........................#.......#..#..##.............................#................##.......##............#............. +.........#........................................................................................................................ +.........................#............................#........................#.....#..........#.....................#...#..#.... +.......#........#..............................#..........................#...#.................#............#.................... +#...#..#...............................#..........................................#...#...#.........................#............. +...........................................................#..............#..........#..................#......................... +...........................................................#..............................................................#....#.. +...........#.......................#................#......#.....................................#..................#.....#....... +.....#.................................#........................#.#...................#.....#............#........................ +...........................#.#............#..............#..............#..#...#..........................#................#...... +.........#...................#........................#......#..#..................................................#.............. +.............................................#.................................................................#.......#.......#.. +#.......................#...............#.......................#...........#................................#.................... +......................................#............#....................................................##...................#.... +#...............#...........................................#......#....................................#...............#......... +...#...............#.................................#..................................#..#............#......................... +......................#..#............#..#..............................................#......#....#...............#............. +..................#.....................#.....#...................#..#................#....#..........#......#..........#......... +.......................#.....#..............#..........................#............#...................#.#....................... +......#...........#..............#........#........................................#......................................#....... +............#................#..##....................................................................#........................#.. +...........#....................................#.............................#.....#....#...#.#......................#........... +...................................................#...............................#....................#.........#............... +........#......#..#................#..................................................................#.................#......... +.............................#................................................#...........#.....#....#............................ +........#.............#.........#............#....#.....................#........#...............#................................ +..#................................#................................#..#......#.............................................#..... +.........#...........................................#..................................#....#..#.....................#........... +.....................................................#.........................................................#.................. +.........#...#.#.....#.......##................................#...#..........................#........#.......................... +......................##...............................#.......................................................................... +.........#...#.....#........................................#................#........#............#........#..................... +...............#............#.................................................................#................................#.. +......#..............#...................................................#............#.....................#..................... +.........#.........#.............................................................................................................. +.#........#...............#..##......#..............................#...........#..#.........#...........................#........ +...................#................................................................................#...............#............. +..............................................................................#........................#................#......... +...........#.....#..#.#....................................................#......#......#..#................##................... +....#......#...................................................................#..............#.....#.........#..................# +..#...........#...#...........#.......##..........##......................#......................................................# +..................#................................................#.#............................................................ +#................................................................#............#...##..........................................#... +......#............................................#.......#.#.................................................#.................. +..............................#..............#.#....#............#..........................#..................................... +............................#..................................#.................#..#...........................................#. +.........................................#................#..................#...#............................#..#......#........# +....................#..............................................................#..............................#..........##... +...#......................................#...........#..#............................................#.....#....#................ +................#..#....#......................................................................................................... +...................................#...................#...............................#................................#.......#. +..........#.................#.............#..................#...............................................................#.... +.....#...........#........#................#.........................................#..........................#......##......... +..........................#................#..............................................................#....................... +........#.................................................................#......................#.#..........................#... |