diff options
author | Julien Dessaux | 2021-12-19 21:28:34 +0100 |
---|---|---|
committer | Julien Dessaux | 2021-12-20 23:16:44 +0100 |
commit | 3f49055c82a8885e108d1bac3226654f156fad58 (patch) | |
tree | c385c2373108ff9ea9c4e7f9129d6ff2c6d3eb33 /2021 | |
parent | Added solutions for 16th day: Packet Decoder (diff) | |
download | advent-of-code-3f49055c82a8885e108d1bac3226654f156fad58.tar.gz advent-of-code-3f49055c82a8885e108d1bac3226654f156fad58.tar.bz2 advent-of-code-3f49055c82a8885e108d1bac3226654f156fad58.zip |
Added solutions for 17th day: target practice
Diffstat (limited to '2021')
-rw-r--r-- | 2021/17/example | 1 | ||||
-rw-r--r-- | 2021/17/first.go | 69 | ||||
-rw-r--r-- | 2021/17/input | 1 | ||||
-rw-r--r-- | 2021/17/second.go | 54 |
4 files changed, 125 insertions, 0 deletions
diff --git a/2021/17/example b/2021/17/example new file mode 100644 index 0000000..a07e02d --- /dev/null +++ b/2021/17/example @@ -0,0 +1 @@ +target area: x=20..30, y=-10..-5 diff --git a/2021/17/first.go b/2021/17/first.go new file mode 100644 index 0000000..529bbcf --- /dev/null +++ b/2021/17/first.go @@ -0,0 +1,69 @@ +package main + +import ( + "bufio" + "fmt" + "os" + "strconv" + "strings" +) + +type vector struct { + x, y int +} + +func main() { + scanner := bufio.NewScanner(os.Stdin) + scanner.Scan() + line := scanner.Text() + elts := strings.Split(line, "..") + eltsx1 := strings.Split(elts[0], "=") + x1, _ := strconv.Atoi(eltsx1[1]) + eltsx2 := strings.Split(elts[1], ",") + x2, _ := strconv.Atoi(eltsx2[0]) + eltsy1 := strings.Split(elts[1], "=") + y1, _ := strconv.Atoi(eltsy1[1]) + y2, _ := strconv.Atoi(elts[2]) + + // We need to calculate which steps and horizontal speeds can hit the target box + x := 0 + vx := 0 // its a vx max more than a vx + i := 1 + for { + if x+vx+1 <= x2 { + vx += 1 + x += vx + i++ + } else { + break + } + } + + hmax := 0 + for nextVy := 0; nextVy < 10000; nextVy++ { + y := 0 + vy := nextVy + tmpHmax := 0 + j := 0 + for { + if y+vy >= y1 { + y += vy + vy-- + j++ + } else { + break + } + if tmpHmax < y { + tmpHmax = y + } + } + if y >= y1 && y <= y2 && hmax < tmpHmax { + hmax = tmpHmax + fmt.Printf("h=%d, y=%d, vy=%d, j=%d\n", hmax, y, nextVy, j) + } + } + + fmt.Printf("x1=%d, x2=%d, y1=%d, y2=%d\n", x1, x2, y1, y2) + fmt.Printf("i=%d, x=%d, vx=%d\n", i, x, vx) + fmt.Println(hmax) +} diff --git a/2021/17/input b/2021/17/input new file mode 100644 index 0000000..1242878 --- /dev/null +++ b/2021/17/input @@ -0,0 +1 @@ +target area: x=265..287, y=-103..-58 diff --git a/2021/17/second.go b/2021/17/second.go new file mode 100644 index 0000000..49eb9cb --- /dev/null +++ b/2021/17/second.go @@ -0,0 +1,54 @@ +package main + +import ( + "bufio" + "fmt" + "os" + "strconv" + "strings" +) + +type vector struct { + x, y int +} + +func main() { + scanner := bufio.NewScanner(os.Stdin) + scanner.Scan() + line := scanner.Text() + elts := strings.Split(line, "..") + eltsx1 := strings.Split(elts[0], "=") + x1, _ := strconv.Atoi(eltsx1[1]) + eltsx2 := strings.Split(elts[1], ",") + x2, _ := strconv.Atoi(eltsx2[0]) + eltsy1 := strings.Split(elts[1], "=") + y1, _ := strconv.Atoi(eltsy1[1]) + y2, _ := strconv.Atoi(elts[2]) + + possible := 0 + for nextVx := 0; nextVx <= x2; nextVx++ { + out: + for nextVy := y1; nextVy < 1000; nextVy++ { + vx := nextVx + vy := nextVy + x := 0 + y := 0 + for { + if x >= x1 && x <= x2 && y >= y1 && y <= y2 { + possible++ + continue out + } + if x > x2 || y < y1 { + break + } + if vx >= 0 { + x += vx + vx-- + } + y += vy + vy-- + } + } + } + fmt.Println(possible) +} |