mirror of
https://github.com/RussellChamp/advent-of-code-2021.git
synced 2026-10-27 21:52:32 +00:00
improved template added timer
This commit is contained in:
1 parent
f6f6a849ec
commit
2992d3ba43
4 files changed
+62
-21
No files matched your search
+6
-2
@@ -2,6 +2,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"AoC2021/utils/timer"
|
||||||
"bufio"
|
"bufio"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
@@ -20,10 +21,13 @@ func check(e error) {
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
fmt.Println("--- Day 1: Sonar Sweep ---")
|
fmt.Println("--- Day 1: Sonar Sweep ---")
|
||||||
|
timer.Start()
|
||||||
part1()
|
part1()
|
||||||
|
timer.Tick()
|
||||||
fmt.Println()
|
fmt.Println()
|
||||||
|
|
||||||
part2()
|
part2()
|
||||||
|
timer.Tick()
|
||||||
fmt.Println()
|
fmt.Println()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -54,7 +58,7 @@ func part1() {
|
|||||||
lastValue = value
|
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() {
|
func part2() {
|
||||||
@@ -90,7 +94,7 @@ func part2() {
|
|||||||
w1, w2, w3 = w2, w3, value
|
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) {
|
func printPip(first int, previous int, current int, total int) {
|
||||||
|
|||||||
+37
-18
@@ -1,7 +1,8 @@
|
|||||||
/* Day #: */
|
/* Day #DAY#: #TITLE# */
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"AoC2021/utils/timer"
|
||||||
"bufio"
|
"bufio"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"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() {
|
func main() {
|
||||||
|
fmt.Println("--- Day #DAY#: #TITLE# ---")
|
||||||
|
timer.Start()
|
||||||
|
part1()
|
||||||
|
timer.Tick()
|
||||||
|
fmt.Println()
|
||||||
|
|
||||||
input1, err := os.Open("./input.txt")
|
part2()
|
||||||
check(err)
|
timer.Tick()
|
||||||
|
|
||||||
text := ReadAllLines(input1)
|
|
||||||
input1.Close()
|
|
||||||
|
|
||||||
fmt.Printf("Read %d lines from %s\n", len(text), "input.txt")
|
|
||||||
|
|
||||||
fmt.Println()
|
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: ")
|
||||||
|
}
|
||||||
@@ -2,7 +2,7 @@ default: 1
|
|||||||
# This is my 2021 Advent of Code project
|
# This is my 2021 Advent of Code project
|
||||||
|
|
||||||
init:
|
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:
|
0 1:
|
||||||
cd day-$@; go run .
|
cd day-$@; go run .
|
||||||
@@ -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
|
||||||
|
}
|
||||||
Reference in new issue
Block a user