-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path11.tcl
executable file
·67 lines (59 loc) · 1.46 KB
/
11.tcl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env tclsh
proc aoc_11 { } {
set r [list]
set d [aoc_read "11.data"]
set count1 0
set count2 0
foreach stone $d {
incr count1 [count_stones 25 $stone]
incr count2 [count_stones 75 $stone]
}
return [list $count1 $count2]
}
proc count_stones { { steps 1} number } {
global cache
if {[info exists cache($steps.$number)]} {
return $cache($steps.$number)
}
set stones [list]
set number_l [split $number ""]
set number_sz [llength $number_l]
if {$number eq "0"} {
lappend stones 1
} elseif {($number_sz % 2) == 0} {
lappend stones [join0 {*}[lrange $number_l 0 [expr $number_sz/2-1]] ""]
lappend stones [join0 {*}[lrange $number_l [expr $number_sz/2] end] ""]
} else {
lappend stones [expr {$number * 2024}]
}
set count 0
set nsteps [expr $steps - 1]
foreach stone $stones {
if {$nsteps} {
incr count [count_stones $nsteps $stone]
} else {
incr count [llength $stone]
}
}
set cache($steps.$number) $count
return $count
}
proc join0 { args } {
set r ""
foreach e $args {
if {$r eq "" && $e eq "0"} {
continue
}
set r "$r$e"
}
if {$r eq ""} {
set r "0"
}
return $r
}
if {[file tail $argv0] eq [file tail [info script]]} {
source "rd.tcl"
# Example results: 55312, X
# My results: 209412,
puts [aoc_11]
}