-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path02.tcl
executable file
·50 lines (48 loc) · 1.11 KB
/
02.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_02 { } {
set d [aoc_read "02.data"]
set count1 0
set count2 0
foreach l [split $d \n] {
incr count1 [aoc_is_save_1 $l]
incr count2 [aoc_is_save_2 $l]
}
return [list $count1 $count2]
}
proc aoc_is_save_1 { l } {
set chk "0"
for {set i 1} {$i < [llength $l]} {incr i} {
set a [lindex $l $i-1]
set b [lindex $l $i]
set d [expr {$b - $a}]
if "!(abs(\$d) <= 0 || abs(\$d) >= 4 || $chk)" {
if {$chk eq "0"} {
if {$d < 0} {
set chk {$d > 0}
} else {
set chk {$d < 0}
}
}
} else {
return 0
}
}
return 1
}
proc aoc_is_save_2 { l } {
if {[aoc_is_save_1 $l]} {
return 1
}
for {set i 0} {$i < [llength $l]} {incr i} {
if {[aoc_is_save_1 [lreplace $l $i $i]]} {
return 1
}
}
return 0
}
if {[file tail $argv0] eq [file tail [info script]]} {
source "rd.tcl"
# Example results: 2, 4
# My results: 242, 311
puts [aoc_02]
}