Day 12: Part 1 and 2
Signed-off-by: James Griffin <james@unsupervised.ca>
This commit is contained in:
188
twelve/day_twelve.go
Normal file
188
twelve/day_twelve.go
Normal file
@@ -0,0 +1,188 @@
|
||||
package twelve
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"math"
|
||||
"os"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type instruction struct {
|
||||
action string
|
||||
value int
|
||||
}
|
||||
|
||||
type waypoint struct {
|
||||
x, y int
|
||||
}
|
||||
|
||||
type boat struct {
|
||||
orientation int
|
||||
orders []instruction
|
||||
x, y int
|
||||
waypoint waypoint
|
||||
}
|
||||
|
||||
func (b *boat) load(filename string) error {
|
||||
file, err := os.Open(filename)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
scanner := bufio.NewScanner(file)
|
||||
for scanner.Scan() {
|
||||
amount, err := strconv.Atoi(scanner.Text()[1:])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
b.orders = append(b.orders, instruction{
|
||||
action: scanner.Text()[:1],
|
||||
value: amount,
|
||||
})
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func loadBoat(filename string) (*boat, error) {
|
||||
b := &boat{
|
||||
waypoint: waypoint{
|
||||
x: 10,
|
||||
y: 1,
|
||||
},
|
||||
}
|
||||
|
||||
err := b.load(filename)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return b, nil
|
||||
}
|
||||
|
||||
func (b *boat) advance(distance int) {
|
||||
if b.orientation > 315 || b.orientation < 45 {
|
||||
b.x += distance
|
||||
return
|
||||
}
|
||||
|
||||
if b.orientation > 45 && b.orientation < 135 {
|
||||
b.y += distance
|
||||
return
|
||||
}
|
||||
|
||||
if b.orientation > 135 && b.orientation < 225 {
|
||||
b.x -= distance
|
||||
}
|
||||
|
||||
if b.orientation > 225 && b.orientation < 315 {
|
||||
b.y -= distance
|
||||
}
|
||||
}
|
||||
|
||||
func (b *boat) navigate() int {
|
||||
for _, o := range b.orders {
|
||||
switch o.action {
|
||||
case "F":
|
||||
b.advance(o.value)
|
||||
case "N":
|
||||
b.y += o.value
|
||||
case "S":
|
||||
b.y -= o.value
|
||||
case "E":
|
||||
b.x += o.value
|
||||
case "W":
|
||||
b.x -= o.value
|
||||
case "L":
|
||||
b.orientation += o.value
|
||||
case "R":
|
||||
b.orientation -= o.value
|
||||
}
|
||||
|
||||
for b.orientation >= 360 {
|
||||
b.orientation -= 360
|
||||
}
|
||||
|
||||
for b.orientation < 0 {
|
||||
b.orientation += 360
|
||||
}
|
||||
}
|
||||
|
||||
return int(math.Abs(float64(b.x))) + int(math.Abs(float64(b.y)))
|
||||
}
|
||||
|
||||
func (b *boat) rotate(degrees int) {
|
||||
newX, newY := b.waypoint.x, b.waypoint.y
|
||||
|
||||
if degrees == 90 || degrees == -270 {
|
||||
newX = b.waypoint.y * -1
|
||||
newY = b.waypoint.x
|
||||
}
|
||||
|
||||
if degrees == 180 || degrees == -180 {
|
||||
newX = b.waypoint.x * -1
|
||||
newY = b.waypoint.y * -1
|
||||
}
|
||||
|
||||
if degrees == 270 || degrees == -90 {
|
||||
newX = b.waypoint.y
|
||||
newY = b.waypoint.x * -1
|
||||
}
|
||||
|
||||
b.waypoint.x = newX
|
||||
b.waypoint.y = newY
|
||||
}
|
||||
|
||||
func (b *boat) waypointNavigate() int {
|
||||
for _, o := range b.orders {
|
||||
switch o.action {
|
||||
case "F":
|
||||
b.x += o.value * b.waypoint.x
|
||||
b.y += o.value * b.waypoint.y
|
||||
case "N":
|
||||
b.waypoint.y += o.value
|
||||
case "S":
|
||||
b.waypoint.y -= o.value
|
||||
case "E":
|
||||
b.waypoint.x += o.value
|
||||
case "W":
|
||||
b.waypoint.x -= o.value
|
||||
case "L":
|
||||
b.rotate(o.value)
|
||||
case "R":
|
||||
b.rotate(o.value * -1)
|
||||
}
|
||||
|
||||
for b.orientation >= 360 {
|
||||
b.orientation -= 360
|
||||
}
|
||||
|
||||
for b.orientation < 0 {
|
||||
b.orientation += 360
|
||||
}
|
||||
}
|
||||
return int(math.Abs(float64(b.x))) + int(math.Abs(float64(b.y)))
|
||||
}
|
||||
|
||||
// PartOne What is the Manhatten distance that the boat travelled?
|
||||
func PartOne() string {
|
||||
b, err := loadBoat("twelve/input.txt")
|
||||
if err != nil {
|
||||
return err.Error()
|
||||
}
|
||||
|
||||
return fmt.Sprintf("The boat travelled a manhattan distance of %d", b.navigate())
|
||||
}
|
||||
|
||||
// PartTwo What is the Manhatten distance that the boat travelled using a waypoint?
|
||||
func PartTwo() string {
|
||||
b, err := loadBoat("twelve/input.txt")
|
||||
if err != nil {
|
||||
return err.Error()
|
||||
}
|
||||
|
||||
return fmt.Sprintf("The boat travelled a manhattan distance of %d using a waypoint", b.waypointNavigate())
|
||||
}
|
36
twelve/day_twelve_test.go
Normal file
36
twelve/day_twelve_test.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package twelve
|
||||
|
||||
import "testing"
|
||||
|
||||
func Test_load_boat(t *testing.T) {
|
||||
b, err := loadBoat("sample.txt")
|
||||
if err != nil {
|
||||
t.Logf(err.Error())
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
if len(b.orders) != 5 {
|
||||
t.Logf("Expected 5 orders, got %d", len(b.orders))
|
||||
t.FailNow()
|
||||
}
|
||||
}
|
||||
|
||||
func Test_boat_navigate(t *testing.T) {
|
||||
b, _ := loadBoat("sample.txt")
|
||||
|
||||
result := b.navigate()
|
||||
if result != 25 {
|
||||
t.Logf("Expected a navigation result of 25, got %d", result)
|
||||
t.FailNow()
|
||||
}
|
||||
}
|
||||
|
||||
func Test_waypoint_navigate(t *testing.T) {
|
||||
b, _ := loadBoat("sample.txt")
|
||||
|
||||
result := b.waypointNavigate()
|
||||
if result != 286 {
|
||||
t.Logf("Expected a navigation result of 286, got %d", result)
|
||||
t.FailNow()
|
||||
}
|
||||
}
|
791
twelve/input.txt
Normal file
791
twelve/input.txt
Normal file
@@ -0,0 +1,791 @@
|
||||
F40
|
||||
N1
|
||||
W1
|
||||
F95
|
||||
W2
|
||||
N5
|
||||
R90
|
||||
N3
|
||||
E3
|
||||
F21
|
||||
E3
|
||||
F44
|
||||
W3
|
||||
R90
|
||||
N2
|
||||
L180
|
||||
E5
|
||||
F99
|
||||
W1
|
||||
F11
|
||||
R90
|
||||
N4
|
||||
F45
|
||||
S5
|
||||
L180
|
||||
W1
|
||||
R180
|
||||
E5
|
||||
R90
|
||||
F5
|
||||
S4
|
||||
E3
|
||||
S4
|
||||
L180
|
||||
W5
|
||||
F26
|
||||
F1
|
||||
S3
|
||||
L180
|
||||
F79
|
||||
R90
|
||||
S5
|
||||
R90
|
||||
E5
|
||||
L180
|
||||
W4
|
||||
F12
|
||||
N5
|
||||
E4
|
||||
F31
|
||||
S5
|
||||
W2
|
||||
F93
|
||||
W2
|
||||
F8
|
||||
S2
|
||||
E5
|
||||
F100
|
||||
L90
|
||||
F10
|
||||
R90
|
||||
N1
|
||||
F15
|
||||
S3
|
||||
E4
|
||||
L90
|
||||
L180
|
||||
S3
|
||||
E5
|
||||
R90
|
||||
F13
|
||||
N4
|
||||
F15
|
||||
L90
|
||||
W4
|
||||
N4
|
||||
W1
|
||||
S5
|
||||
F44
|
||||
W4
|
||||
F68
|
||||
N2
|
||||
W4
|
||||
F58
|
||||
L90
|
||||
E5
|
||||
F81
|
||||
S5
|
||||
L90
|
||||
S1
|
||||
F95
|
||||
R90
|
||||
E3
|
||||
R180
|
||||
F81
|
||||
R90
|
||||
S3
|
||||
E3
|
||||
L180
|
||||
W4
|
||||
W3
|
||||
N1
|
||||
W5
|
||||
R270
|
||||
W3
|
||||
N4
|
||||
W3
|
||||
N4
|
||||
F47
|
||||
E3
|
||||
R90
|
||||
F86
|
||||
S4
|
||||
N1
|
||||
R180
|
||||
E2
|
||||
N1
|
||||
S5
|
||||
R90
|
||||
N4
|
||||
L90
|
||||
E3
|
||||
R180
|
||||
N1
|
||||
W1
|
||||
N3
|
||||
E5
|
||||
N2
|
||||
F9
|
||||
N4
|
||||
R90
|
||||
F36
|
||||
E2
|
||||
S1
|
||||
R90
|
||||
N2
|
||||
F76
|
||||
F88
|
||||
E5
|
||||
F78
|
||||
W4
|
||||
F53
|
||||
N1
|
||||
W1
|
||||
R90
|
||||
R180
|
||||
N4
|
||||
W5
|
||||
S1
|
||||
N2
|
||||
E3
|
||||
F83
|
||||
L90
|
||||
E3
|
||||
L90
|
||||
W2
|
||||
L90
|
||||
N1
|
||||
W2
|
||||
F21
|
||||
R90
|
||||
F58
|
||||
S3
|
||||
F100
|
||||
S5
|
||||
F78
|
||||
S5
|
||||
R90
|
||||
E5
|
||||
R90
|
||||
W5
|
||||
W1
|
||||
L90
|
||||
F23
|
||||
L90
|
||||
F56
|
||||
W3
|
||||
F8
|
||||
W2
|
||||
N5
|
||||
F39
|
||||
S5
|
||||
F84
|
||||
N4
|
||||
R90
|
||||
N4
|
||||
F18
|
||||
S4
|
||||
F50
|
||||
S1
|
||||
E3
|
||||
S5
|
||||
L90
|
||||
N2
|
||||
W2
|
||||
N1
|
||||
F86
|
||||
R90
|
||||
S1
|
||||
R90
|
||||
L90
|
||||
W2
|
||||
F100
|
||||
E4
|
||||
L180
|
||||
F100
|
||||
E1
|
||||
S5
|
||||
W2
|
||||
L90
|
||||
N4
|
||||
E4
|
||||
R90
|
||||
F94
|
||||
W5
|
||||
S5
|
||||
L180
|
||||
W5
|
||||
S5
|
||||
L90
|
||||
S3
|
||||
R180
|
||||
E2
|
||||
F22
|
||||
R90
|
||||
E4
|
||||
F65
|
||||
N1
|
||||
E5
|
||||
F82
|
||||
W3
|
||||
F100
|
||||
E1
|
||||
F87
|
||||
L90
|
||||
S4
|
||||
W1
|
||||
F10
|
||||
W3
|
||||
S2
|
||||
F9
|
||||
E2
|
||||
F49
|
||||
F35
|
||||
E1
|
||||
R90
|
||||
F6
|
||||
W4
|
||||
F60
|
||||
R90
|
||||
S1
|
||||
F45
|
||||
W4
|
||||
F44
|
||||
E3
|
||||
L90
|
||||
W2
|
||||
N3
|
||||
L90
|
||||
N2
|
||||
W1
|
||||
N2
|
||||
W2
|
||||
S4
|
||||
E4
|
||||
W5
|
||||
R90
|
||||
E4
|
||||
L90
|
||||
F16
|
||||
W5
|
||||
S3
|
||||
N5
|
||||
L90
|
||||
F83
|
||||
W4
|
||||
L90
|
||||
E2
|
||||
F25
|
||||
W3
|
||||
R270
|
||||
W3
|
||||
L90
|
||||
N5
|
||||
F36
|
||||
S4
|
||||
R90
|
||||
F15
|
||||
R90
|
||||
S2
|
||||
L90
|
||||
E5
|
||||
F25
|
||||
S3
|
||||
F2
|
||||
N4
|
||||
W1
|
||||
N2
|
||||
F6
|
||||
S3
|
||||
R90
|
||||
L90
|
||||
W2
|
||||
N2
|
||||
E5
|
||||
S3
|
||||
L90
|
||||
F31
|
||||
W4
|
||||
L90
|
||||
N4
|
||||
F30
|
||||
S3
|
||||
R90
|
||||
S3
|
||||
L180
|
||||
W4
|
||||
N4
|
||||
F72
|
||||
W3
|
||||
S1
|
||||
R90
|
||||
F60
|
||||
E4
|
||||
R180
|
||||
W4
|
||||
S1
|
||||
W4
|
||||
R90
|
||||
F10
|
||||
E3
|
||||
F58
|
||||
E5
|
||||
N2
|
||||
W5
|
||||
L180
|
||||
N1
|
||||
E1
|
||||
R90
|
||||
W5
|
||||
N1
|
||||
E3
|
||||
L90
|
||||
N2
|
||||
R90
|
||||
E2
|
||||
R180
|
||||
S1
|
||||
R90
|
||||
F91
|
||||
L90
|
||||
W4
|
||||
N3
|
||||
L270
|
||||
F52
|
||||
W2
|
||||
R90
|
||||
F92
|
||||
N4
|
||||
E5
|
||||
F46
|
||||
N2
|
||||
F36
|
||||
W3
|
||||
L90
|
||||
N5
|
||||
F60
|
||||
N1
|
||||
S3
|
||||
F94
|
||||
L180
|
||||
S1
|
||||
R270
|
||||
R180
|
||||
F26
|
||||
S1
|
||||
F23
|
||||
E5
|
||||
R90
|
||||
F27
|
||||
S3
|
||||
L90
|
||||
F8
|
||||
E5
|
||||
F5
|
||||
S1
|
||||
R90
|
||||
F99
|
||||
W3
|
||||
F47
|
||||
W3
|
||||
S3
|
||||
W1
|
||||
L180
|
||||
W3
|
||||
R180
|
||||
F41
|
||||
L180
|
||||
E2
|
||||
L180
|
||||
N5
|
||||
R90
|
||||
F17
|
||||
S2
|
||||
E2
|
||||
F2
|
||||
R90
|
||||
N2
|
||||
F53
|
||||
S4
|
||||
L90
|
||||
F87
|
||||
R180
|
||||
E1
|
||||
S4
|
||||
F43
|
||||
R90
|
||||
F45
|
||||
W4
|
||||
F7
|
||||
W5
|
||||
L90
|
||||
W4
|
||||
L90
|
||||
E3
|
||||
L90
|
||||
E3
|
||||
R90
|
||||
F14
|
||||
N1
|
||||
F23
|
||||
E4
|
||||
N1
|
||||
N1
|
||||
R90
|
||||
F98
|
||||
L180
|
||||
E5
|
||||
F92
|
||||
R180
|
||||
E4
|
||||
S2
|
||||
R270
|
||||
W3
|
||||
L180
|
||||
E1
|
||||
S5
|
||||
N3
|
||||
E5
|
||||
R90
|
||||
E3
|
||||
L90
|
||||
F21
|
||||
F84
|
||||
L90
|
||||
S5
|
||||
R90
|
||||
F68
|
||||
L180
|
||||
E3
|
||||
L90
|
||||
W4
|
||||
F18
|
||||
S4
|
||||
W5
|
||||
L90
|
||||
R180
|
||||
W1
|
||||
L180
|
||||
F88
|
||||
E3
|
||||
N3
|
||||
W3
|
||||
S3
|
||||
L90
|
||||
F69
|
||||
R180
|
||||
W4
|
||||
F98
|
||||
S3
|
||||
L90
|
||||
E2
|
||||
N2
|
||||
F26
|
||||
E2
|
||||
E1
|
||||
N2
|
||||
W5
|
||||
R90
|
||||
W1
|
||||
F13
|
||||
W4
|
||||
R180
|
||||
N2
|
||||
F25
|
||||
W4
|
||||
F89
|
||||
W4
|
||||
F76
|
||||
S5
|
||||
F73
|
||||
E1
|
||||
N3
|
||||
L90
|
||||
E4
|
||||
F97
|
||||
L180
|
||||
N2
|
||||
R180
|
||||
E1
|
||||
F88
|
||||
E3
|
||||
N5
|
||||
W2
|
||||
F62
|
||||
S3
|
||||
E5
|
||||
R180
|
||||
N1
|
||||
N3
|
||||
N4
|
||||
F3
|
||||
W2
|
||||
R180
|
||||
F28
|
||||
L90
|
||||
S4
|
||||
E1
|
||||
L90
|
||||
E4
|
||||
F63
|
||||
R90
|
||||
N2
|
||||
R90
|
||||
F22
|
||||
N3
|
||||
L90
|
||||
W4
|
||||
S1
|
||||
F67
|
||||
W5
|
||||
N4
|
||||
F44
|
||||
S4
|
||||
F64
|
||||
L180
|
||||
W3
|
||||
N2
|
||||
W1
|
||||
F63
|
||||
N3
|
||||
R90
|
||||
S5
|
||||
R90
|
||||
F20
|
||||
L180
|
||||
L270
|
||||
S1
|
||||
L90
|
||||
F66
|
||||
W5
|
||||
R90
|
||||
N1
|
||||
L180
|
||||
W4
|
||||
F94
|
||||
S3
|
||||
R180
|
||||
F18
|
||||
L90
|
||||
F29
|
||||
S3
|
||||
L90
|
||||
S4
|
||||
F74
|
||||
L90
|
||||
F85
|
||||
F35
|
||||
R90
|
||||
S4
|
||||
F68
|
||||
R90
|
||||
F44
|
||||
S2
|
||||
W4
|
||||
S2
|
||||
F27
|
||||
R90
|
||||
E5
|
||||
F30
|
||||
E1
|
||||
L90
|
||||
W4
|
||||
F39
|
||||
N3
|
||||
L90
|
||||
E1
|
||||
S4
|
||||
F87
|
||||
W2
|
||||
L90
|
||||
N3
|
||||
W1
|
||||
F51
|
||||
W1
|
||||
L180
|
||||
F24
|
||||
N2
|
||||
E1
|
||||
N2
|
||||
F4
|
||||
R90
|
||||
E3
|
||||
S1
|
||||
F69
|
||||
R90
|
||||
E4
|
||||
F31
|
||||
L90
|
||||
S3
|
||||
E3
|
||||
E5
|
||||
L90
|
||||
F75
|
||||
E4
|
||||
L90
|
||||
F14
|
||||
L90
|
||||
N1
|
||||
R90
|
||||
F36
|
||||
S4
|
||||
F49
|
||||
L90
|
||||
N5
|
||||
W3
|
||||
R90
|
||||
F35
|
||||
L180
|
||||
R180
|
||||
F26
|
||||
W3
|
||||
F16
|
||||
R90
|
||||
F90
|
||||
E3
|
||||
N3
|
||||
F87
|
||||
N5
|
||||
L180
|
||||
F4
|
||||
R90
|
||||
N1
|
||||
E4
|
||||
N5
|
||||
F93
|
||||
W1
|
||||
N4
|
||||
L90
|
||||
F35
|
||||
L90
|
||||
W1
|
||||
E3
|
||||
N5
|
||||
W5
|
||||
F5
|
||||
S1
|
||||
W1
|
||||
N1
|
||||
F61
|
||||
S1
|
||||
W2
|
||||
N1
|
||||
R90
|
||||
F26
|
||||
R90
|
||||
L90
|
||||
W4
|
||||
F12
|
||||
R90
|
||||
W1
|
||||
R90
|
||||
F18
|
||||
E1
|
||||
F14
|
||||
N3
|
||||
W3
|
||||
S2
|
||||
F25
|
||||
E5
|
||||
F89
|
||||
W5
|
||||
L90
|
||||
S4
|
||||
F38
|
||||
L180
|
||||
F98
|
||||
W3
|
||||
S1
|
||||
F77
|
||||
R270
|
||||
E2
|
||||
F95
|
||||
W1
|
||||
F56
|
||||
N4
|
||||
R180
|
||||
E3
|
||||
L270
|
||||
E1
|
||||
F6
|
||||
S3
|
||||
L180
|
||||
E5
|
||||
R180
|
||||
E1
|
||||
N2
|
||||
L180
|
||||
E4
|
||||
S3
|
||||
E2
|
||||
L180
|
||||
F72
|
||||
N4
|
||||
R90
|
||||
L90
|
||||
W4
|
||||
F82
|
||||
S3
|
||||
R270
|
||||
F32
|
||||
F39
|
||||
L90
|
||||
N5
|
||||
W1
|
||||
L90
|
||||
N3
|
||||
F95
|
||||
L180
|
||||
S5
|
||||
L90
|
||||
F46
|
||||
E1
|
||||
L90
|
||||
W2
|
||||
S5
|
||||
L90
|
||||
S5
|
||||
F77
|
||||
L90
|
||||
N4
|
||||
E3
|
||||
N1
|
||||
F39
|
||||
R90
|
||||
R90
|
||||
F40
|
||||
L90
|
||||
N4
|
||||
W1
|
||||
F7
|
||||
E4
|
||||
S5
|
||||
E5
|
||||
N1
|
||||
F96
|
||||
E4
|
||||
F10
|
||||
F8
|
||||
S5
|
||||
E5
|
||||
F26
|
||||
S4
|
||||
R90
|
||||
S2
|
||||
F61
|
||||
W4
|
||||
S4
|
||||
R90
|
||||
E2
|
||||
F39
|
||||
S5
|
||||
R90
|
||||
S4
|
||||
F83
|
||||
S5
|
||||
F18
|
||||
S3
|
||||
E5
|
||||
R180
|
||||
F7
|
5
twelve/sample.txt
Normal file
5
twelve/sample.txt
Normal file
@@ -0,0 +1,5 @@
|
||||
F10
|
||||
N3
|
||||
F7
|
||||
R90
|
||||
F11
|
Reference in New Issue
Block a user