-
Notifications
You must be signed in to change notification settings - Fork 0
/
07.tcl
executable file
·50 lines (50 loc) · 1.33 KB
/
07.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
#!/usr/bin/env tclsh
proc aoc_07 { } {
set d1 [aoc_read "07.data"]
set count1 0
set count2 0
foreach res_nums [lmap l [split $d1 "\n"] { split $l ":" } ] {
set res [lindex $res_nums 0]
set nums [lindex $res_nums 1]
puts -nonewline "."
flush stdout
incr count1 [recursive_search $res {"+" "*"} {*}$nums]
incr count2 [recursive_search $res {"+" "*" "||"} {*}$nums]
}
puts ""
return [list $count1 $count2]
}
proc recursive_search { num ops a b args } {
set ans_l [list]
foreach op $ops {
switch $op {
"+" { set ans [expr {$a + $b}] }
"*" { set ans [expr {$a * $b}] }
"||" { set ans "$a$b" }
}
if {$ans <= $num} {
lappend ans_l $ans
}
}
if {[llength $args] == 0} {
if {[lsearch $ans_l $num] != -1} {
return $num
} else {
return 0
}
}
foreach ans $ans_l {
set res [recursive_search $num $ops $ans {*}$args]
if {$res > 0} {
return $res
}
}
return 0
}
## -------------------------------------------------------------------
if {[file tail $argv0] eq [file tail [info script]]} {
source "rd.tcl"
# Example results: 3749 11387
# My results: 1620690235709 145397611075341
puts [aoc_07]
}