updated util packages

This commit is contained in:
alexchao26
2020-12-10 01:04:51 -05:00
parent b9a74cf122
commit 1fec4f74d3
47 changed files with 285 additions and 95 deletions
+1
View File
@@ -1 +1,2 @@
.vscode/*
input.txt
+5 -4
View File
@@ -6,6 +6,7 @@ import (
"math"
"strings"
"github.com/alexchao26/advent-of-code-go/mathutil"
"github.com/alexchao26/advent-of-code-go/util"
)
@@ -38,7 +39,7 @@ func part1(input string) int {
var coordsToBest [2]int
distCounts := map[int]int{} // dedeupe equidistant cells
for _, coord := range coords {
man := util.ManhattanDistance(r, c, coord[0], coord[1])
man := mathutil.ManhattanDistance(r, c, coord[0], coord[1])
if man <= bestManhattan {
bestManhattan = man
coordsToBest = coord
@@ -81,7 +82,7 @@ func part2(input string, dist int) int {
for c := boundLeft; c <= boundRight; c++ {
point := [2]int{r, c}
for _, coord := range coords {
coordsToTotalDist[point] += util.ManhattanDistance(point[0], point[1], coord[0], coord[1])
coordsToTotalDist[point] += mathutil.ManhattanDistance(point[0], point[1], coord[0], coord[1])
}
if coordsToTotalDist[point] < dist {
area++
@@ -99,8 +100,8 @@ func parseInputCoords(input string) [][2]int {
c := strings.Split(l, ", ")
if len(c) == 2 {
coords = append(coords, [2]int{
util.StrToInt(c[0]),
util.StrToInt(c[1]),
mathutil.StrToInt(c[0]),
mathutil.StrToInt(c[1]),
})
}
}
+2 -1
View File
@@ -7,6 +7,7 @@ import (
"sort"
"strings"
"github.com/alexchao26/advent-of-code-go/mathutil"
"github.com/alexchao26/advent-of-code-go/util"
)
@@ -100,7 +101,7 @@ func part2(input string, workers, fudgeTime int) int {
if len(prereqsCompletionTimes) == 0 {
readySteps = append(readySteps, name)
} else {
earliestScheduleTime := util.MaxInts(prereqsCompletionTimes...)
earliestScheduleTime := mathutil.MaxInt(prereqsCompletionTimes...)
if earliestScheduleTime <= time {
readySteps = append(readySteps, name)
}
+2 -1
View File
@@ -5,6 +5,7 @@ import (
"fmt"
"strings"
"github.com/alexchao26/advent-of-code-go/mathutil"
"github.com/alexchao26/advent-of-code-go/util"
)
@@ -50,7 +51,7 @@ func parseInput(input string) []int {
parsed := make([]int, len(split))
for i, v := range split {
parsed[i] = util.StrToInt(v)
parsed[i] = mathutil.StrToInt(v)
}
return parsed
+4 -3
View File
@@ -6,6 +6,7 @@ import (
"strconv"
"strings"
"github.com/alexchao26/advent-of-code-go/mathutil"
"github.com/alexchao26/advent-of-code-go/util"
)
@@ -53,13 +54,13 @@ func part1(input string) int {
playerTurn %= players
}
return util.MaxInts(playerScores...)
return mathutil.MaxInt(playerScores...)
}
func part2(input string) int {
// lazily modify input...
split := strings.Split(input, " ")
steps := util.StrToInt(split[6]) * 100
steps := mathutil.StrToInt(split[6]) * 100
split[6] = strconv.Itoa(steps)
return part1(strings.Join(split, " "))
@@ -67,7 +68,7 @@ func part2(input string) int {
func parseInput(input string) (players int, lastPoints int) {
split := strings.Split(input, " ")
return util.StrToInt(split[0]), util.StrToInt(split[6])
return mathutil.StrToInt(split[0]), mathutil.StrToInt(split[6])
}
type CircularLinkedListNode struct {
+3 -2
View File
@@ -7,6 +7,7 @@ import (
"strings"
"time"
"github.com/alexchao26/advent-of-code-go/mathutil"
"github.com/alexchao26/advent-of-code-go/util"
)
@@ -62,8 +63,8 @@ func parseInputs(input string) (positions [][2]int, velocities [][2]int) {
posY := strings.TrimSpace(l[18:24])
velX := strings.TrimSpace(l[36:38])
velY := strings.TrimSpace(l[40:42])
positions = append(positions, [2]int{util.StrToInt(posX), util.StrToInt(posY)})
velocities = append(velocities, [2]int{util.StrToInt(velX), util.StrToInt(velY)})
positions = append(positions, [2]int{mathutil.StrToInt(posX), mathutil.StrToInt(posY)})
velocities = append(velocities, [2]int{mathutil.StrToInt(velX), mathutil.StrToInt(velY)})
}
return positions, velocities
+2 -1
View File
@@ -5,6 +5,7 @@ import (
"fmt"
"strings"
"github.com/alexchao26/advent-of-code-go/mathutil"
"github.com/alexchao26/advent-of-code-go/util"
)
@@ -88,7 +89,7 @@ func part2(input string) string {
}
func parseInputs(input string) int {
return util.StrToInt(strings.TrimSpace(input))
return mathutil.StrToInt(strings.TrimSpace(input))
}
func generateGrid(gridSN int) [][]int {
+2 -1
View File
@@ -6,6 +6,7 @@ import (
"strconv"
"strings"
"github.com/alexchao26/advent-of-code-go/mathutil"
"github.com/alexchao26/advent-of-code-go/util"
)
@@ -65,7 +66,7 @@ func part2(input string) int {
func parseInput(input string) int {
lines := strings.Split(input, "\n")
return util.StrToInt(lines[0])
return mathutil.StrToInt(lines[0])
}
func step(recipes []int, elf1, elf2 int) ([]int, int, int) {
-2
View File
@@ -16,11 +16,9 @@ func main() {
if part == 1 {
ans := part1(util.ReadFile("./input.txt"))
util.CopyToClipboard(fmt.Sprintf("%v", ans))
fmt.Println("Output:", ans)
} else {
ans := part2(util.ReadFile("./input.txt"))
util.CopyToClipboard(fmt.Sprintf("%v", ans))
fmt.Println("Output:", ans)
}
}
-2
View File
@@ -16,11 +16,9 @@ func main() {
if part == 1 {
ans := part1(util.ReadFile("./input.txt"))
util.CopyToClipboard(fmt.Sprintf("%v", ans))
fmt.Println("Output:", ans)
} else {
ans := part2(util.ReadFile("./input.txt"))
util.CopyToClipboard(fmt.Sprintf("%v", ans))
fmt.Println("Output:", ans)
}
}
+4 -2
View File
@@ -6,11 +6,13 @@ MakePermutations is in the util package as that will likely be reused
package main
import (
"github.com/alexchao26/advent-of-code-go/util"
"fmt"
"log"
"strconv"
"strings"
"github.com/alexchao26/advent-of-code-go/algos"
"github.com/alexchao26/advent-of-code-go/util"
)
func main() {
@@ -25,7 +27,7 @@ func main() {
}
// Make perms via a util function
perms := util.MakePermutations([]int{0, 1, 2, 3, 4})
perms := algos.MakePermutations([]int{0, 1, 2, 3, 4})
// iterate over all perms and run through a single pass of the Amps
// if the final output (from Amp E) is higher, update the highestOutput variable
+4 -2
View File
@@ -6,11 +6,13 @@ MakePermutations is in the util package as that will likely be reused
package main
import (
"github.com/alexchao26/advent-of-code-go/util"
"fmt"
"log"
"strconv"
"strings"
"github.com/alexchao26/advent-of-code-go/algos"
"github.com/alexchao26/advent-of-code-go/util"
)
func main() {
@@ -25,7 +27,7 @@ func main() {
}
// Make perms via a util function
perms := util.MakePermutations([]int{5, 6, 7, 8, 9})
perms := algos.MakePermutations([]int{5, 6, 7, 8, 9})
// iterate over all perms and run through a single pass of the Amps
// if the final output (from Amp E) is higher, update the highestOutput variable
+6 -3
View File
@@ -9,11 +9,14 @@ Draw function generates a string to display in terminal
package main
import (
"github.com/alexchao26/advent-of-code-go/util"
"fmt"
"log"
"strconv"
"strings"
"github.com/alexchao26/advent-of-code-go/algos"
"github.com/alexchao26/advent-of-code-go/mathutil"
"github.com/alexchao26/advent-of-code-go/util"
)
func main() {
@@ -79,7 +82,7 @@ func Draw(mapCoordsToColor map[string]int) string {
}
// Determine the bounds of the grid
edgeLength := 2 * util.MaxInts(-lowY, -lowX, highY, highX)
edgeLength := 2 * mathutil.MaxInt(-lowY, -lowX, highY, highX)
grid := make([][]string, edgeLength)
for i := 0; i < edgeLength; i++ {
@@ -108,7 +111,7 @@ func Draw(mapCoordsToColor map[string]int) string {
// trim off due to making the initial grid too large
grid = trim(grid)
// rotate it because of how I coded up the robot's coordinates :/
grid = util.RotateGrid(grid)
grid = algos.RotateStringGrid(grid)
// retrim
grid = trim(grid)
+6 -3
View File
@@ -9,12 +9,15 @@ Robot struct houses an Intcode computer and its RecursiveMove method populates a
package main
import (
"github.com/alexchao26/advent-of-code-go/util"
"fmt"
"log"
"math"
"strconv"
"strings"
"github.com/alexchao26/advent-of-code-go/algos"
"github.com/alexchao26/advent-of-code-go/mathutil"
"github.com/alexchao26/advent-of-code-go/util"
)
func main() {
@@ -333,7 +336,7 @@ func Draw(mapCoordsToType map[string]int) [][]int {
}
// Determine the bounds of the grid
edgeLength := 2 * util.MaxInts(-lowY, -lowX, highY, highX)
edgeLength := 2 * mathutil.MaxInt(-lowY, -lowX, highY, highX)
grid := make([][]int, edgeLength)
for i := 0; i < edgeLength; i++ {
@@ -359,7 +362,7 @@ func Draw(mapCoordsToType map[string]int) [][]int {
// trim off due to making the initial grid too large
grid = trim(grid)
// rotate it because of how I coded up the robot's coordinates :/
grid = util.RotateGridInts(grid)
grid = algos.RotateIntGrid(grid)
// retrim
grid = trim(grid)
+6 -3
View File
@@ -12,11 +12,14 @@ Robot struct houses an Intcode computer and its RecursiveMove method populates a
package main
import (
"github.com/alexchao26/advent-of-code-go/util"
"fmt"
"log"
"strconv"
"strings"
"github.com/alexchao26/advent-of-code-go/algos"
"github.com/alexchao26/advent-of-code-go/mathutil"
"github.com/alexchao26/advent-of-code-go/util"
)
func main() {
@@ -387,7 +390,7 @@ func Draw(mapCoordsToType map[string]int) [][]int {
}
// Determine the bounds of the grid
edgeLength := 2 * util.MaxInts(-lowY, -lowX, highY, highX)
edgeLength := 2 * mathutil.MaxInt(-lowY, -lowX, highY, highX)
grid := make([][]int, edgeLength)
for i := 0; i < edgeLength; i++ {
@@ -413,7 +416,7 @@ func Draw(mapCoordsToType map[string]int) [][]int {
// trim off due to making the initial grid too large
grid = trim(grid)
// rotate it because of how I coded up the robot's coordinates :/
grid = util.RotateGridInts(grid)
grid = algos.RotateIntGrid(grid)
// retrim
grid = trim(grid)
+2 -1
View File
@@ -5,6 +5,7 @@ import (
"fmt"
"strings"
"github.com/alexchao26/advent-of-code-go/mathutil"
"github.com/alexchao26/advent-of-code-go/util"
)
@@ -60,7 +61,7 @@ func parseInputs(input string) []int {
nums := []int{}
for _, n := range split {
nums = append(nums, util.StrToInt(n))
nums = append(nums, mathutil.StrToInt(n))
}
return nums
+2 -1
View File
@@ -5,6 +5,7 @@ import (
"fmt"
"strings"
"github.com/alexchao26/advent-of-code-go/mathutil"
"github.com/alexchao26/advent-of-code-go/util"
)
@@ -58,7 +59,7 @@ func parseInput(input string) map[string]map[string]int {
continue
}
parts := strings.Split(content, " ")
graph[color][parts[1]+" "+parts[2]] = util.StrToInt(parts[0])
graph[color][parts[1]+" "+parts[2]] = mathutil.StrToInt(parts[0])
}
}
return graph
+4 -4
View File
@@ -29,7 +29,7 @@ func main() {
func part1(input string) int {
nums := parseInput(input)
nums = append(nums, util.MaxInts(nums...)+3)
nums = append(nums, mathutil.MaxInt(nums...)+3)
sort.Ints(nums)
var oneDiff, threeDiff int
@@ -51,7 +51,7 @@ func part1(input string) int {
func part2(input string) int {
nums := parseInput(input)
nums = append(nums, util.MaxInts(nums...)+3)
nums = append(nums, mathutil.MaxInt(nums...)+3)
sort.Ints(nums)
// return dynamicProgramming(input)
@@ -63,7 +63,7 @@ func parseInput(input string) []int {
lines := strings.Split(input, "\n")
for _, l := range lines {
ans = append(ans, util.StrToInt(l))
ans = append(ans, mathutil.StrToInt(l))
}
return ans
@@ -109,7 +109,7 @@ func makeMemoKey(nums []int, lastJolt int) string {
func dynamicProgramming(input string) int {
nums := parseInput(input)
nums = append(nums, util.MaxInts(nums...)+3, 0)
nums = append(nums, mathutil.MaxInt(nums...)+3, 0)
sort.Ints(nums)
// initialize table with "1 way" to get to zero jolts
+1 -1
View File
@@ -44,7 +44,7 @@ func parseInput(input string) []int {
lines := strings.Split(input, "\n")
for _, l := range lines {
ans = append(ans, util.StrToInt(l))
ans = append(ans, mathutil.StrToInt(l))
}
return ans
+1 -1
View File
@@ -44,7 +44,7 @@ func parseInput(input string) []int {
lines := strings.Split(input, "\n")
for _, l := range lines {
ans = append(ans, util.StrToInt(l))
ans = append(ans, mathutil.StrToInt(l))
}
return ans
+1 -1
View File
@@ -44,7 +44,7 @@ func parseInput(input string) []int {
lines := strings.Split(input, "\n")
for _, l := range lines {
ans = append(ans, util.StrToInt(l))
ans = append(ans, mathutil.StrToInt(l))
}
return ans
+1 -1
View File
@@ -44,7 +44,7 @@ func parseInput(input string) []int {
lines := strings.Split(input, "\n")
for _, l := range lines {
ans = append(ans, util.StrToInt(l))
ans = append(ans, mathutil.StrToInt(l))
}
return ans
+1 -1
View File
@@ -44,7 +44,7 @@ func parseInput(input string) []int {
lines := strings.Split(input, "\n")
for _, l := range lines {
ans = append(ans, util.StrToInt(l))
ans = append(ans, mathutil.StrToInt(l))
}
return ans
+1 -1
View File
@@ -44,7 +44,7 @@ func parseInput(input string) []int {
lines := strings.Split(input, "\n")
for _, l := range lines {
ans = append(ans, util.StrToInt(l))
ans = append(ans, mathutil.StrToInt(l))
}
return ans
+1 -1
View File
@@ -44,7 +44,7 @@ func parseInput(input string) []int {
lines := strings.Split(input, "\n")
for _, l := range lines {
ans = append(ans, util.StrToInt(l))
ans = append(ans, mathutil.StrToInt(l))
}
return ans
+1 -1
View File
@@ -44,7 +44,7 @@ func parseInput(input string) []int {
lines := strings.Split(input, "\n")
for _, l := range lines {
ans = append(ans, util.StrToInt(l))
ans = append(ans, mathutil.StrToInt(l))
}
return ans
+1 -1
View File
@@ -44,7 +44,7 @@ func parseInput(input string) []int {
lines := strings.Split(input, "\n")
for _, l := range lines {
ans = append(ans, util.StrToInt(l))
ans = append(ans, mathutil.StrToInt(l))
}
return ans
+2 -1
View File
@@ -5,6 +5,7 @@ import (
"fmt"
"strings"
"github.com/alexchao26/advent-of-code-go/mathutil"
"github.com/alexchao26/advent-of-code-go/util"
)
@@ -44,7 +45,7 @@ func parseInput(input string) []int {
lines := strings.Split(input, "\n")
for _, l := range lines {
ans = append(ans, util.StrToInt(l))
ans = append(ans, mathutil.StrToInt(l))
}
return ans
+2 -1
View File
@@ -5,6 +5,7 @@ import (
"fmt"
"strings"
"github.com/alexchao26/advent-of-code-go/mathutil"
"github.com/alexchao26/advent-of-code-go/util"
)
@@ -44,7 +45,7 @@ func parseInput(input string) []int {
lines := strings.Split(input, "\n")
for _, l := range lines {
ans = append(ans, util.StrToInt(l))
ans = append(ans, mathutil.StrToInt(l))
}
return ans
+2 -1
View File
@@ -5,6 +5,7 @@ import (
"fmt"
"strings"
"github.com/alexchao26/advent-of-code-go/mathutil"
"github.com/alexchao26/advent-of-code-go/util"
)
@@ -44,7 +45,7 @@ func parseInput(input string) []int {
lines := strings.Split(input, "\n")
for _, l := range lines {
ans = append(ans, util.StrToInt(l))
ans = append(ans, mathutil.StrToInt(l))
}
return ans
+2 -1
View File
@@ -5,6 +5,7 @@ import (
"fmt"
"strings"
"github.com/alexchao26/advent-of-code-go/mathutil"
"github.com/alexchao26/advent-of-code-go/util"
)
@@ -44,7 +45,7 @@ func parseInput(input string) []int {
lines := strings.Split(input, "\n")
for _, l := range lines {
ans = append(ans, util.StrToInt(l))
ans = append(ans, mathutil.StrToInt(l))
}
return ans
+2 -1
View File
@@ -5,6 +5,7 @@ import (
"fmt"
"strings"
"github.com/alexchao26/advent-of-code-go/mathutil"
"github.com/alexchao26/advent-of-code-go/util"
)
@@ -44,7 +45,7 @@ func parseInput(input string) []int {
lines := strings.Split(input, "\n")
for _, l := range lines {
ans = append(ans, util.StrToInt(l))
ans = append(ans, mathutil.StrToInt(l))
}
return ans
+2 -1
View File
@@ -5,6 +5,7 @@ import (
"fmt"
"strings"
"github.com/alexchao26/advent-of-code-go/mathutil"
"github.com/alexchao26/advent-of-code-go/util"
)
@@ -44,7 +45,7 @@ func parseInput(input string) []int {
lines := strings.Split(input, "\n")
for _, l := range lines {
ans = append(ans, util.StrToInt(l))
ans = append(ans, mathutil.StrToInt(l))
}
return ans
@@ -1,4 +1,4 @@
package util
package algos
// MakePermutations will make all permutations of the numbers input
// returns a pointer to avoid copying a large number of permutations
+3 -3
View File
@@ -1,8 +1,8 @@
package util
package algos
// RotateGrid returns the inputted grid, rotated counterclockwise
// call it multiple times for 180, & 270 degree rotations
func RotateGrid(grid [][]string) [][]string {
func RotateStringGrid(grid [][]string) [][]string {
rotated := make([][]string, len(grid[0]))
for i := range rotated {
rotated[i] = make([]string, len(grid))
@@ -17,7 +17,7 @@ func RotateGrid(grid [][]string) [][]string {
}
// RotateGridInts will transpose a 2D array of ints
func RotateGridInts(grid [][]int) [][]int {
func RotateIntGrid(grid [][]int) [][]int {
rotated := make([][]int, len(grid[0]))
for i := range rotated {
rotated[i] = make([]int, len(grid))
+25
View File
@@ -0,0 +1,25 @@
package algos
// SlidingWindowSum returns the left and right indices of a window within the
// nums slice, where all numbers in the slice [left:right] sum up to targetSum
// It also returns a boolean indicating if a valid window is found
func SlidingWindowSum(nums []int, targetSum int) (leftIndex, rightIndex int, found bool) {
var left, right, sum int
for right < len(nums) {
switch {
case left == right:
sum += nums[right]
right++
case sum > targetSum:
sum -= nums[left]
left++
case sum < targetSum:
sum += nums[right]
right++
}
if sum == targetSum {
return left, right, true
}
}
return 0, 0, false
}
+36
View File
@@ -0,0 +1,36 @@
package algos
import "testing"
func TestSlidingWindowSum(t *testing.T) {
type args struct {
nums []int
targetSum int
}
tests := []struct {
name string
args args
wantLeftIndex int
wantRightIndex int
wantFound bool
}{
{"found 1", args{[]int{-1, 23, 12, 14, 3, 4}, 21}, 3, 6, true},
{"found 2", args{[]int{-1, 23, 12, 14, 3, 4, 59}, 21}, 3, 6, true},
{"not found 1", args{[]int{0, 1, 2, 3, 4, 5, 6}, 45}, 0, 0, false},
{"not found 2", args{[]int{0, 1, 2, 3, 4, 5, 6}, -34}, 0, 0, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotLeftIndex, gotRightIndex, gotFound := SlidingWindowSum(tt.args.nums, tt.args.targetSum)
if gotLeftIndex != tt.wantLeftIndex {
t.Errorf("SlidingWindowSum() gotLeftIndex = %v, want %v", gotLeftIndex, tt.wantLeftIndex)
}
if gotRightIndex != tt.wantRightIndex {
t.Errorf("SlidingWindowSum() gotRightIndex = %v, want %v", gotRightIndex, tt.wantRightIndex)
}
if gotFound != tt.wantFound {
t.Errorf("SlidingWindowSum() gotFound = %v, want %v", gotFound, tt.wantFound)
}
})
}
}
+27
View File
@@ -0,0 +1,27 @@
package algos
// TwoSum returns the two numbers found within a slice that add up to
// the input target, and a boolean to check if the sum is found or not
func TwoSum(nums []int, target int) (num1 int, num2 int, found bool) {
seen := make(map[int]bool, len(nums))
for _, v := range nums {
if seen[target-v] {
return target - v, v, true
}
seen[v] = true
}
return 0, 0, false
}
// ThreeSum returns the three values within a slice that sum to the
// target input and a boolean stating if a match was found
func ThreeSum(nums []int, target int) (int, int, int, bool) {
for i, v := range nums {
if num1, num2, found := TwoSum(nums[i+1:], target-v); found {
return v, num1, num2, true
}
}
return 0, 0, 0, false
}
+76
View File
@@ -0,0 +1,76 @@
package algos
import "testing"
func TestTwoSum(t *testing.T) {
type args struct {
nums []int
target int
}
tests := []struct {
name string
args args
wantNum1 int
wantNum2 int
wantFound bool
}{
{"found 1", args{[]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 6}, 2, 4, true},
{"found 2", args{[]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 17}, 8, 9, true},
{"not found 1", args{[]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 62}, 0, 0, false},
{"not found 2", args{[]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, -12}, 0, 0, false},
{"found 3", args{[]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, -13}, -12}, 1, -13, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotNum1, gotNum2, gotFound := TwoSum(tt.args.nums, tt.args.target)
if gotNum1 != tt.wantNum1 {
t.Errorf("TwoSum() gotNum1 = %v, want %v", gotNum1, tt.wantNum1)
}
if gotNum2 != tt.wantNum2 {
t.Errorf("TwoSum() gotNum2 = %v, want %v", gotNum2, tt.wantNum2)
}
if gotFound != tt.wantFound {
t.Errorf("TwoSum() gotFound = %v, want %v", gotFound, tt.wantFound)
}
})
}
}
func TestThreeSum(t *testing.T) {
type args struct {
nums []int
target int
}
tests := []struct {
name string
args args
want int
want1 int
want2 int
want3 bool
}{
{"found 1", args{[]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 6}, 0, 2, 4, true},
{"found 2", args{[]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 17}, 0, 8, 9, true},
{"not found 1", args{[]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 62}, 0, 0, 0, false},
{"not found 2", args{[]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, -12}, 0, 0, 0, false},
{"found 3", args{[]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, -13}, -12}, 0, 1, -13, true},
{"found 3", args{[]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 27}, 8, 9, 10, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, got1, got2, got3 := ThreeSum(tt.args.nums, tt.args.target)
if got != tt.want {
t.Errorf("ThreeSum() got = %v, want %v", got, tt.want)
}
if got1 != tt.want1 {
t.Errorf("ThreeSum() got1 = %v, want %v", got1, tt.want1)
}
if got2 != tt.want2 {
t.Errorf("ThreeSum() got2 = %v, want %v", got2, tt.want2)
}
if got3 != tt.want3 {
t.Errorf("ThreeSum() got3 = %v, want %v", got3, tt.want3)
}
})
}
}
+1 -1
View File
@@ -1,4 +1,4 @@
package util
package mathutil
import (
"math"
+37
View File
@@ -0,0 +1,37 @@
package mathutil
import (
"fmt"
"strconv"
)
func MaxInt(nums ...int) int {
maxNum := nums[0]
for _, v := range nums {
if v > maxNum {
maxNum = v
}
}
return maxNum
}
func MinInt(nums ...int) int {
minNum := nums[0]
for _, v := range nums {
if v < minNum {
minNum = v
}
}
return minNum
}
func StrToInt(in string) int {
num, err := strconv.Atoi(in)
if err != nil {
panic(fmt.Sprintf("converting string to number: %s", err))
}
return num
}
func IntToStr(in int) string {
return strconv.Itoa(in)
}
+1 -1
View File
@@ -1,4 +1,4 @@
package util
package mathutil
import (
"math"
@@ -1,4 +1,4 @@
package util
package mathutil
import (
"fmt"
+1 -1
View File
@@ -104,7 +104,7 @@ func parseInput(input string) []int {
lines := strings.Split(input, "\n")
for _, l := range lines {
ans = append(ans, util.StrToInt(l))
ans = append(ans, mathutil.StrToInt(l))
}
return ans
-12
View File
@@ -1,12 +0,0 @@
package util
// MaxInts takes a variable number of integers and returns the largest one
func MaxInts(nums ...int) int {
maxNum := nums[0]
for _, v := range nums {
if v > maxNum {
maxNum = v
}
}
return maxNum
}
-10
View File
@@ -3,7 +3,6 @@ package util
import (
"io/ioutil"
"path"
"path/filepath"
"runtime"
"strings"
)
@@ -31,12 +30,3 @@ func ReadFile(pathFromCaller string) string {
strContent := string(content)
return strings.TrimRight(strContent, "\n")
}
// Dirname is a port of __dirname in node
func Dirname() string {
_, filename, _, ok := runtime.Caller(1)
if !ok {
panic("getting calling function")
}
return filepath.Dir(filename)
}
-14
View File
@@ -1,14 +0,0 @@
package util
import (
"fmt"
"strconv"
)
func StrToInt(in string) int {
num, err := strconv.Atoi(in)
if err != nil {
panic(fmt.Sprintf("converting string to number: %s", err))
}
return num
}