From f6f6a849ece378ca57ff109ba8bbd55e3e8a5fad Mon Sep 17 00:00:00 2001 From: Russell Champoux Date: Wed, 1 Dec 2021 13:42:07 -0500 Subject: [PATCH] day 1 --- .gitignore | 3 +- day-1/main.go | 111 +++++++++++++++++++++++++++++++++++++++++++++++ main.go.template | 37 ++++++++++++++++ makefile | 7 ++- 4 files changed, 155 insertions(+), 3 deletions(-) create mode 100644 day-1/main.go create mode 100644 main.go.template diff --git a/.gitignore b/.gitignore index 704fb8d..fe93a5f 100644 --- a/.gitignore +++ b/.gitignore @@ -15,4 +15,5 @@ # vendor/ # Other stuff -*.log \ No newline at end of file +*.log +input.txt \ No newline at end of file diff --git a/day-1/main.go b/day-1/main.go new file mode 100644 index 0000000..906fcd5 --- /dev/null +++ b/day-1/main.go @@ -0,0 +1,111 @@ +/* Day 1: Sonar Sweep */ +package main + +import ( + "bufio" + "fmt" + "os" + "strconv" +) + +const DEBUG = true +const WIDTH = 100 + +func check(e error) { + if e != nil { + fmt.Println("Oh snap!") + panic(e) + } +} + +func main() { + fmt.Println("--- Day 1: Sonar Sweep ---") + part1() + fmt.Println() + + part2() + fmt.Println() +} + +func part1() { + fmt.Println("* Part 1 *") + fmt.Println(" Goal: Count the number of times a depth measurement increases from the previous measurement.") + fmt.Println(" Answer: How many measurements are larger than the previous measurement?") + + input, err := os.Open("./input.txt") + check(err) + defer input.Close() + + scanner := bufio.NewScanner(input) + + lastValue := -1 + measurements := 0 + increases := 0 + + for scanner.Scan() { + value, err := strconv.Atoi(scanner.Text()) + check(err) + measurements++ + if lastValue != -1 && lastValue < value { + increases++ + } + printPip(lastValue, lastValue, value, measurements) + + lastValue = value + } + + fmt.Printf("\nRead %d measurements resulting in %d increases\n", measurements, increases) +} + +func part2() { + fmt.Println("* Part 2 *") + fmt.Println(" Goal: Count the number of times the sum of measurements in this sliding window increases") + fmt.Println(" Answer: How many sums are larger than the previous sum?") + + input, err := os.Open("./input.txt") + check(err) + defer input.Close() + + scanner := bufio.NewScanner(input) + + w1, w2, w3 := -1, -1, -1 + measurements := 0 + increases := 0 + + for scanner.Scan() { + value, err := strconv.Atoi(scanner.Text()) + check(err) + measurements++ + + previous := w1 + w2 + w3 + current := w2 + w3 + value + + if w1 != -1 && current > previous { + increases++ + } + + printPip(w1, previous, current, measurements) + + // shift over the window + w1, w2, w3 = w2, w3, value + } + + fmt.Printf("\nRead %d measurements resulting in %d increases\n", measurements, increases) +} + +func printPip(first int, previous int, current int, total int) { + if !DEBUG { + return + } + + if first != -1 && current > previous { + fmt.Print("+") + } else if first != -1 && current < previous { + fmt.Print("-") + } else { + fmt.Print(".") + } + if total%WIDTH == 0 { + fmt.Println() // linebreak + } +} diff --git a/main.go.template b/main.go.template new file mode 100644 index 0000000..ac6a030 --- /dev/null +++ b/main.go.template @@ -0,0 +1,37 @@ +/* Day #: */ +package main + +import ( + "bufio" + "fmt" + "os" +) + +func check(e error) { + if e != nil { + fmt.Println("Oh snap!") + panic(e) + } +} + +func ReadAllLines(input *os.File) []string { + scanner := bufio.NewScanner(input) + var text []string + for scanner.Scan() { + text = append(text, scanner.Text()) + } + return text +} + +func main() { + + input1, err := os.Open("./input.txt") + check(err) + + text := ReadAllLines(input1) + input1.Close() + + fmt.Printf("Read %d lines from %s\n", len(text), "input.txt") + + fmt.Println() +} diff --git a/makefile b/makefile index 962a9d9..2991b79 100644 --- a/makefile +++ b/makefile @@ -1,5 +1,8 @@ -default: 0 +default: 1 # This is my 2021 Advent of Code project -0: +init: + mkdir day-$(day); sed 's/#/$(day)/' main.go.template > day-$(day)/main.go; touch day-$(day)/input.txt + +0 1: cd day-$@; go run . \ No newline at end of file