From 55383aeedf6bbf2558f3770887fbb30e8c909895 Mon Sep 17 00:00:00 2001 From: David Ashby Date: Mon, 6 Dec 2021 09:45:29 -0500 Subject: [PATCH] day six is done --- 06/main.go | 117 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 06/main.go diff --git a/06/main.go b/06/main.go new file mode 100644 index 0000000..a837a12 --- /dev/null +++ b/06/main.go @@ -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()) +}