Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions D01/benchmark_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package main

import (
"testing"
)

func BenchmarkPart1(b *testing.B) {
for n := 0; n < b.N; n++ {
b.StopTimer()
left, right := parseInputs()
b.StartTimer()
Part1(left, right)
}
}

func BenchmarkPart2(b *testing.B) {
for n := 0; n < b.N; n++ {
b.StopTimer()
left, right := parseInputs()
Part1(left, right)
b.StartTimer()
Part2(left, right)
}
}
52 changes: 22 additions & 30 deletions D01/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,44 +38,35 @@ func getTotalDistance(leftList, rightList []uint) uint {
return totalDistance
}

func parseFile(f *os.File) (left, right []uint, e error) {
func parseFile(f *os.File) (left, right []uint) {
sc := bufio.NewScanner(f)

left, right = make([]uint, 0, 100), make([]uint, 0, 100)

for sc.Scan() {
leftIntStr, spaceAndIntStr, found := strings.Cut(sc.Text(), " ")
leftIntStr, spaceAndIntStr, _ := strings.Cut(sc.Text(), " ")

if !found {
return nil, nil, fmt.Errorf("invalid file format")
}

l, err := strconv.ParseUint(leftIntStr, 10, 64)

if err != nil {
return nil, nil, err
}
l, _ := strconv.ParseUint(leftIntStr, 10, 64)

rightIntStr := strings.ReplaceAll(spaceAndIntStr, " ", "")

r, err := strconv.ParseUint(rightIntStr, 10, 64)

if err != nil {
return nil, nil, err
}
r, _ := strconv.ParseUint(rightIntStr, 10, 64)

left = append(left, uint(l))
right = append(right, uint(r))
}

return left, right, nil
return left, right
}

func Part1(left, right []uint) {
fmt.Printf("Part1: Total Distance = %d\n", getTotalDistance(left, right))
func Part1(left, right []uint) uint {
sortList(left)
sortList(right)

return getTotalDistance(left, right)
}

func Part2(left, right []uint) {
func Part2(left, right []uint) uint64 {
multMap := make(map[uint]uint)

for _, v := range left {
Expand All @@ -98,10 +89,10 @@ func Part2(left, right []uint) {
similarityScore = similarityScore + uint64(k)*uint64(v)
}

fmt.Printf("Part2: Similarity Score = %d\n", similarityScore)
return similarityScore
}

func main() {
func parseInputs() (left, right []uint) {
input, err := os.Open(INPUT_FILE)

if err != nil {
Expand All @@ -110,15 +101,16 @@ func main() {

defer input.Close()

left, right, err := parseFile(input)
left, right = parseFile(input)
return left, right
}

if err != nil {
panic(err)
}
func main() {

sortList(left)
sortList(right)
left, right := parseInputs()
sol1 := Part1(left, right)
similarityScore := Part2(left, right)

Part1(left, right)
Part2(left, right)
fmt.Printf("Part1: Total Distance = %d\n", sol1)
fmt.Printf("Part2: Similarity Score = %d\n", similarityScore)
}
44 changes: 44 additions & 0 deletions D02/benchmark_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package main

import "testing"

func BenchmarkPart1(b *testing.B) {
for n := 0; n < b.N; n++ {
b.StopTimer()
lines, err := readDataFileLines()

if err != nil {
panic(err)
}

reports, err := ParseLinesToReports(lines)

if err != nil {
panic(err)
}

b.StartTimer()
Part1(reports)
}
}

func BenchmarkPart2(b *testing.B) {
for n := 0; n < b.N; n++ {
b.StopTimer()
lines, err := readDataFileLines()

if err != nil {
panic(err)
}

reports, err := ParseLinesToReports(lines)

if err != nil {
panic(err)
}

Part1(reports)
b.StartTimer()
Part2(reports)
}
}
14 changes: 8 additions & 6 deletions D02/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func IsReportSafe(report Report) bool {
return (allIncreasing || allDecreasing) && adjacentDiffOk
}

func Part1(reports []Report) {
func Part1(reports []Report) int {
safeCtr := 0

for _, r := range reports {
Expand All @@ -123,7 +123,7 @@ func Part1(reports []Report) {
}
}

fmt.Printf("Part 1: Safe reports = %d\n", safeCtr)
return safeCtr
}

func tryApplyProblemDampener(report Report) bool {
Expand All @@ -139,7 +139,7 @@ func tryApplyProblemDampener(report Report) bool {
return false
}

func Part2(reports []Report) {
func Part2(reports []Report) int {
safeCtr := 0

for _, report := range reports {
Expand All @@ -156,7 +156,7 @@ func Part2(reports []Report) {
}
}

fmt.Printf("Part 2: Safe reports = %d\n", safeCtr)
return safeCtr
}

func main() {
Expand All @@ -173,6 +173,8 @@ func main() {
panic(err)
}

Part1(reports)
Part2(reports)
safeCtr := Part1(reports)
fmt.Printf("Part 1: Safe reports = %d\n", safeCtr)
safeCtr = Part2(reports)
fmt.Printf("Part 2: Safe reports = %d\n", safeCtr)
}
26 changes: 26 additions & 0 deletions D04/benchmark_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package main

import (
"testing"
)

func BenchmarkPart1(b *testing.B) {
for n := 0; n < b.N; n++ {
b.StopTimer()
mat := parseInputs()

b.StartTimer()
Part1(mat)
}
}

func BenchmarkPart2(b *testing.B) {
for n := 0; n < b.N; n++ {
b.StopTimer()
mat := parseInputs()

Part1(mat)
b.StartTimer()
Part2(mat)
}
}
27 changes: 16 additions & 11 deletions D04/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func countDiagonalMatches(mat Mat2, seq string, addScanReverse bool) int {
return matches
}

func Part1(mat Mat2) {
func Part1(mat Mat2) int {
seq := XMAS

matches := countHorizontalMatches(mat, seq, true)
Expand All @@ -113,7 +113,7 @@ func Part1(mat Mat2) {

mat.Reverse()

fmt.Printf("Part1: Found %d matches\n", matches)
return matches
}

func foundTypeA(mat Mat2, row, col int) bool {
Expand All @@ -136,7 +136,7 @@ func foundTypeD(mat Mat2, row, col int) bool {
return mat[row][col] == 'A' && mat[row-1][col-1] == 'S' && mat[row+1][col-1] == 'M' && mat[row-1][col+1] == 'S' && mat[row+1][col+1] == 'M'
}

func Part2(mat Mat2) {
func Part2(mat Mat2) int {

nrows, ncols := mat.NumRowsCols()

Expand All @@ -163,26 +163,31 @@ func Part2(mat Mat2) {
}
}

fmt.Printf("Part2: Found %d matches\n", matches)
return matches
}

func main() {

func parseInputs() Mat2 {
file, err := os.Open(FILE)

if err != nil {
fmt.Println(err)
return
panic(err)
}

defer file.Close()

mat := parseFileAsMatrix(file)
return parseFileAsMatrix(file)
}

func main() {

mat := parseInputs()

if rows, cols := mat.NumRowsCols(); rows != cols {
panic("Cannot handle non quadratic search matrices")
}

Part1(mat)
Part2(mat)
matches := Part1(mat)
fmt.Printf("Part1: Found %d matches\n", matches)
matches = Part2(mat)
fmt.Printf("Part2: Found %d matches\n", matches)
}
12 changes: 12 additions & 0 deletions D05/benchmark_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package main

import "testing"

func BenchmarkPart1And2(b *testing.B) {
for n := 0; n < b.N; n++ {
b.StopTimer()
updates, rules := parseInputs()
b.StartTimer()
Part1And2(updates, rules)
}
}
Loading