From 0b9bdf682db871de58ededa64e126a9fdd82429f Mon Sep 17 00:00:00 2001 From: David Ashby Date: Wed, 1 Dec 2021 09:28:43 -0500 Subject: [PATCH] day one --- 01/main.go | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 01/main.go diff --git a/01/main.go b/01/main.go new file mode 100644 index 0000000..121bbb4 --- /dev/null +++ b/01/main.go @@ -0,0 +1,61 @@ +package main + +import ( + "bufio" + "fmt" + "os" + "strconv" + "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 partOne() { + scanner := makeScanner(false) + + p := 0 + c := 0 + for scanner.Scan() { + line := scanner.Text() + n, _ := strconv.Atoi(line) + if p != 0 && n > p { + c++ + } + p = n + } + fmt.Println("part 1:", c) +} + +func partTwo() { + scanner := makeScanner(false) + + one, two, three := 0, 0, 0 + c := 0 + for scanner.Scan() { + line := scanner.Text() + n, _ := strconv.Atoi(line) + if one != 0 && two != 0 && three != 0 && one+two+three < two+three+n { + c++ + } + one, two, three = two, three, n + } + fmt.Println("part 2:", c) +}