Last active
December 2, 2021 08:31
-
-
Save garethlewin/811baef67890dbbc06a93e3db27ccf2a to your computer and use it in GitHub Desktop.
This file contains 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
❯ 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) |
This file contains 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 ( | |
"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