-
Notifications
You must be signed in to change notification settings - Fork 0
/
day06.go
65 lines (57 loc) · 1.43 KB
/
day06.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main
import (
"fmt"
"os"
"strconv"
"strings"
"github.com/dergeberl/aoc/utils"
)
type fishes []int
func main() {
input, err := os.ReadFile("input.txt")
if err != nil {
os.Exit(1)
}
fmt.Printf("Part 1: %v\n", SolveDay06Part1(string(input)))
fmt.Printf("Part 2: %v\n", SolveDay06Part2(string(input)))
}
//SolveDay06Part1 returns the number of lantern fishes after 80 days
func SolveDay06Part1(input string) int {
fish := getNumbersFromList(input)
return fish.calcLanternFish(80)
}
//SolveDay06Part2 returns the number of lantern fishes after 256 days
func SolveDay06Part2(input string) int {
fish := getNumbersFromList(input)
return fish.calcLanternFish(256)
}
// calcLanternFish2 returns the number of fishes after x days
func (f fishes) calcLanternFish(days int) int {
states := make([]int, 9)
for i := range f {
states[f[i]]++
}
for i := 1; i <= days; i++ {
birthFish := states[0]
states[0] = states[1]
states[1] = states[2]
states[2] = states[3]
states[3] = states[4]
states[4] = states[5]
states[5] = states[6]
states[6] = states[7] + birthFish
states[7] = states[8]
states[8] = birthFish
}
return utils.SumSlice(states)
}
// getNumbersFromList returns the fishes from an input
func getNumbersFromList(input string) fishes {
numbers := strings.Split(input, ",")
var output []int
for i := range numbers {
tmpInt, _ := strconv.Atoi(numbers[i])
output = append(output, tmpInt)
}
return output
}