-
Notifications
You must be signed in to change notification settings - Fork 0
/
05.tcl
executable file
·57 lines (54 loc) · 1.46 KB
/
05.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
#!/usr/bin/env tclsh
proc aoc_05 { } {
set d [aoc_read "05.data"]
## Parse input updates and write comparator with the rules.
set updates [list]
set code ""
foreach line [split $d "\n"] {
switch -glob $line {
"*|*" {
set rule [split $line "|"]
set a [lindex $rule 0]
set b [lindex $rule 1]
set code "$code
if {\$n1 eq \"$a\" && \$n2 eq \"$b\"} {
return -1
} elseif {\$n1 eq \"$b\" && \$n2 eq \"$a\"} {
return 1
}
"
}
"*,*" {
lappend updates [split $line ","]
}
}
}
set code "$code
return 0
"
proc cmp { n1 n2 } $code
## Sort updates.
set count1 0
set count2 0
foreach update $updates {
set update_s [lsort -command cmp $update]
if {$update eq $update_s} {
incr count1 [lmiddle $update]
} else {
incr count2 [lmiddle $update_s]
}
}
return [list $count1 $count2]
}
proc lmiddle { lst } {
set l [llength $lst]
set m [expr {int($l/2)}]
return [lindex $lst $m]
}
## -------------------------------------------------------------------
if {[file tail $argv0] eq [file tail [info script]]} {
source "rd.tcl"
# Example results: 143, 123
# My results: 5329, 5833
puts [aoc_05]
}