Created
May 24, 2020 18:11
-
-
Save caffeinatedgaze/0c1fcd221d6b9c81b9f8a057dc1b28ec to your computer and use it in GitHub Desktop.
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 ( | |
"fmt" | |
"os" | |
"io" | |
) | |
type Tuple struct { | |
is_there bool; | |
count int | |
} | |
func readFile(file *os.File) (numbers map[int]Tuple) { | |
var line int | |
numbers = make(map[int]Tuple) | |
for { | |
_, err := fmt.Fscanf(file, "%d", &line) | |
if err == io.EOF { | |
return | |
} | |
if numbers[line].is_there { | |
numbers[line] = Tuple{true, numbers[line].count + 1} | |
} else { | |
numbers[line] = Tuple{true, 1} | |
} | |
} | |
} | |
func main() { | |
filename := "input.txt" | |
file, _ := os.Open(filename) | |
var target int | |
fmt.Fscanf(file, "%d\n", &target) | |
numbers := readFile(file) | |
output, _ := os.Create("output.txt") | |
for key, _ := range numbers { | |
needed_value := target - key | |
if numbers[needed_value].is_there { | |
if needed_value == key && numbers[needed_value].count == 1 { | |
continue | |
} | |
output.WriteString("1\n") | |
return | |
} | |
} | |
output.WriteString("0\n") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment