about summary refs log tree commit diff
path: root/aoc
diff options
context:
space:
mode:
authorNguyễn Gia Phong <mcsinyx@disroot.org>2022-12-09 19:07:26 +0900
committerNguyễn Gia Phong <mcsinyx@disroot.org>2022-12-09 19:07:26 +0900
commitcde44cdda55d0a0139a8e6a98e1aa47831972395 (patch)
tree0cfc6ca45def36ff8ff0d4ec6b44167f5447a2b4 /aoc
parentf8b0528d933f4b7ac64c8460ed765e8e41240d7d (diff)
downloadcp-cde44cdda55d0a0139a8e6a98e1aa47831972395.tar.gz
[aoc/2022] Finish day 9
Diffstat (limited to 'aoc')
-rwxr-xr-xaoc/2022/09/part-one.tcl25
-rwxr-xr-xaoc/2022/09/part-two.tcl30
2 files changed, 55 insertions, 0 deletions
diff --git a/aoc/2022/09/part-one.tcl b/aoc/2022/09/part-one.tcl
new file mode 100755
index 0000000..3e7716a
--- /dev/null
+++ b/aoc/2022/09/part-one.tcl
@@ -0,0 +1,25 @@
+#!/usr/bin/env tclsh
+proc vec x {list [expr {abs($x)}] [expr {($x>0)-($x<0)}]}
+set tx [set hx 0]
+set ty [set hy 0]
+while {1} {
+    set line [gets stdin]
+    if {[eof stdin] || $line == ""} then break
+    lassign [split $line] d n
+    for {set i 0} {$i < $n} {incr i} {
+        switch $d {
+            R {incr hx 1}
+            U {incr hy 1}
+            L {incr hx -1}
+            D {incr hy -1}
+        }
+        lassign [vec [expr {$hx-$tx}]] mx dx
+        lassign [vec [expr {$hy-$ty}]] my dy
+        if {$mx == 2 || $my == 2} {
+            incr tx $dx
+            incr ty $dy
+        }
+        set v($tx,$ty) 1
+    }
+}
+puts [array size v]
diff --git a/aoc/2022/09/part-two.tcl b/aoc/2022/09/part-two.tcl
new file mode 100755
index 0000000..e3d539f
--- /dev/null
+++ b/aoc/2022/09/part-two.tcl
@@ -0,0 +1,30 @@
+#!/usr/bin/env tclsh
+proc vec x {list [expr {abs($x)}] [expr {($x>0)-($x<0)}]}
+for {set i 0} {$i < 10} {incr i} {
+    set kx($i) 0
+    set ky($i) 0
+}
+while {1} {
+    set line [gets stdin]
+    if {[eof stdin] || $line == ""} then break
+    lassign [split $line] d n
+    for {set i 0} {$i < $n} {incr i} {
+        switch $d {
+            R {incr kx(0) 1}
+            U {incr ky(0) 1}
+            L {incr kx(0) -1}
+            D {incr ky(0) -1}
+        }
+        for {set j 1} {$j < 10} {incr j} {
+            set p [expr {$j-1}]
+            lassign [vec [expr {$kx($p)-$kx($j)}]] mx dx
+            lassign [vec [expr {$ky($p)-$ky($j)}]] my dy
+            if {$mx == 2 || $my == 2} {
+                incr kx($j) $dx
+                incr ky($j) $dy
+            }
+        }
+        set v($kx(9),$ky(9)) 1
+    }
+}
+puts [array size v]