solve day 12 part 2

This commit is contained in:
Nico 2020-12-12 22:46:59 +00:00
parent 54a6a60141
commit 358d1ad23d
1 changed files with 56 additions and 14 deletions

View File

@ -7,10 +7,16 @@ import (
"fmt" "fmt"
) )
type Instruction struct{
Op rune
Arg int
}
type Ship struct{ type Ship struct{
NS int // North-South position NS int // North-South position
EW int // East-West position EW int // East-West position
Facing int // N: 0, E: 1, S: 2, W: 3 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 // abs is a simple local abs for integers to avoid importing math
@ -26,43 +32,79 @@ func main() {
ship := Ship{Facing: 1} ship := Ship{Facing: 1}
d, _ := ioutil.ReadFile("input") d, _ := ioutil.ReadFile("input")
lines := strings.Split(string(d),"\n") // I feel like I write this for EVERY AoC lines := strings.Split(string(d),"\n") // I feel like I write this for EVERY AoC
var prog []Instruction
for _, l := range lines { for _, l := range lines {
if l != "" { if l != "" {
command := l[0] // Not unicode-safe! But input is known to contain only ASCII chars command := l[0] // Not unicode-safe! But input is known to contain only ASCII chars
num, _ := strconv.Atoi(l[1:]) 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': case 'F':
switch ship.Facing { switch ship.Facing {
case 0: case 0:
ship.NS += num ship.NS += l.Arg
case 2: case 2:
ship.NS -= num ship.NS -= l.Arg
case 1: case 1:
ship.EW += num ship.EW += l.Arg
case 3: case 3:
ship.EW -= num ship.EW -= l.Arg
} }
case 'N': case 'N':
ship.NS += num ship.NS += l.Arg
case 'S': case 'S':
ship.NS -= num ship.NS -= l.Arg
case 'E': case 'E':
ship.EW += num ship.EW += l.Arg
case 'W': case 'W':
ship.EW -= num ship.EW -= l.Arg
case 'R': case 'R':
h := num/90 h := l.Arg/90
f := ship.Facing + h f := ship.Facing + h
ship.Facing = ((f%4)+4)%4 ship.Facing = ((f%4)+4)%4
case 'L': case 'L':
h := num/90 h := l.Arg/90
f := ship.Facing - h f := ship.Facing - h
ship.Facing = ((f%4)+4)%4 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 2:", abs(ship.EW)+abs(ship.NS))
fmt.Println("Part 1:", abs(ship.EW)+abs(ship.NS))
} }