advent-of-code

Perserverance, or the lack thereof

git clone git://git.shimmy1996.com/advent-of-code.git

main.go (894B)

    1 package main
    2 
    3 import (
    4 	"bufio"
    5 	"fmt"
    6 	"os"
    7 	"sort"
    8 	"strconv"
    9 )
   10 
   11 func readInput(filename string) (res []int) {
   12 	file, _ := os.Open(filename)
   13 	defer file.Close()
   14 
   15 	scanner := bufio.NewScanner(file)
   16 	for scanner.Scan() {
   17 		num, _ := strconv.Atoi(scanner.Text())
   18 		res = append(res, num)
   19 	}
   20 	res = append(res, 0)
   21 	sort.Ints(res)
   22 	res = append(res, res[len(res)-1]+3)
   23 	return
   24 }
   25 
   26 func main() {
   27 	input := readInput("./input.txt")
   28 	// 1885
   29 	fmt.Println(part1(input))
   30 	// 2024782584832
   31 	fmt.Println(part2(input))
   32 }
   33 
   34 func part1(input []int) int {
   35 	diffCount := make(map[int]int)
   36 	for i := 1; i < len(input); i = i + 1 {
   37 		diffCount[input[i]-input[i-1]] += 1
   38 	}
   39 	return diffCount[1] * diffCount[3]
   40 }
   41 
   42 func part2(input []int) int64 {
   43 	arrCount := map[int]int64{0: 1}
   44 	for _, v := range input[1:] {
   45 		arrCount[v] = arrCount[v-1] + arrCount[v-2] + arrCount[v-3]
   46 	}
   47 	return arrCount[input[len(input)-1]]
   48 }