Skip to content

Instantly share code, notes, and snippets.

@garethlewin
Last active Dec 2, 2021
Embed
What would you like to do?
❯ benchstat v3.txt v4_garman_bytes.txt
name old time/op new time/op delta
Answer-8 136ns ± 0% 23ns ± 0% -83.28% (p=0.016 n=4+5)
❯ benchstat v4_fugi.txt v4_garman_bytes.txt
name old time/op new time/op delta
Answer-8 37.8ns ± 0% 22.8ns ± 0% -39.66% (p=0.008 n=5+5)
package main
import (
"fmt"
"io/ioutil"
"os"
"strings"
)
func main() {
if err := run(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func run() error {
input, err := ioutil.ReadAll(os.Stdin)
if err != nil {
return err
}
answer, err := Answer(strings.TrimSpace(string(input)))
fmt.Printf("answer: %v\n", answer)
return err
}
func Answer(input string) (int, error) {
increases := -1
var prev, intVal int64
for _, c := range []byte(input) {
if c != '\n' {
intVal = intVal*255 + int64(c)
} else {
if intVal > prev {
increases++
}
prev = intVal
intVal = 0
}
}
if intVal > prev {
increases++
}
return increases, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment