advent-of-code

Perserverance, or the lack thereof

git clone git://git.shimmy1996.com/advent-of-code.git
commit 328fcd1c0ebf48d91ea2323e9a35017cb15d28e8
parent c80ff36745187674bd373354d3f41b2d3548a866
Author: Shimmy Xu <shimmy.xu@shimmy1996.com>
Date:   Sat, 12 Dec 2020 07:05:21 -0600

Add 2020 day 12

Diffstat:
A2020/day12/input.txt | 773+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A2020/day12/main.go | 121+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 894 insertions(+), 0 deletions(-)
diff --git a/2020/day12/input.txt b/2020/day12/input.txt
@@ -0,0 +1,773 @@
+R180
+S1
+F44
+R90
+E5
+N3
+E5
+S4
+W1
+F68
+E2
+L90
+F40
+W3
+N2
+L180
+S3
+R90
+R90
+W5
+F45
+L90
+F9
+W5
+N1
+E1
+F39
+R90
+F6
+E3
+N1
+E2
+N3
+L180
+N5
+L90
+L90
+S2
+F72
+L90
+E5
+L90
+S2
+F79
+N3
+R90
+F25
+R90
+F16
+W2
+L90
+F81
+W5
+L90
+E5
+S3
+W5
+N1
+L180
+F73
+N1
+F60
+S3
+E1
+F65
+W1
+R90
+F36
+S4
+W1
+R90
+S2
+F33
+S5
+L90
+E3
+F11
+S4
+W2
+L90
+F57
+W4
+N1
+R180
+S2
+F87
+N2
+E2
+S2
+F89
+L90
+W1
+N3
+F63
+R270
+W4
+N1
+R90
+F67
+L90
+F74
+N5
+W5
+S3
+L90
+N1
+L180
+W4
+S4
+E1
+L90
+S1
+E4
+S3
+F95
+S4
+F32
+L90
+F9
+R180
+N4
+W3
+R90
+W4
+F10
+W5
+F21
+L180
+F17
+S4
+L90
+F24
+R90
+F1
+E5
+R180
+F63
+N5
+N4
+E1
+F73
+S2
+S5
+W1
+N1
+R90
+F77
+W4
+N4
+F74
+W5
+F82
+W4
+F8
+E4
+N2
+R90
+R90
+E3
+F44
+F42
+L90
+W5
+R90
+W5
+F45
+W5
+F35
+W1
+L90
+S1
+L90
+N5
+R90
+F4
+R180
+F19
+R180
+F16
+W5
+S1
+R90
+S2
+W3
+F44
+S4
+W4
+F95
+R180
+F1
+R90
+F36
+N4
+F12
+R90
+F26
+F14
+R90
+E5
+N1
+W2
+F88
+N5
+R180
+S2
+E4
+R90
+N4
+E4
+S2
+F9
+N2
+E3
+N5
+F28
+N4
+E3
+N3
+W3
+F93
+N2
+R180
+E2
+F9
+W1
+F28
+R90
+S1
+F82
+W4
+S1
+F59
+S2
+F7
+E2
+R180
+E5
+F19
+S3
+E4
+F53
+L270
+E1
+L90
+W3
+F2
+S3
+F40
+E3
+S1
+F94
+W3
+L90
+F87
+W3
+F37
+S5
+E2
+N2
+R270
+F55
+R90
+S1
+W2
+N1
+L90
+F21
+W5
+N4
+L90
+N3
+F50
+F18
+N2
+F3
+W5
+F68
+N5
+L90
+R270
+F31
+L90
+F90
+R90
+E3
+N3
+L90
+F97
+S1
+W2
+N2
+F10
+E1
+W3
+S4
+F56
+R270
+F70
+S1
+L90
+E1
+F89
+W2
+F94
+L180
+F94
+R90
+N4
+F89
+R180
+W5
+F81
+R90
+N3
+F61
+W3
+W5
+W2
+F90
+F66
+N5
+R90
+E2
+F31
+L90
+E4
+E1
+R180
+W5
+F8
+W3
+R90
+F92
+R90
+E1
+R90
+E2
+F66
+E5
+R90
+S2
+R90
+W5
+R90
+F52
+S5
+E2
+N1
+F57
+W1
+F30
+W5
+F51
+N3
+F82
+L90
+S1
+W4
+R90
+W5
+N5
+E5
+N5
+F41
+N3
+R90
+S3
+E1
+R90
+W2
+N5
+W5
+F45
+L270
+F93
+E4
+R270
+F95
+S3
+W2
+N3
+R90
+W2
+E2
+F56
+R90
+N4
+E3
+R90
+W3
+N4
+F54
+R90
+E5
+F86
+E3
+R90
+F8
+N1
+F79
+S3
+E1
+N2
+F90
+L90
+E2
+R90
+W2
+F95
+E2
+L90
+E1
+F47
+W3
+L90
+F78
+L90
+W3
+R90
+N1
+F34
+W2
+L90
+W4
+R90
+W2
+R180
+E3
+S5
+W2
+F61
+W3
+R90
+E5
+F30
+S3
+F11
+W4
+S2
+F33
+R270
+F94
+L270
+S3
+L90
+F48
+R180
+S4
+F17
+N4
+F64
+L90
+N5
+R90
+S3
+N4
+F53
+S5
+W5
+L180
+E3
+F96
+R90
+F48
+R180
+F84
+E4
+R270
+F48
+F32
+R90
+F79
+S2
+R90
+E5
+S4
+L90
+S5
+F5
+W4
+F30
+R180
+S2
+E3
+N4
+F80
+E1
+F75
+E5
+L90
+S2
+W3
+F87
+L90
+F57
+S5
+F78
+N5
+E2
+E2
+F53
+N5
+F58
+E4
+R90
+N3
+E1
+S4
+W2
+N3
+R180
+W4
+S1
+F17
+R90
+N4
+W4
+S3
+W1
+R90
+S4
+R90
+N1
+W4
+N2
+F17
+R90
+N1
+L90
+S3
+E4
+S3
+R90
+F66
+E2
+N4
+W4
+S1
+L90
+F56
+R180
+S5
+F43
+E5
+F44
+E5
+S2
+E2
+R180
+F64
+N4
+W5
+L180
+E2
+L90
+N4
+E5
+F75
+L90
+E3
+L90
+F79
+R180
+E2
+L90
+F88
+S4
+W3
+R90
+E3
+F43
+E3
+F43
+N1
+W4
+S3
+F55
+N4
+F52
+E3
+L180
+E2
+N2
+F80
+R180
+S5
+F92
+N3
+W2
+R90
+E2
+L90
+W3
+S4
+L90
+N5
+E1
+L180
+F25
+W4
+F65
+E1
+S5
+R90
+N4
+F4
+E4
+F70
+F26
+N2
+N4
+W2
+F3
+R90
+N5
+F84
+W1
+R90
+N4
+W4
+F43
+R270
+S2
+F2
+S4
+L90
+F59
+L90
+F59
+R90
+S5
+F88
+L90
+N5
+E2
+F7
+S2
+W5
+L90
+S4
+R270
+F5
+L90
+E1
+F25
+E1
+S5
+F84
+L180
+F10
+W3
+L180
+S4
+F51
+N1
+W2
+L90
+F23
+W4
+N1
+E2
+F40
+W5
+N3
+F93
+R180
+E5
+S1
+E5
+F11
+S1
+E2
+L90
+E2
+F11
+R270
+W4
+L180
+N3
+R90
+F5
+L90
+R90
+N2
+F50
+R180
+S1
+E2
+S4
+E3
+N4
+W2
+F69
+E1
+N1
+W4
+R90
+F68
+W3
+S2
+F5
+W2
+S2
+S4
+R180
+W1
+R90
+F14
+E5
+S3
+W2
+F84
+E1
+L90
+F99
+S4
+S4
+W1
+F31
+E4
+F77
+S4
+F75
+R90
+W3
+R90
+L90
+E5
+N4
+W3
+L90
+E2
+L90
+W1
+F91
+L90
+E2
+L90
+N1
+E1
+R180
+S1
+L90
+F72
+W2
+R270
+F18
+N5
+F7
+E3
+F83
+W5
+E1
+S3
+E3
+F76
+S5
+L90
+S4
+E2
+S1
+R270
+F52
+R270
+F51
+N2
+F41
+N1
+E5
+S3
+R90
+W4
+F53
diff --git a/2020/day12/main.go b/2020/day12/main.go
@@ -0,0 +1,121 @@
+package main
+
+import (
+	"bufio"
+	"fmt"
+	"os"
+	"strconv"
+)
+
+const (
+	N = iota
+	S
+	E
+	W
+	L
+	R
+	F
+)
+
+type navi struct {
+	len int
+	act []int
+	val []int
+}
+
+func (this *navi) get(i int) (int, int) {
+	return this.act[i], this.val[i]
+}
+
+func readInput(filename string) (res navi) {
+	file, _ := os.Open(filename)
+	defer file.Close()
+
+	actMap := map[byte]int{
+		'N': N, 'S': S, 'E': E, 'W': W,
+		'L': L, 'R': R, 'F': F,
+	}
+	scanner := bufio.NewScanner(file)
+	for scanner.Scan() {
+		row := scanner.Text()
+		res.act = append(res.act, actMap[row[0]])
+		val, _ := strconv.Atoi(row[1:])
+		res.val = append(res.val, val)
+	}
+	res.len = len(res.act)
+	return
+}
+
+func main() {
+	input := readInput("./input.txt")
+	// 796
+	fmt.Println(part(input, exec1))
+	// 39446
+	fmt.Println(part(input, exec2))
+}
+
+func rotate(dir int, act int, val int) int {
+	left := map[int]int{N: W, W: S, S: E, E: N}
+	right := map[int]int{N: E, E: S, S: W, W: N}
+	for i := 0; i < val/90; i++ {
+		if act == L {
+			dir = left[dir]
+		} else if act == R {
+			dir = right[dir]
+		}
+	}
+	return dir
+}
+
+func exec1(input navi) (x, y int) {
+	loc := map[int]int{}
+	dir := E
+	for i := 0; i < input.len; i++ {
+		act, val := input.get(i)
+		switch act {
+		case L, R:
+			dir = rotate(dir, act, val)
+		case F:
+			loc[dir] += val
+		default:
+			loc[act] += val
+		}
+	}
+	return loc[E] - loc[W], loc[N] - loc[S]
+}
+
+func exec2(input navi) (x, y int) {
+	loc := map[int]int{}
+	wpt := map[int]int{E: 10, N: 1}
+	for i := 0; i < input.len; i++ {
+		act, val := input.get(i)
+		switch act {
+		case L, R:
+			wpt = map[int]int{
+				rotate(N, act, val): wpt[N],
+				rotate(E, act, val): wpt[E],
+				rotate(S, act, val): wpt[S],
+				rotate(W, act, val): wpt[W],
+			}
+		case F:
+			for _, dir := range []int{N, E, S, W} {
+				loc[dir] += wpt[dir] * val
+			}
+		default:
+			wpt[act] += val
+		}
+	}
+	return loc[E] - loc[W], loc[N] - loc[S]
+}
+
+func abs(x int) int {
+	if x < 0 {
+		return -x
+	}
+	return x
+}
+
+func part(input navi, exec func(navi) (int, int)) int {
+	x, y := exec(input)
+	return abs(x) + abs(y)
+}