solve day 12 part 2
This commit is contained in:
parent
54a6a60141
commit
358d1ad23d
|
@ -7,10 +7,16 @@ import (
|
|||
"fmt"
|
||||
)
|
||||
|
||||
type Instruction struct{
|
||||
Op rune
|
||||
Arg int
|
||||
}
|
||||
type Ship struct{
|
||||
NS int // North-South position
|
||||
EW int // East-West position
|
||||
Facing int // N: 0, E: 1, S: 2, W: 3
|
||||
WayNS int // Waypoint North-South position
|
||||
WayEW int // Waypoint East-West position
|
||||
}
|
||||
|
||||
// abs is a simple local abs for integers to avoid importing math
|
||||
|
@ -26,43 +32,79 @@ func main() {
|
|||
ship := Ship{Facing: 1}
|
||||
d, _ := ioutil.ReadFile("input")
|
||||
lines := strings.Split(string(d),"\n") // I feel like I write this for EVERY AoC
|
||||
var prog []Instruction
|
||||
for _, l := range lines {
|
||||
if l != "" {
|
||||
command := l[0] // Not unicode-safe! But input is known to contain only ASCII chars
|
||||
num, _ := strconv.Atoi(l[1:])
|
||||
switch rune(command) {
|
||||
prog = append(prog, Instruction{Op: rune(command), Arg: num})
|
||||
}
|
||||
}
|
||||
for _, l := range prog {
|
||||
switch l.Op {
|
||||
case 'F':
|
||||
switch ship.Facing {
|
||||
case 0:
|
||||
ship.NS += num
|
||||
ship.NS += l.Arg
|
||||
case 2:
|
||||
ship.NS -= num
|
||||
ship.NS -= l.Arg
|
||||
case 1:
|
||||
ship.EW += num
|
||||
ship.EW += l.Arg
|
||||
case 3:
|
||||
ship.EW -= num
|
||||
ship.EW -= l.Arg
|
||||
}
|
||||
case 'N':
|
||||
ship.NS += num
|
||||
ship.NS += l.Arg
|
||||
case 'S':
|
||||
ship.NS -= num
|
||||
ship.NS -= l.Arg
|
||||
case 'E':
|
||||
ship.EW += num
|
||||
ship.EW += l.Arg
|
||||
case 'W':
|
||||
ship.EW -= num
|
||||
ship.EW -= l.Arg
|
||||
case 'R':
|
||||
h := num/90
|
||||
h := l.Arg/90
|
||||
f := ship.Facing + h
|
||||
ship.Facing = ((f%4)+4)%4
|
||||
case 'L':
|
||||
h := num/90
|
||||
h := l.Arg/90
|
||||
f := ship.Facing - h
|
||||
ship.Facing = ((f%4)+4)%4
|
||||
}
|
||||
fmt.Printf("INSTRUCTION: %s, FACING: %d, N/S POS: %d, E/W POS: %d\n",l, ship.Facing, ship.NS, ship.EW)
|
||||
}
|
||||
fmt.Println("Part 1:", abs(ship.EW)+abs(ship.NS))
|
||||
// Part 2 - we use the waypoint this time
|
||||
ship = Ship{Facing: 1, WayNS: 1, WayEW: 10}
|
||||
for _, l := range prog {
|
||||
switch l.Op {
|
||||
case 'F':
|
||||
NS := l.Arg*ship.WayNS
|
||||
EW := l.Arg*ship.WayEW
|
||||
ship.NS += NS
|
||||
ship.EW += EW
|
||||
case 'N':
|
||||
ship.WayNS += l.Arg
|
||||
case 'S':
|
||||
ship.WayNS -= l.Arg
|
||||
case 'E':
|
||||
ship.WayEW += l.Arg
|
||||
case 'W':
|
||||
ship.WayEW -= l.Arg
|
||||
case 'R':
|
||||
for i := l.Arg; i != 0; i -= 90 {
|
||||
NS := -ship.WayEW
|
||||
EW := ship.WayNS
|
||||
ship.WayEW = EW
|
||||
ship.WayNS = NS
|
||||
}
|
||||
case 'L':
|
||||
for i := l.Arg*3; i != 0; i -= 90 {
|
||||
NS := -ship.WayEW
|
||||
EW := ship.WayNS
|
||||
ship.WayEW = EW
|
||||
ship.WayNS = NS
|
||||
}
|
||||
}
|
||||
}
|
||||
fmt.Println(ship)
|
||||
fmt.Println("Part 1:", abs(ship.EW)+abs(ship.NS))
|
||||
fmt.Println("Part 2:", abs(ship.EW)+abs(ship.NS))
|
||||
|
||||
}
|
Loading…
Reference in New Issue