From 3abd4fa7833ccc68da916741678bb6241da2c09d Mon Sep 17 00:00:00 2001 From: Dreaded_X Date: Fri, 17 Dec 2021 20:23:52 +0100 Subject: [PATCH] 2021 - Day 17 --- 2021/17/go.mod | 9 +++ 2021/17/go.sum | 2 + 2021/17/main.go | 143 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 154 insertions(+) create mode 100644 2021/17/go.mod create mode 100644 2021/17/go.sum create mode 100644 2021/17/main.go diff --git a/2021/17/go.mod b/2021/17/go.mod new file mode 100644 index 0000000..103619c --- /dev/null +++ b/2021/17/go.mod @@ -0,0 +1,9 @@ +module AoC/2021/17 + +require AoC/2021/common v0.0.0 + +require github.com/joho/godotenv v1.4.0 // indirect + +replace AoC/2021/common v0.0.0 => ../common + +go 1.17 diff --git a/2021/17/go.sum b/2021/17/go.sum new file mode 100644 index 0000000..8c9f290 --- /dev/null +++ b/2021/17/go.sum @@ -0,0 +1,2 @@ +github.com/joho/godotenv v1.4.0 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg= +github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= diff --git a/2021/17/main.go b/2021/17/main.go new file mode 100644 index 0000000..a7d2356 --- /dev/null +++ b/2021/17/main.go @@ -0,0 +1,143 @@ +package main + +import ( + aoc "AoC/2021/common" + "bufio" + "fmt" + "regexp" + "strconv" +) + +type vec struct { + x int + y int +} + +type probe struct { + x vec + u vec +} + +type target struct { + xmin int + xmax int + ymin int + ymax int +} + +func (a *vec) add(b vec) { + a.x += b.x + a.y += b.y +} + +func (v *vec) in(t target) bool { + return v.x >= t.xmin && v.x <= t.xmax && v.y >= t.ymin && v.y <= t.ymax +} + +func (v *vec) past(t target) bool { + return v.x > t.xmax || v.y < t.ymin +} + +func main() { + challenge := aoc.New(2021, 17) + + challenge.Test(`target area: x=20..30, y=-10..-5`, []int{45, 112}) + + challenge.Solution(1, func (input *bufio.Scanner) int { + input.Scan() + line := input.Text() + + r := regexp.MustCompile("-?[0-9]+") + + var coords [4]int + for i, c := range r.FindAllString(line, -1) { + coords[i], _ = strconv.Atoi(c) + } + t := target{coords[0], coords[1], coords[2], coords[3]} + + max := 0 + velocity := vec{0, 0} + for vx := -1000; vx < 1000; vx++ { + for vy := -1000; vy < 1000; vy++ { + p := probe{u: vec{vx, vy}} + height := 0 + for step := 0; true; step++ { + if p.x.in(t) { + if height > max { + max = height + velocity = vec{vx, vy} + } + + break + } + + if p.x.past(t) { + break + } + + if p.x.y > height { + height = p.x.y + } + + p.x.add(p.u) + + if p.u.x > 0 { + p.u.add(vec{-1, 0}) + } else if p.u.x < 0 { + p.u.add(vec{1, 0}) + } + + p.u.add(vec{0, -1}) + } + + } + } + + fmt.Printf("Max height %d is achieved using %v\n", max, velocity) + + return max + }) + + challenge.Solution(2, func (input *bufio.Scanner) int { + input.Scan() + line := input.Text() + + r := regexp.MustCompile("-?[0-9]+") + + var coords [4]int + for i, c := range r.FindAllString(line, -1) { + coords[i], _ = strconv.Atoi(c) + } + t := target{coords[0], coords[1], coords[2], coords[3]} + + counter := 0 + for vx := -1000; vx < 1000; vx++ { + for vy := -1000; vy < 1000; vy++ { + p := probe{u: vec{vx, vy}} + for step := 0; true; step++ { + if p.x.in(t) { + counter++ + break + } + + if p.x.past(t) { + break + } + + p.x.add(p.u) + + if p.u.x > 0 { + p.u.add(vec{-1, 0}) + } else if p.u.x < 0 { + p.u.add(vec{1, 0}) + } + + p.u.add(vec{0, -1}) + } + + } + } + + return counter + }) +}