-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
141 lines (126 loc) · 3.03 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package main
import (
"bufio"
"fmt"
"io"
aoc "github.com/teivah/advent-of-code"
)
func fs1(input io.Reader) int {
scanner := bufio.NewScanner(input)
var actions []Action
for scanner.Scan() {
line := scanner.Text()
actions = append(actions, toAction(line))
}
ship := newShip(actions)
ship.seal()
return ship.ship.ManhattanZero()
}
type Ship struct {
direction aoc.Direction
ship aoc.Position
waypoint aoc.Position
actions []Action
}
func newShip(actions []Action) *Ship {
return &Ship{
direction: aoc.Right,
actions: actions,
waypoint: aoc.Position{-1, 10},
}
}
func (s *Ship) seal() {
for _, action := range s.actions {
switch action.actionType {
case north:
s.ship = s.ship.Move(aoc.Up, action.unit)
case south:
s.ship = s.ship.Move(aoc.Down, action.unit)
case east:
s.ship = s.ship.Move(aoc.Right, action.unit)
case west:
s.ship = s.ship.Move(aoc.Left, action.unit)
case turnLeft:
for i := 0; i < action.unit/90; i++ {
s.direction = s.direction.Turn(aoc.Left)
}
case turnRight:
for i := 0; i < action.unit/90; i++ {
s.direction = s.direction.Turn(aoc.Right)
}
case forward:
s.ship = s.ship.Move(s.direction, action.unit)
}
}
}
type Action struct {
actionType ActionType
unit int
}
func (a Action) String() string {
return fmt.Sprintf("%c%d", a.actionType, a.unit)
}
func toAction(s string) Action {
return Action{
actionType: ActionType(s[0]),
unit: aoc.StringToInt(s[1:]),
}
}
type ActionType rune
const (
north ActionType = 'N'
south ActionType = 'S'
east ActionType = 'E'
west ActionType = 'W'
turnLeft ActionType = 'L'
turnRight ActionType = 'R'
forward ActionType = 'F'
)
func fs2(input io.Reader) int {
scanner := bufio.NewScanner(input)
var actions []Action
for scanner.Scan() {
line := scanner.Text()
actions = append(actions, toAction(line))
}
ship := newShip(actions)
ship.seal2()
return ship.ship.ManhattanZero()
}
func (s *Ship) seal2() {
for _, action := range s.actions {
switch action.actionType {
case north:
s.waypoint = s.waypoint.Move(aoc.Up, action.unit)
case south:
s.waypoint = s.waypoint.Move(aoc.Down, action.unit)
case east:
s.waypoint = s.waypoint.Move(aoc.Right, action.unit)
case west:
s.waypoint = s.waypoint.Move(aoc.Left, action.unit)
case turnLeft:
for i := 0; i < action.unit/90; i++ {
s.waypoint.Col, s.waypoint.Row = s.waypoint.Row, -s.waypoint.Col
}
case turnRight:
for i := 0; i < action.unit/90; i++ {
s.waypoint.Col, s.waypoint.Row = -s.waypoint.Row, s.waypoint.Col
}
case forward:
if s.waypoint.Row != 0 {
if s.waypoint.Row < 0 {
s.ship = s.ship.Move(aoc.Up, action.unit*aoc.Abs(s.waypoint.Row))
} else {
s.ship = s.ship.Move(aoc.Down, action.unit*aoc.Abs(s.waypoint.Row))
}
}
if s.waypoint.Col != 0 {
if s.waypoint.Col < 0 {
s.ship = s.ship.Move(aoc.Left, action.unit*aoc.Abs(s.waypoint.Col))
} else {
s.ship = s.ship.Move(aoc.Right, action.unit*aoc.Abs(s.waypoint.Col))
}
}
}
}
}