day 17a complete

This commit is contained in:
Luke Else 2021-12-17 22:33:26 +00:00
parent b2f39abd93
commit 8a8a1c0ebf
4 changed files with 128 additions and 0 deletions

111
day17/17a/main.go Normal file
View File

@ -0,0 +1,111 @@
package main
import (
"AdventOfCode2021/shared"
"bufio"
"fmt"
"os"
"regexp"
"strconv"
)
func main() {
content := returnContent("../input")
//content := returnContent("../testInput")
coordLeft := shared.Coordinate{
X: shared.Min((*content)[0], (*content)[1]),
Y: shared.Max((*content)[2], (*content)[3]),
}
coordRight := shared.Coordinate{
X: shared.Max((*content)[0], (*content)[1]),
Y: shared.Min((*content)[2], (*content)[3]),
}
y := 0
for {
for x := 1; x < coordRight.X; x++ {
probe := Probe{
Position: &shared.Coordinate{X: 0, Y: 0},
Velocity: &shared.Coordinate{X: x, Y: y},
}
success, maxY := probe.Model(&coordLeft, &coordRight)
if success {
fmt.Println(y, maxY)
}
}
y++
}
}
type Probe struct {
Position *shared.Coordinate
Velocity *shared.Coordinate
}
func (p *Probe) Step() {
p.Position.X += p.Velocity.X
p.Position.Y += p.Velocity.Y
if p.Velocity.X > 0 {
p.Velocity.X--
} else if p.Velocity.X < 0 {
p.Velocity.X++
}
p.Velocity.Y--
}
func (p *Probe) Model(left *shared.Coordinate, right *shared.Coordinate) (success bool, maxY int) {
maxY = 0
for p.Position.X <= right.X && p.Position.Y >= right.Y {
p.Step()
if p.Position.Y > maxY {
maxY = p.Position.Y
}
if CheckLocation(p, left, right) {
return true, maxY
}
}
return false, maxY
}
func CheckLocation(probe *Probe, left *shared.Coordinate, right *shared.Coordinate) bool {
if probe.Position.X >= left.X && probe.Position.X <= right.X {
if probe.Position.Y <= left.Y && probe.Position.Y >= right.Y {
return true
}
}
return false
}
func returnContent(path string) *[]int {
//read file and return it as an array of integers
file, err := os.Open(path)
var content []int
if err != nil {
fmt.Println("Unlucky, the file didn't open")
return &content
}
defer file.Close()
scanner := bufio.NewScanner(file)
regex, _ := regexp.Compile(`[-+]?[.]?[\d]+`)
strings := []string{}
for scanner.Scan() {
strings = regex.FindAllString(scanner.Text(), 4)
}
for _, val := range strings {
num, _ := strconv.Atoi(val)
content = append(content, num)
}
return &content
}

1
day17/input Normal file
View File

@ -0,0 +1 @@
target area: x=209..238, y=-86..-59

1
day17/testInput Normal file
View File

@ -0,0 +1 @@
target area: x=20..30, y=-10..-5

15
shared/Math.go Normal file
View File

@ -0,0 +1,15 @@
package shared
func Max(x int, y int) int {
if x >= y {
return x
}
return y
}
func Min(x int, y int) int {
if x <= y {
return x
}
return y
}