AdventOfCode2021/06/main.go
2021-12-06 09:45:29 -05:00

118 lines
1.9 KiB
Go

package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
"time"
)
func main() {
start := time.Now()
partOne()
duration := time.Since(start)
partTwo()
duration2 := time.Since(start)
fmt.Printf("p1: %s, p2: %s\n", duration, duration2-duration)
}
func makeScanner(test bool) *bufio.Scanner {
var f *os.File
if test {
f, _ = os.Open("inputs/testinput")
} else {
f, _ = os.Open("inputs/input")
}
reader := bufio.NewReader(f)
return bufio.NewScanner(reader)
}
func mustAtoi(line string) int {
i, _ := strconv.Atoi(line)
return i
}
type school struct {
zero int
one int
two int
three int
four int
five int
six int
seven int
eight int
}
func (s *school) AddOneDay() {
s.zero, s.one, s.two, s.three, s.four, s.five, s.six, s.seven, s.eight = s.one, s.two, s.three, s.four, s.five, s.six, s.seven+s.zero, s.eight, s.zero
}
func (s *school) Sum() int {
return s.zero + s.one + s.two + s.three + s.four + s.five + s.six + s.seven + s.eight
}
func partOne() {
scanner := makeScanner(false)
// just a single line today
scanner.Scan()
input := strings.Split(scanner.Text(), ",")
school := school{}
for _, i := range input {
switch i {
case "0":
school.zero++
case "1":
school.one++
case "2":
school.two++
case "3":
school.three++
case "4":
school.four++
case "5":
school.five++
case "6":
school.six++
}
}
for i := 0; i < 80; i++ {
school.AddOneDay()
}
fmt.Println(school.Sum())
}
func partTwo() {
scanner := makeScanner(false)
// just a single line today
scanner.Scan()
input := strings.Split(scanner.Text(), ",")
school := school{}
for _, i := range input {
switch i {
case "0":
school.zero++
case "1":
school.one++
case "2":
school.two++
case "3":
school.three++
case "4":
school.four++
case "5":
school.five++
case "6":
school.six++
}
}
for i := 0; i < 256; i++ {
school.AddOneDay()
}
fmt.Println(school.Sum())
}