From 2992d3ba431d541e622cbfc38fc988009cb10c21 Mon Sep 17 00:00:00 2001 From: Russell Champoux Date: Wed, 1 Dec 2021 14:32:33 -0500 Subject: [PATCH] improved template added timer --- day-1/main.go | 8 +++++-- main.go.template | 55 +++++++++++++++++++++++++++++--------------- makefile | 2 +- utils/timer/timer.go | 18 +++++++++++++++ 4 files changed, 62 insertions(+), 21 deletions(-) create mode 100644 utils/timer/timer.go diff --git a/day-1/main.go b/day-1/main.go index 906fcd5..6d9de2f 100644 --- a/day-1/main.go +++ b/day-1/main.go @@ -2,6 +2,7 @@ package main import ( + "AoC2021/utils/timer" "bufio" "fmt" "os" @@ -20,10 +21,13 @@ func check(e error) { func main() { fmt.Println("--- Day 1: Sonar Sweep ---") + timer.Start() part1() + timer.Tick() fmt.Println() part2() + timer.Tick() fmt.Println() } @@ -54,7 +58,7 @@ func part1() { lastValue = value } - fmt.Printf("\nRead %d measurements resulting in %d increases\n", measurements, increases) + fmt.Printf("Read %d measurements resulting in %d increases\n", measurements, increases) } func part2() { @@ -90,7 +94,7 @@ func part2() { w1, w2, w3 = w2, w3, value } - fmt.Printf("\nRead %d measurements resulting in %d increases\n", measurements, increases) + fmt.Printf("Read %d measurements resulting in %d increases\n", measurements, increases) } func printPip(first int, previous int, current int, total int) { diff --git a/main.go.template b/main.go.template index ac6a030..8d065e4 100644 --- a/main.go.template +++ b/main.go.template @@ -1,7 +1,8 @@ -/* Day #: */ +/* Day #DAY#: #TITLE# */ package main import ( + "AoC2021/utils/timer" "bufio" "fmt" "os" @@ -14,24 +15,42 @@ func check(e error) { } } -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() { + fmt.Println("--- Day #DAY#: #TITLE# ---") + timer.Start() + part1() + timer.Tick() + fmt.Println() - 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") - + part2() + timer.Tick() fmt.Println() } + +func part1() { + fmt.Println("* Part 1 *") + fmt.Println(" Goal: ") + fmt.Println(" Answer: ") + + input, err := os.Open("./input.txt") + check(err) + defer input.Close() + + scanner := bufio.NewScanner(input) + lines := 0 + answer := 42 + + for scanner.Scan() { + _ = scanner.Text() + lines++ + } + + fmt.Printf("Read %d lines from input\n", lines) + fmt.Printf("The solution is %d\n", answer) +} + +func part2() { + fmt.Println("* Part 2 *") + fmt.Println(" Goal: ") + fmt.Println(" Answer: ") +} diff --git a/makefile b/makefile index 2991b79..5f2ead0 100644 --- a/makefile +++ b/makefile @@ -2,7 +2,7 @@ default: 1 # This is my 2021 Advent of Code project init: - mkdir day-$(day); sed 's/#/$(day)/' main.go.template > day-$(day)/main.go; touch day-$(day)/input.txt + mkdir day-$(day); sed 's/#DAY#/$(day)/g' main.go.template | sed 's/#TITLE#/$(title)/g' > day-$(day)/main.go; touch day-$(day)/input.txt 0 1: cd day-$@; go run . \ No newline at end of file diff --git a/utils/timer/timer.go b/utils/timer/timer.go new file mode 100644 index 0000000..bbba959 --- /dev/null +++ b/utils/timer/timer.go @@ -0,0 +1,18 @@ +package timer + +import ( + "fmt" + "time" +) + +var lastTime time.Time + +func Start() { + lastTime = time.Now() +} + +func Tick() { + now := time.Now() + fmt.Printf("[Time taken: %s]\n", now.Sub(lastTime).String()) + lastTime = now +}