day six is done

This commit is contained in:
David 2021-12-06 09:45:29 -05:00
parent fb73d56bd5
commit 55383aeedf
1 changed files with 117 additions and 0 deletions

117
06/main.go Normal file
View File

@ -0,0 +1,117 @@
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())
}