advent-of-code/hacker-rank/flipping-bits/main.go

65 lines
1.2 KiB
Go
Raw Permalink Normal View History

2024-10-26 12:03:06 -06:00
package main
import (
"bufio"
"fmt"
"io"
"os"
"strconv"
"strings"
)
/*
* Complete the 'flippingBits' function below.
*
* The function is expected to return a LONG_INTEGER.
* The function accepts LONG_INTEGER n as parameter.
*/
func flippingBits(n int64) int64 {
// Write your code here
max := int64(^uint32(0))
return n ^ max
}
func main() {
reader := bufio.NewReaderSize(os.Stdin, 16 * 1024 * 1024)
stdout, err := os.Create(os.Getenv("OUTPUT_PATH"))
checkError(err)
defer stdout.Close()
writer := bufio.NewWriterSize(stdout, 16 * 1024 * 1024)
qTemp, err := strconv.ParseInt(strings.TrimSpace(readLine(reader)), 10, 64)
checkError(err)
q := int32(qTemp)
for qItr := 0; qItr < int(q); qItr++ {
n, err := strconv.ParseInt(strings.TrimSpace(readLine(reader)), 10, 64)
checkError(err)
result := flippingBits(n)
fmt.Fprintf(writer, "%d\n", result)
}
writer.Flush()
}
func readLine(reader *bufio.Reader) string {
str, _, err := reader.ReadLine()
if err == io.EOF {
return ""
}
return strings.TrimRight(string(str), "\r\n")
}
func checkError(err error) {
if err != nil {
panic(err)
}
}