advent-of-code

Perserverance, or the lack thereof

git clone git://git.shimmy1996.com/advent-of-code.git
commit 0c084d14a584da8b7bf14ca3c9893854be6a8984
parent 53f744f628cd0de8424875a410804a1918619ee9
Author: Shimmy Xu <shimmy.xu@shimmy1996.com>
Date:   Thu, 10 Dec 2020 20:04:02 -0600

Add 2020 day 10

Diffstat:
A2020/day10/input.txt | 93+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A2020/day10/main.go | 48++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 141 insertions(+), 0 deletions(-)
diff --git a/2020/day10/input.txt b/2020/day10/input.txt
@@ -0,0 +1,93 @@
+80
+87
+10
+122
+57
+142
+134
+59
+113
+139
+101
+41
+138
+112
+46
+96
+43
+125
+36
+54
+133
+17
+42
+98
+7
+114
+78
+67
+77
+28
+149
+58
+20
+105
+31
+19
+18
+27
+40
+71
+117
+66
+21
+72
+146
+90
+97
+94
+123
+1
+119
+30
+84
+61
+91
+118
+2
+29
+104
+73
+13
+76
+24
+148
+68
+111
+131
+83
+49
+8
+132
+9
+64
+79
+124
+95
+88
+135
+3
+51
+39
+6
+60
+108
+14
+35
+147
+89
+34
+65
+50
+145
+128
diff --git a/2020/day10/main.go b/2020/day10/main.go
@@ -0,0 +1,48 @@
+package main
+
+import (
+	"bufio"
+	"fmt"
+	"os"
+	"sort"
+	"strconv"
+)
+
+func readInput(filename string) (res []int) {
+	file, _ := os.Open(filename)
+	defer file.Close()
+
+	scanner := bufio.NewScanner(file)
+	for scanner.Scan() {
+		num, _ := strconv.Atoi(scanner.Text())
+		res = append(res, num)
+	}
+	res = append(res, 0)
+	sort.Ints(res)
+	res = append(res, res[len(res)-1]+3)
+	return
+}
+
+func main() {
+	input := readInput("./input.txt")
+	// 1885
+	fmt.Println(part1(input))
+	// 2024782584832
+	fmt.Println(part2(input))
+}
+
+func part1(input []int) int {
+	diffCount := make(map[int]int)
+	for i := 1; i < len(input); i = i + 1 {
+		diffCount[input[i]-input[i-1]] += 1
+	}
+	return diffCount[1] * diffCount[3]
+}
+
+func part2(input []int) int64 {
+	arrCount := map[int]int64{0: 1}
+	for _, v := range input[1:] {
+		arrCount[v] = arrCount[v-1] + arrCount[v-2] + arrCount[v-3]
+	}
+	return arrCount[input[len(input)-1]]
+}