mirror of
https://github.com/Threnklyn/advent-of-code-go.git
synced 2026-05-18 19:13:27 +02:00
2020-day12: 243 on part 1, then confused the heck out of myself for part 2 w/ x/y vs row/col vs x,y coordinates and rotating around the origin..
This commit is contained in:
+96
-12
@@ -5,6 +5,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/alexchao26/advent-of-code-go/mathutil"
|
||||||
"github.com/alexchao26/advent-of-code-go/util"
|
"github.com/alexchao26/advent-of-code-go/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -16,35 +17,118 @@ func main() {
|
|||||||
|
|
||||||
if part == 1 {
|
if part == 1 {
|
||||||
ans := part1(util.ReadFile("./input.txt"))
|
ans := part1(util.ReadFile("./input.txt"))
|
||||||
util.CopyToClipboard(fmt.Sprintf("%v", ans))
|
|
||||||
fmt.Println("Output:", ans)
|
fmt.Println("Output:", ans)
|
||||||
} else {
|
} else {
|
||||||
ans := part2(util.ReadFile("./input.txt"))
|
ans := part2(util.ReadFile("./input.txt"))
|
||||||
util.CopyToClipboard(fmt.Sprintf("%v", ans))
|
|
||||||
fmt.Println("Output:", ans)
|
fmt.Println("Output:", ans)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func part1(input string) int {
|
// globals for part 1
|
||||||
parsed := parseInput(input)
|
var directions = []string{"N", "E", "S", "W"}
|
||||||
_ = parsed
|
var directionsToDiff = map[string][2]int{
|
||||||
|
// !! X and Y like a coordinate system! Not typical 2D matrices in algos
|
||||||
|
"N": [2]int{0, 1},
|
||||||
|
"E": [2]int{1, 0},
|
||||||
|
"S": [2]int{0, -1},
|
||||||
|
"W": [2]int{-1, 0},
|
||||||
|
}
|
||||||
|
|
||||||
return 0
|
func part1(input string) int {
|
||||||
|
instructions := parseInput(input)
|
||||||
|
|
||||||
|
// X and Y like a coordinate system
|
||||||
|
var shipX, shipY int
|
||||||
|
dirIndex := 1 // index in directions slice
|
||||||
|
for _, inst := range instructions {
|
||||||
|
switch inst.action {
|
||||||
|
case "N":
|
||||||
|
shipY += inst.value
|
||||||
|
case "S":
|
||||||
|
shipY -= inst.value
|
||||||
|
case "E":
|
||||||
|
shipX += inst.value
|
||||||
|
case "W":
|
||||||
|
shipX -= inst.value
|
||||||
|
case "L":
|
||||||
|
// rotate ship left, this is equivalent
|
||||||
|
// -1 + 4 to keeping dirIndex positive for the modding
|
||||||
|
dirIndex += (-1 + 4) * inst.value / 90
|
||||||
|
dirIndex %= 4
|
||||||
|
case "R":
|
||||||
|
dirIndex += inst.value / 90
|
||||||
|
dirIndex %= 4
|
||||||
|
case "F":
|
||||||
|
d := directionsToDiff[directions[dirIndex]]
|
||||||
|
shipX += d[0] * inst.value
|
||||||
|
shipY += d[1] * inst.value
|
||||||
|
default:
|
||||||
|
panic("unexpected action")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return mathutil.ManhattanDistance(0, 0, shipX, shipY)
|
||||||
}
|
}
|
||||||
|
|
||||||
func part2(input string) int {
|
func part2(input string) int {
|
||||||
parsed := parseInput(input)
|
instructions := parseInput(input)
|
||||||
_ = parsed
|
|
||||||
|
|
||||||
return 0
|
// X and Y like a coordinate system
|
||||||
|
waypointX := 10
|
||||||
|
waypointY := 1
|
||||||
|
var shipX, shipY int
|
||||||
|
|
||||||
|
for _, inst := range instructions {
|
||||||
|
switch inst.action {
|
||||||
|
case "N":
|
||||||
|
waypointY += inst.value
|
||||||
|
case "S":
|
||||||
|
waypointY -= inst.value
|
||||||
|
case "E":
|
||||||
|
waypointX += inst.value
|
||||||
|
case "W":
|
||||||
|
waypointX -= inst.value
|
||||||
|
case "L":
|
||||||
|
// rotate waypoint left around ship (origin)
|
||||||
|
turns := inst.value / 90
|
||||||
|
for turns > 0 {
|
||||||
|
// this simple bit is all it needs to rotate around the origin
|
||||||
|
// I had a 20 line if/else block...
|
||||||
|
waypointX, waypointY = -waypointY, waypointX
|
||||||
|
turns--
|
||||||
|
}
|
||||||
|
case "R":
|
||||||
|
turns := inst.value / 90
|
||||||
|
for turns > 0 {
|
||||||
|
waypointX, waypointY = waypointY, -waypointX
|
||||||
|
turns--
|
||||||
|
}
|
||||||
|
case "F":
|
||||||
|
shipX += inst.value * waypointX
|
||||||
|
shipY += inst.value * waypointY
|
||||||
|
default:
|
||||||
|
panic("unexpected action")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return mathutil.ManhattanDistance(0, 0, shipX, shipY)
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseInput(input string) []int {
|
type instruction struct {
|
||||||
var ans []int
|
action string
|
||||||
|
value int
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseInput(input string) []instruction {
|
||||||
|
var ans []instruction
|
||||||
|
|
||||||
lines := strings.Split(input, "\n")
|
lines := strings.Split(input, "\n")
|
||||||
for _, l := range lines {
|
for _, l := range lines {
|
||||||
ans = append(ans, mathutil.StrToInt(l))
|
inst := instruction{
|
||||||
|
action: l[:1],
|
||||||
|
value: mathutil.StrToInt(l[1:]),
|
||||||
|
}
|
||||||
|
ans = append(ans, inst)
|
||||||
}
|
}
|
||||||
|
|
||||||
return ans
|
return ans
|
||||||
|
|||||||
+15
-5
@@ -1,14 +1,24 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import "testing"
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/alexchao26/advent-of-code-go/util"
|
||||||
|
)
|
||||||
|
|
||||||
|
var exampleInput = `F10
|
||||||
|
N3
|
||||||
|
F7
|
||||||
|
R90
|
||||||
|
F11`
|
||||||
|
|
||||||
var tests1 = []struct {
|
var tests1 = []struct {
|
||||||
name string
|
name string
|
||||||
want int
|
want int
|
||||||
input string
|
input string
|
||||||
// add extra args if needed
|
|
||||||
}{
|
}{
|
||||||
// {"actual", ACTUAL_ANSWER, util.ReadFile("input.txt")},
|
{"example", 25, exampleInput},
|
||||||
|
{"actual", 820, util.ReadFile("input.txt")},
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPart1(t *testing.T) {
|
func TestPart1(t *testing.T) {
|
||||||
@@ -25,9 +35,9 @@ var tests2 = []struct {
|
|||||||
name string
|
name string
|
||||||
want int
|
want int
|
||||||
input string
|
input string
|
||||||
// add extra args if needed
|
|
||||||
}{
|
}{
|
||||||
// {"actual", ACTUAL_ANSWER, util.ReadFile("input.txt")},
|
{"example", 286, exampleInput},
|
||||||
|
{"actual", 66614, util.ReadFile("input.txt")},
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPart2(t *testing.T) {
|
func TestPart2(t *testing.T) {
|
||||||
|
|||||||
Reference in New Issue
Block a user