2023 Days 4 and 6
This commit is contained in:
parent
9cc373290b
commit
069aa736dc
7 changed files with 404 additions and 0 deletions
3
2023/06/go.mod
Normal file
3
2023/06/go.mod
Normal file
|
@ -0,0 +1,3 @@
|
|||
module tristans.cloud/aoc
|
||||
|
||||
go 1.20
|
53
2023/06/main.go
Normal file
53
2023/06/main.go
Normal file
|
@ -0,0 +1,53 @@
|
|||
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if len(os.Args) < 3 {
|
||||
println("usage: part1 <time> <distance>");
|
||||
return;
|
||||
}
|
||||
time, err := strconv.Atoi(os.Args[1]);
|
||||
if err != nil {
|
||||
println("time must be a number!");
|
||||
}
|
||||
dist, err := strconv.Atoi(os.Args[2]);
|
||||
if err != nil {
|
||||
println("dist must be a number!");
|
||||
}
|
||||
|
||||
minimum := min(0, time, time, dist);
|
||||
|
||||
maximum := time - minimum - 1;
|
||||
|
||||
variety := maximum - minimum;
|
||||
|
||||
println(variety);
|
||||
|
||||
}
|
||||
|
||||
func min(start, end, time, dist int) int {
|
||||
|
||||
if end - start < 2 {
|
||||
return start
|
||||
}
|
||||
|
||||
guess := start + ((end-start) / 2);
|
||||
|
||||
res := calc(guess, time);
|
||||
|
||||
if res > dist {
|
||||
return min(0, guess, time, dist);
|
||||
}
|
||||
return min(guess, end, time, dist);
|
||||
|
||||
}
|
||||
|
||||
func calc(pushtime, totaltime int) int {
|
||||
return (totaltime*pushtime) - (pushtime*pushtime)
|
||||
}
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue