Browse Source

day 11 part 1

Mistivia 3 months ago
parent
commit
1cc355995d
2 changed files with 41 additions and 0 deletions
  1. 40 0
      11/1.tcl
  2. 1 0
      11/input

+ 40 - 0
11/1.tcl

@@ -0,0 +1,40 @@
+set fp [open "input"]
+gets $fp line
+close $fp
+
+proc is_even {x} {
+    return [expr {$x % 2 == 0}]
+}
+
+proc zero_trim {x} {
+    for {set i 0} {$i < [expr {[string length $x] - 1}]} {incr i} {
+        if {[string index $x $i] != 0} {
+            break
+        }
+    }
+    return [string range $x $i end]
+}
+
+proc blink {x} {
+    if {$x == 0} {
+        return 1
+    }
+    if {[is_even [string length $x]]} {
+        set len [string length $x]
+        set halflen [expr {$len / 2}]
+        return [list [zero_trim [string range $x 0 [expr {$halflen - 1}]]] \
+                     [zero_trim [string range $x $halflen end]]]
+    }
+    return [expr {$x * 2024}]
+}
+
+set lst $line
+for {set i 0} {$i < 25} {incr i} {
+    set new_lst {}
+    foreach x $lst {
+        lappend new_lst {*}[blink $x]
+    }
+    set lst $new_lst
+}
+
+puts [llength $lst]

+ 1 - 0
11/input

@@ -0,0 +1 @@
+5178527 8525 22 376299 3 69312 0 275