-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
69 lines (55 loc) · 1.08 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package main
import (
"bufio"
"fmt"
"io"
"os"
"path"
"strconv"
"strings"
)
func input() *os.File {
input, err := os.Open(path.Join("2020", "9", "input.txt"))
if err != nil {
panic(err)
}
return input
}
func parse(r io.Reader) []int {
scanner := bufio.NewScanner(r)
var numbers []int
for scanner.Scan() {
row := scanner.Text()
v, _ := strconv.Atoi(row)
numbers = append(numbers, v)
}
if scanner.Err() != nil {
panic(scanner.Err())
}
return numbers
}
func solve(numbers []int, preambleSize int) int {
for i := preambleSize; i < len(numbers); i++ {
if !hasUniqueComposite(numbers[i], numbers[i-preambleSize:i]) {
return numbers[i]
}
}
panic("no solution")
}
func hasUniqueComposite(target int, nums []int) bool {
for _, i := range nums {
for _, j := range nums {
if i == j {
continue
}
if i+j == target {
return true
}
}
}
return false
}
func main() {
fmt.Println(solve(parse(strings.NewReader("35\n20\n15\n25\n47\n40\n62\n55\n65\n95\n102\n117\n150\n182\n127\n219\n299\n277\n309\n576")), 5))
fmt.Println(solve(parse(input()), 25))
}