-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
72 lines (58 loc) · 1.13 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
70
71
72
package main
import (
"io"
lib "github.com/teivah/advent-of-code"
)
func fs1(input io.Reader, length int) int {
numbers := lib.ReaderToInts(input)
var window []int
for i := 0; i < length; i++ {
window = append(window, numbers[i])
}
for i := length; i < len(numbers); i++ {
number := numbers[i]
if !sumPossible(number, window) {
return number
}
window = append(window[1:], number)
}
return -1
}
func sumPossible(target int, numbers []int) bool {
for i := 0; i < len(numbers)-1; i++ {
v := numbers[i]
for j := i + 1; j < len(numbers); j++ {
if v+numbers[j] == target {
return true
}
}
}
return false
}
func fs2(input io.Reader, target int) int {
numbers := lib.ReaderToInts(input)
l := 0
r := 0
sum := 0
var window []int
for r < len(numbers) {
sum += numbers[r]
window = append(window, numbers[r])
if sum == target {
return lib.MaxInts(window) + lib.MinInts(window)
}
r++
if sum < target {
continue
}
for sum > target {
sum -= numbers[l]
window = window[1:]
l++
if sum == target {
return lib.MaxInts(window) + lib.MinInts(window)
}
}
}
return -1
}