-
Notifications
You must be signed in to change notification settings - Fork 1
/
aoc6.go
57 lines (49 loc) · 967 Bytes
/
aoc6.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
// aoc6.go --
// advent of code 2021 day 6
//
// https://adventofcode.com/2021/day/6
// https://github.com/erik-adelbert/aoc
//
// (ɔ) Erik Adelbert - erik_AT_adelbert_DOT_fr
// -------------------------------------------
// 2021-12-6: initial commit
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
)
type popcnts [9]uint64
// incube computes fishes population.
func incube(a []uint64) {
i, n := len(a)-1, a[0]
copy(a, a[1:])
a[6], a[i] = a[6]+n, n
}
func popcnt(p popcnts) uint64 {
var popcnt uint64
for _, n := range p {
popcnt += n
}
return popcnt
}
func main() {
var fishes popcnts
input := bufio.NewScanner(os.Stdin)
for input.Scan() {
args := strings.Split(input.Text(), ",")
for _, arg := range args {
i, _ := strconv.Atoi(arg)
fishes[i]++
}
}
for i := 0; i < 256; i++ {
if i == 80 {
fmt.Println(popcnt(fishes)) // part1
}
incube(fishes[:]) // pass slice
}
fmt.Println(popcnt(fishes)) // part2
}