add timing tests to day5

This commit is contained in:
RussellChamp committed 2021-12-05 17:47:00 -05:00
1 parent 7f76abf47a
commit 6226b37c5b
4 files changed
+90 -25

No files matched your search

+22 -21
View File
@@ -2,6 +2,7 @@
package main package main
import ( import (
"AoC2021/utils/log"
"AoC2021/utils/timer" "AoC2021/utils/timer"
"bufio" "bufio"
"fmt" "fmt"
@@ -14,7 +15,7 @@ const MAX_SIZE = 1000
func check(e error) { func check(e error) {
if e != nil { if e != nil {
fmt.Println("Oh snap!") log.Println(log.DEBUG, "Oh snap!")
panic(e) panic(e)
} }
} }
@@ -23,11 +24,11 @@ func checkCoord(x int, y int) {
badVal := false badVal := false
if x < 0 || x >= MAX_SIZE { if x < 0 || x >= MAX_SIZE {
badVal = true badVal = true
fmt.Println("Invalid x value:", x) log.Println(log.DEBUG, "Invalid x value:", x)
} }
if y < 0 || y >= MAX_SIZE { if y < 0 || y >= MAX_SIZE {
badVal = true badVal = true
fmt.Println("Invalid y value:", y) log.Println(log.DEBUG, "Invalid y value:", y)
} }
if badVal { if badVal {
@@ -36,15 +37,15 @@ func checkCoord(x int, y int) {
} }
func main() { func main() {
fmt.Println("--- Day 5: Hydrothermal Venture ---") log.Println(log.NORMAL, "--- Day 5: Hydrothermal Venture ---")
timer.Start() timer.Start()
// part1() part1()
// timer.Tick() timer.Tick()
// fmt.Println() log.Println(log.NORMAL)
part2() part2()
timer.Tick() timer.Tick()
fmt.Println() log.Println(log.NORMAL)
} }
func parseLine(line string) (int, int, int, int) { func parseLine(line string) (int, int, int, int) {
@@ -94,9 +95,9 @@ func Sign(a int) int {
} }
func part1() { func part1() {
fmt.Println("* Part 1 *") log.Println(log.NORMAL, "* Part 1 *")
fmt.Println(" Goal: Determine the number of points where at least two lines overlap") log.Println(log.NORMAL, " Goal: Determine the number of points where at least two lines overlap")
fmt.Println(" Answer: At how many points do at least two lines overlap?") log.Println(log.NORMAL, " Answer: At how many points do at least two lines overlap?")
input, err := os.Open("./input.txt") input, err := os.Open("./input.txt")
check(err) check(err)
@@ -107,7 +108,7 @@ func part1() {
goodLines := 0 goodLines := 0
var grid [MAX_SIZE][MAX_SIZE]uint var grid [MAX_SIZE][MAX_SIZE]uint
// fmt.Printf("Initialized grid of size %d\n", MAX_SIZE) // log.Printf("Initialized grid of size %d\n", MAX_SIZE)
// Draw the lines // Draw the lines
for scanner.Scan() { for scanner.Scan() {
@@ -140,14 +141,14 @@ func part1() {
} }
} }
fmt.Printf("Read %d lines from input. Found %d straight segments\n", lines, goodLines) log.Printf(log.NORMAL, "Read %d lines from input. Found %d straight segments\n", lines, goodLines)
fmt.Printf("Found %d line intersections\n", intersections) log.Printf(log.NORMAL, "Found %d line intersections\n", intersections)
} }
func part2() { func part2() {
fmt.Println("* Part 2 *") log.Println(log.NORMAL, "* Part 2 *")
fmt.Println(" Goal: Including diagonal line segments, determine the number of points where at least two lines overlap") log.Println(log.NORMAL, " Goal: Including diagonal line segments, determine the number of points where at least two lines overlap")
fmt.Println(" Answer: At how many points do at least two lines overlap?") log.Println(log.NORMAL, " Answer: At how many points do at least two lines overlap?")
input, err := os.Open("./input.txt") input, err := os.Open("./input.txt")
check(err) check(err)
@@ -171,7 +172,7 @@ func part2() {
xVel := Sign(x2 - x1) xVel := Sign(x2 - x1)
yVel := Sign(y2 - y1) yVel := Sign(y2 - y1)
//fmt.Printf("Drawing line from (%d,%d) to (%d,%d) using velocity [%d,%d]", x1, y1, x2, y2, xVel, yVel) //log.Printf("Drawing line from (%d,%d) to (%d,%d) using velocity [%d,%d]", x1, y1, x2, y2, xVel, yVel)
dots := 0 dots := 0
for x, y := x1, y1; x != x2 || y != y2; { for x, y := x1, y1; x != x2 || y != y2; {
@@ -185,7 +186,7 @@ func part2() {
} }
// Mark the last point // Mark the last point
grid[x2][y2] += 1 grid[x2][y2] += 1
//fmt.Printf(" -- Drew %d dots\n", dots+1) //log.Printf(" -- Drew %d dots\n", dots+1)
} }
// Count the intersections // Count the intersections
intersections := 0 intersections := 0
@@ -197,6 +198,6 @@ func part2() {
} }
} }
fmt.Printf("Read %d lines from input.\n", lines) log.Printf(log.NORMAL, "Read %d lines from input.\n", lines)
fmt.Printf("Found %d line intersections\n", intersections) log.Printf(log.NORMAL, "Found %d line intersections\n", intersections)
} }
+22 -4
View File
@@ -1,11 +1,29 @@
package main package main
import ( import (
assert "AoC2021/utils/testing" "AoC2021/utils/log"
"AoC2021/utils/timing"
"fmt"
"testing" "testing"
"time"
) )
func TestDay5(t *testing.T) { const TRIALS = 1000
assert.IsTrue(t, true, "cool")
assert.StrEquals(t, "foo", "bar", "baz") func TestDay5Part1(t *testing.T) {
log.LOG_LEVEL = log.NONE
duration := timing.TimeFunction(part1, TRIALS)
avg := time.Duration(int64(duration) / int64(TRIALS))
fmt.Printf("Part1: [Took %s across %d trials for an average of %s]\n", duration.String(), TRIALS, avg)
}
func TestDay5Part2(t *testing.T) {
log.LOG_LEVEL = log.NONE
duration := timing.TimeFunction(part2, TRIALS)
avg := time.Duration(int64(duration) / int64(TRIALS))
fmt.Printf("Part2: [Took %s across %d trials for an average of %s]\n", duration.String(), TRIALS, avg)
} }
+32
View File
@@ -0,0 +1,32 @@
package log
import "fmt"
const (
NONE = 0
NORMAL = 1
DEBUG = 2
)
var LOG_LEVEL = NORMAL
func Print(minLevel int, a ...interface{}) (int, error) {
if LOG_LEVEL >= minLevel {
return fmt.Print(a...)
}
return 0, nil
}
func Println(minLevel int, a ...interface{}) (int, error) {
if LOG_LEVEL >= minLevel {
return fmt.Println(a...)
}
return 0, nil
}
func Printf(minLevel int, format string, a ...interface{}) (int, error) {
if LOG_LEVEL >= minLevel {
return fmt.Printf(format, a...)
}
return 0, nil
}
+14
View File
@@ -0,0 +1,14 @@
package timing
import "time"
func TimeFunction(fn func(), trials int) time.Duration {
startTime := time.Now()
for i := 0; i < trials; i++ {
fn()
}
endTime := time.Now()
return endTime.Sub(startTime)
}