Last active
December 3, 2024 22:46
-
-
Save Micrified/7150452a1392e87f7ef7e80a23bc2fa0 to your computer and use it in GitHub Desktop.
2b.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"os" | |
"fmt" | |
"sync" | |
"strings" | |
"strconv" | |
) | |
func validate(b string) (<-chan int) { | |
r := make(chan int) | |
go func(ns []string) { | |
gt, leq := false, false | |
defer close(r) | |
for i := 0; i < len(ns) - 1; i++ { | |
p, _ := strconv.Atoi(ns[i]) | |
q, _ := strconv.Atoi(ns[i+1]) | |
d := p - q | |
if p > q { | |
gt = gt || true | |
} else { | |
d = -d | |
leq = leq || true | |
} | |
if d < 1 || d > 3 || (gt && leq) { | |
r <- 0 | |
break | |
} | |
} | |
r <- 1 | |
}(strings.Split(b, " ")) | |
return r | |
} | |
func main() { | |
n, g := 0, sync.WaitGroup{} | |
s, c := bufio.NewScanner(os.Stdin), make(chan int) | |
for n = 0; s.Scan(); n++ { | |
go func(line string) { | |
c <- <-validate(line) | |
g.Done() | |
}(s.Text()) | |
} | |
go func(n int) { | |
g.Add(n) | |
g.Wait() | |
close(c) | |
}(n) | |
sum := 0 | |
for k := range(c) { | |
sum += k | |
} | |
fmt.Printf("%d\n", sum) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment