From 50ae6b52a9e5ed2b92453ecbea0c36a10b8dd2c6 Mon Sep 17 00:00:00 2001 From: Mistivia Date: Wed, 18 Dec 2024 00:51:01 +0800 Subject: day 6 part 1 --- 06/1.tcl | 87 ++++++++++++++++++++++++++++++++++++++++++ 06/input | 130 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 217 insertions(+) create mode 100644 06/1.tcl create mode 100644 06/input diff --git a/06/1.tcl b/06/1.tcl new file mode 100644 index 0000000..49c41cf --- /dev/null +++ b/06/1.tcl @@ -0,0 +1,87 @@ +#!/usr/bin/env tclsh + +set fp [open input r] +set map "" +while {1} { + set ret [gets $fp line] + if {$ret == -1} { + break + } + if {[string length $line] == 0} { + break + } + lappend map $line +} + +proc char_at {x y} { + set line [lindex $::map $y] + return [string index $line $x] +} + +set height [llength $map] +set width [string length [lindex $map 0]] + +proc find_start {} { + for {set i 0} {$i < $::width} {incr i} { + for {set j 0} {$j < $::height} {incr j} { + if {[char_at $i $j] == "^"} { + return "$i $j" + } + } + } +} + +set start_point [find_start] + +proc front_point {x y direction} { + if {$direction == "up"} { + return [list $x [expr {$y - 1}]] + } elseif {$direction == "down"} { + return [list $x [expr {$y + 1}]] + } elseif {$direction == "left"} { + return [list [expr {$x - 1}] $y] + } elseif {$direction == "right"} { + return [list [expr {$x + 1}] $y] + } +} + +proc out_of_range {x y} { + return [expr { + ($x < 0) || ($x >= $::width) || ($y < 0) || ($y >= $::height) + }] +} + +proc turn_right {direction} { + if {$direction == {up}} { + return right + } elseif {$direction == {down}} { + return left + } elseif {$direction == {right}} { + return down + } else { + return up + } +} + +proc next_state {x y direction} { + set forward [front_point $x $y $direction] + if {[out_of_range {*}$forward]} { + return [list {*}$forward $direction] + } + if {[char_at {*}$forward] == {#}} { + set forward [front_point $x $y [turn_right $direction]] + return [list {*}$forward [turn_right $direction]] + } else { + return [list {*}$forward $direction] + } +} + +set state [list {*}$start_point up] +set visited {} + +while {![out_of_range {*}[lrange $state 0 1]]} { + dict set visited [lrange $state 0 1] {} + set state [next_state {*}$state] +} + +puts [dict size $visited] diff --git a/06/input b/06/input new file mode 100644 index 0000000..9c6bad2 --- /dev/null +++ b/06/input @@ -0,0 +1,130 @@ +.......#.........#..............#........................#......#....#............................#..#...............#............ +...................#.............................................#...................................#............................ +...............#.................................##.........#..........#.....##...#...#.....#....#..................#............. +#.............................................#..#.............#..........#.##.............................................#...... +#.........................................................#.....#........................................##....................... +...........#.......#.#...........#....#.#...#......................................#...........................#...##............# +........................................................#..#.....................................#..#................#....#...#... +#.#.....#.........................#...........#.......................................#.......#.....................##............ +.......#..........##.....#........#..................................................#.........................................#.# +#.#..............#.#.............................#............#....#...............#.........................#............#..#.#.# +..........................................................#....................................................#.................. +.......................#....#...........#.....................#....#...#.......#..............#.......................#........... +..............................#..............#.......#....................................#.....................#...#...#......... +........#.................#...#....##..............#...........................#............................#.................#... +..................#........................................#......#......................................................#........ +.............#.................#...#..............................................................................#...#........... +.....................................#..........................................................#.#.#......#.........#............ +..........#...................................................................#.........##.......#..#............................. +...#.#....................................#..#...........#.......................................................##............... +........................................................................................................#......................... +.........#...........#......#..............#........#....................#...#......#............................................. +........#....#...............#....................#..........#.....................................#......#......#........#.#..... +........................#..........................#.......#........................#................#......#..................... +......................................#.........................................#............................#.................... +...................................#.#...#...........#.........................................#.....................#............ +.................#....#.....................................................................#..................#.........#........ +...........#...........................................................#.............................#...............#..#......... +..#.....................#......................................................................................................... +....................#....#.#...........................#...........................................#..............#............... +...#...................................................#.............................................#.........#..........#....... +.............................###.....#..#..#...................................................................................... +##................#.#.......#..............#.....#...............#..#............#................................................ +......#...............................................#.#..#............................#............#.................#.......... +........................................#.#...................................................#.....#............................. +#.......#.#.......................#.....#....................................#...................#................................ +...##................................................................................................#.....................#...... +............#.............#..................#.................#.................................................................. +...#..............#..............................................................................#..........#......#.............. +...#..#...#................................................#.....................#......#......................#.......#.......... +.................#..#........#...............#.#................#......##.................#.................................#..... +#.....#.......#...........................................#..........#..............................#...#.#..#......#.....#....... +....................................#.........#........#..................#.............................................#......... +#...............................#........................#.................................................#.....#................ +..#..................................#.......#.....................#.............................................................. +........#...#.....................................#......#..............#..#..#..........................#.#....#................. +.............#..........#....#................#.......#..........................................#.....#.......................... +.............................................#................................#.........................#...#..................... +.........#........#...................................................................#.......#....#.............................. +....................................#.......................#..............#...............................#...................... +....#....................#................................#....#...........#.............#........................................ +....#...........#...................#.#..................................................#.....................#.................. +..........#....................#............#.##...#.......#....#...............#.....#...#.........................#............. +.......#....................................................................................................#.................#... +...............................................................................#......#.................#...#..................... +..................#........#..........#..........................................................................#................ +..........................................................................................#....................................... +.#..#...........#.................................##...........................#.....#...................#..#..................... +........................#..#..........................................................#............#..................#..........# +...........#...................................................##...........................#...............................#..... +........#......................................................................................................#.................. +........#..#............................#..#...........#..............................#......................#.##...........#....# +........#...#.#..............#...#.....................#....................#.#..............................#.................... +.......................................................#....................................................#..#.................. +...........................................#......................#................#........................#....................# +.............#...............#................................................................................#................... +..#...#............#.............#.....#........................#............#....................#............................... +.....#...................................................................................................................#........ +....................#..#...........#....#.............................................................................#.......#... +..............#......#.........................#...........#...#...........................................#...#.................. +..............................#.....#.........................................................#......#.#......#................... +............................................................#...................#.#............................#.................. +.............###.#........................................#........................................................#.............. +......#................................#...............##......................................................................... +.........#.....#.#.....................#.......................................#...............#..........#....#....#............. +..............................................#....................................#...........................................#.. +............#.#..........#........#..#.....#.............#..........#............#......#...................##..........#......... +............#....#...................................#.............................#.........................#.................... +.#..................................#....#.........................................................................#.............. +.........................#...#...#.................#..................#.............#.#...#...............#....................... +.........#...............................#...........#.............................#...........................#..........#....... +......#..........................#.............#..........#....................................................................... +.......#...............#.........#.............#...............................#.................................................. +..#......#....................##...........................................#........................................#............. +..........................#.....#.......................#...........................................................#.#........... +..#....................#.............#................................................................#.#......................... +.........#....................#.....#....#...............#.......................#........#....................................... +..............#...................................................................#................#...#.......................... +....................#.....#.......#..#......................................#...........#..................................##....# +.......#.......................................................................#.................................................. +......#.............##..........................#..#......................^....................................................... +..............................#..........#...................#.................................#.....................#............ +.......................##..........................#.......#..............................#...................................#.#. +................#....................................................................#...............................#.........#.. +..........................................#......#.....................................#................#........#................ +#......................................#.......................................................#..#...........#................... +.......#.............#..................#..#...#............#.......#.....#.....#.#............................................... +.............................#........#.............#.....#.......#...................................#.#..................#...... +.................#......................................................................................#........#..#............. +........................................................................#..............#.....#..............................#..... +....#....#..........................................#......##...#....................#...................#..........#............. +.................##.......................##.#.........#................................................#......................... +.#..............................................#.........................#.#.#..........#.....................................##. +............................................................#..........#....#...................................................#. +....#...........#.............................#.....................#.........#..................#...........................#.... +......#.#.......#..#......#........#..#...................#....#.................................................................. +............#........................................................................................................#............ +.....................................................#..........................................#................#................ +...#.......#..#..............#.............................#....................................#................................. +.................##.........#........................#....................................#...............#.#.#................... +................#......#......................#..#..........................................#..................................... +......#..................................................##.#.#........#...........................#.......................#...... +........................#............#......#.........#.............#.............................................#.....#.##...... +......................#........................................#.......#.........#.................#..#...........#............... +..#.......#.........##..#.#................................................................................#.........#............ +........................................................................................#............#...........#................ +................#...#....................#..##..#...............#.............................................................#... +...#........#..........................#.....#..........................#....................................#.........#.......##. +.................#...........................#.........#......#............................#.....................#................ +...................#..........#....#.......#...................#.............................#..........#......................... +.......#..............#.................................#.........#....................................#.................#....##.# +..................................#.......................#....#..#.........................#....................#................ +.....#.........#............#...#.........#........#...................#...#......................#.....#............#............ +#.#....#...#........#................................##..........................#.............................##...#....#........ +.............##..........##...#......#.......#.#................#.#.......................#.............#......................... +.............#....................................#....................................#.......................................... +..................................#..............................#.......................................#................#....... +...................#...............#.............................................................................................. +....#........#.................................#........#.....................................#.......#................#........#. +.................#......#.......................................................##.....#..................#....................... +...............#.......................#.#........#...........................................#........#.......................... -- cgit v1.0