improved template added timer

This commit is contained in:
RussellChamp committed 2021-12-01 14:32:33 -05:00
1 parent f6f6a849ec
commit 2992d3ba43
4 files changed
+62 -21

No files matched your search

+6 -2
View File
@@ -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) {
+37 -18
View File
@@ -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: ")
}
+1 -1
View File
@@ -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 .
+18
View File
@@ -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
}