-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
60 lines (50 loc) · 1.47 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
package main
import (
"fmt"
aoc "github.com/shraddhaag/aoc/library"
)
func main() {
input := aoc.ReadFileLineByLine("input2.txt")
fmt.Println("answer for part 1: ", ans1(input))
fmt.Println("answer for part 2: ", ans2(input))
}
func ans1(input []string) int {
answer := 1
time := aoc.FetchSliceOfIntsInString(input[0])
distance := aoc.FetchSliceOfIntsInString(input[1])
for i, t := range time {
answer *= findNoOfWaysToWin(t, distance[i])
}
return answer
}
func ans2(input []string) int {
time := aoc.FetchNumFromStringIgnoringNonNumeric(input[0])
distance := aoc.FetchNumFromStringIgnoringNonNumeric(input[1])
return findNoOfWaysToWin(time, int(distance))
}
// this is the core of the problem.
// first we count number of ways we will NOT win when starting from hold time 0.
// in each iterations: check if current distance is greater than given distance:
// - greater: break loop
// - else: increase counter, increase hold time and continue
//
// we will get the same number of count if we were to start counting from hold time T.
//
// this means:
// - total number of ways to not win: count * 2
// - total number of ways: time + 1 (because we start from 0 instead of 1)
//
// so: total number of ways to win = time +1 - (count * 2)
func findNoOfWaysToWin(time, distance int) int {
var count int
var hold int
for ; hold <= time; hold++ {
dist := hold * (time - hold)
if dist <= distance {
count++
} else {
break
}
}
return time - (count * 2) + 1
}