!changed mathutil package to mathy, removed int/string casting from mathy

This commit is contained in:
alexchao26
2020-12-27 17:21:33 -05:00
parent 57fff6d829
commit 666518f507
114 changed files with 780 additions and 287 deletions
+2 -2
View File
@@ -5,7 +5,7 @@ import (
"fmt"
"strings"
"github.com/alexchao26/advent-of-code-go/mathutil"
"github.com/alexchao26/advent-of-code-go/cast"
"github.com/alexchao26/advent-of-code-go/util"
)
@@ -49,7 +49,7 @@ func part2(input string) int {
func parseInput(input string) (ans []int) {
for _, num := range strings.Split(input, "") {
ans = append(ans, mathutil.StrToInt(num))
ans = append(ans, cast.ToInt(num))
}
return ans
}
+4 -3
View File
@@ -5,7 +5,8 @@ import (
"fmt"
"strings"
"github.com/alexchao26/advent-of-code-go/mathutil"
"github.com/alexchao26/advent-of-code-go/cast"
"github.com/alexchao26/advent-of-code-go/mathy"
"github.com/alexchao26/advent-of-code-go/util"
)
@@ -28,7 +29,7 @@ func part1(input string) int {
rows := parseInput(input)
var checksum int
for _, r := range rows {
checksum += mathutil.MaxInt(r...) - mathutil.MinInt(r...)
checksum += mathy.MaxInt(r...) - mathy.MinInt(r...)
}
return checksum
}
@@ -58,7 +59,7 @@ func parseInput(input string) (ans [][]int) {
ans = append(ans, []int{})
// split by tabs
for _, num := range strings.Split(l, "\t") {
ans[i] = append(ans[i], mathutil.StrToInt(num))
ans[i] = append(ans[i], cast.ToInt(num))
}
}
return ans
+5 -4
View File
@@ -4,7 +4,8 @@ import (
"flag"
"fmt"
"github.com/alexchao26/advent-of-code-go/mathutil"
"github.com/alexchao26/advent-of-code-go/cast"
"github.com/alexchao26/advent-of-code-go/mathy"
"github.com/alexchao26/advent-of-code-go/util"
)
@@ -24,7 +25,7 @@ func main() {
}
func part1(input string) int {
inputNum := mathutil.StrToInt(input)
inputNum := cast.ToInt(input)
directions := [][2]int{
{0, 1}, // right
@@ -60,11 +61,11 @@ func part1(input string) int {
number++ // increment number
}
return mathutil.ManhattanDistance(0, 0, row, col)
return mathy.ManhattanDistance(0, 0, row, col)
}
func part2(input string) int {
inputNum := mathutil.StrToInt(input)
inputNum := cast.ToInt(input)
directions := [][2]int{
{0, 1}, // right
+2 -2
View File
@@ -5,7 +5,7 @@ import (
"fmt"
"strings"
"github.com/alexchao26/advent-of-code-go/mathutil"
"github.com/alexchao26/advent-of-code-go/cast"
"github.com/alexchao26/advent-of-code-go/util"
)
@@ -58,7 +58,7 @@ func part2(input string) int {
func parseInput(input string) (ans []int) {
lines := strings.Split(input, "\n")
for _, l := range lines {
ans = append(ans, mathutil.StrToInt(l))
ans = append(ans, cast.ToInt(l))
}
return ans
}
+2 -2
View File
@@ -5,7 +5,7 @@ import (
"fmt"
"strings"
"github.com/alexchao26/advent-of-code-go/mathutil"
"github.com/alexchao26/advent-of-code-go/cast"
"github.com/alexchao26/advent-of-code-go/util"
)
@@ -71,7 +71,7 @@ func memoryReallocation(input string, part int) int {
func parseInput(input string) (ans [16]int) {
nums := strings.Split(input, "\t")
for i, num := range nums {
ans[i] = mathutil.StrToInt(num)
ans[i] = cast.ToInt(num)
}
return ans
}
+3 -3
View File
@@ -5,7 +5,7 @@ import (
"fmt"
"strings"
"github.com/alexchao26/advent-of-code-go/mathutil"
"github.com/alexchao26/advent-of-code-go/cast"
"github.com/alexchao26/advent-of-code-go/util"
)
@@ -41,7 +41,7 @@ func part1(input string) string {
}
if len(allNames) != 1 {
panic("Expected one name left, got" + mathutil.IntToStr(len(allNames)))
panic("Expected one name left, got" + cast.ToString(len(allNames)))
}
// have to iterate over graph to get remaining name
@@ -138,7 +138,7 @@ func parseInput(input string) map[string]graphNode {
leftParts := strings.Split(parts[0], " ")
name := leftParts[0]
weight := mathutil.StrToInt(leftParts[1][1 : len(leftParts[1])-1])
weight := cast.ToInt(leftParts[1][1 : len(leftParts[1])-1])
var edges []string
if len(parts) == 2 {
+6 -6
View File
@@ -6,8 +6,8 @@ import (
"math"
"strings"
"github.com/alexchao26/advent-of-code-go/mathutil"
"github.com/alexchao26/advent-of-code-go/cast"
"github.com/alexchao26/advent-of-code-go/mathy"
"github.com/alexchao26/advent-of-code-go/util"
)
@@ -29,7 +29,7 @@ func calcRegisters(input string, part int) int {
var highestEverRegister int // for part 2
for _, inst := range instructions {
registerVal := registers[inst.conditional[0]]
compareVal := mathutil.StrToInt(inst.conditional[2])
compareVal := cast.ToInt(inst.conditional[2])
var conditionalResult bool
switch inst.conditional[1] {
case "==":
@@ -50,12 +50,12 @@ func calcRegisters(input string, part int) int {
if conditionalResult {
registers[inst.registerName] += inst.diff
}
highestEverRegister = mathutil.MaxInt(highestEverRegister, registers[inst.registerName])
highestEverRegister = mathy.MaxInt(highestEverRegister, registers[inst.registerName])
}
largestFinalRegister := -math.MaxInt32
for _, v := range registers {
largestFinalRegister = mathutil.MaxInt(largestFinalRegister, v)
largestFinalRegister = mathy.MaxInt(largestFinalRegister, v)
}
if part == 1 {
@@ -82,7 +82,7 @@ func parseInput(input string) []instruction {
}
inst := instruction{
registerName: parts[0],
diff: mathutil.StrToInt(parts[2]),
diff: cast.ToInt(parts[2]),
conditional: [3]string{parts[4], parts[5], parts[6]},
}
if parts[1] == "dec" {
+2 -2
View File
@@ -5,7 +5,7 @@ import (
"fmt"
"strings"
"github.com/alexchao26/advent-of-code-go/mathutil"
"github.com/alexchao26/advent-of-code-go/cast"
"github.com/alexchao26/advent-of-code-go/util"
)
@@ -101,7 +101,7 @@ func reverse(nums []int, left, right int) []int {
func parseInput(input string) (ans []int) {
nums := strings.Split(input, ",")
for _, num := range nums {
ans = append(ans, mathutil.StrToInt(num))
ans = append(ans, cast.ToInt(num))
}
return ans
}
+5 -6
View File
@@ -5,8 +5,7 @@ import (
"fmt"
"strings"
"github.com/alexchao26/advent-of-code-go/mathutil"
"github.com/alexchao26/advent-of-code-go/mathy"
"github.com/alexchao26/advent-of-code-go/util"
)
@@ -36,7 +35,7 @@ func hexEd(input string, part int) int {
for _, step := range steps {
tallyDirections[dirIndices[step]]++
distanceFromStart := getDistanceFromOrigin(tallyDirections)
furthest = mathutil.MaxInt(furthest, distanceFromStart)
furthest = mathy.MaxInt(furthest, distanceFromStart)
}
if part == 1 {
@@ -51,7 +50,7 @@ func getDistanceFromOrigin(tally []int) int {
for i := range tally {
if tally[i] != 0 {
oppositeIndex := (i + 3) % 6
smaller := mathutil.MinInt(tally[oppositeIndex], tally[i])
smaller := mathy.MinInt(tally[oppositeIndex], tally[i])
tally[oppositeIndex] -= smaller
tally[i] -= smaller
}
@@ -63,14 +62,14 @@ func getDistanceFromOrigin(tally []int) int {
toLeft := (i + 5) % 6
toRight := (i + 1) % 6
if tally[toLeft] > 0 && tally[toRight] > 0 {
smaller := mathutil.MinInt(tally[toLeft], tally[toRight])
smaller := mathy.MinInt(tally[toLeft], tally[toRight])
tally[toLeft] -= smaller
tally[toRight] -= smaller
tally[i] += smaller
}
}
distanceFromOrigin := mathutil.SumIntSlice(tally)
distanceFromOrigin := mathy.SumIntSlice(tally)
return distanceFromOrigin
}
+3 -3
View File
@@ -5,7 +5,7 @@ import (
"fmt"
"strings"
"github.com/alexchao26/advent-of-code-go/mathutil"
"github.com/alexchao26/advent-of-code-go/cast"
"github.com/alexchao26/advent-of-code-go/util"
)
@@ -89,9 +89,9 @@ func makeGraphFromInput(input string) map[int][]int {
graph := make(map[int][]int, len(lines))
for _, l := range lines {
parts := strings.Split(l, " <-> ")
ID := mathutil.StrToInt(parts[0])
ID := cast.ToInt(parts[0])
for _, child := range strings.Split(parts[1], ", ") {
graph[ID] = append(graph[ID], mathutil.StrToInt(child))
graph[ID] = append(graph[ID], cast.ToInt(child))
}
}
return graph
+2 -2
View File
@@ -5,7 +5,7 @@ import (
"fmt"
"strings"
"github.com/alexchao26/advent-of-code-go/mathutil"
"github.com/alexchao26/advent-of-code-go/cast"
"github.com/alexchao26/advent-of-code-go/util"
)
@@ -73,7 +73,7 @@ func parseInput(input string) (ans []int) {
lines := strings.Split(input, "\n")
for _, l := range lines {
split := strings.Split(l, " starts with ")
ans = append(ans, mathutil.StrToInt(split[1]))
ans = append(ans, cast.ToInt(split[1]))
}
return ans
}
+2 -3
View File
@@ -5,8 +5,7 @@ import (
"fmt"
"strings"
"github.com/alexchao26/advent-of-code-go/mathutil"
"github.com/alexchao26/advent-of-code-go/cast"
"github.com/alexchao26/advent-of-code-go/util"
)
@@ -43,7 +42,7 @@ func permPromenade(input string, part int) string {
for _, step := range steps {
switch step[0] {
case 's':
countToSpin := mathutil.StrToInt(step[1:])
countToSpin := cast.ToInt(step[1:])
fromEnd := programs[len(programs)-countToSpin:]
fromFront := programs[:len(programs)-countToSpin]
programs = append(fromEnd, fromFront...)
+3 -2
View File
@@ -5,7 +5,7 @@ import (
"fmt"
"log"
"github.com/alexchao26/advent-of-code-go/mathutil"
"github.com/alexchao26/advent-of-code-go/cast"
"github.com/alexchao26/advent-of-code-go/util"
)
@@ -26,7 +26,7 @@ type llNode struct {
}
func spinlock(input string, part int) int {
steps := mathutil.StrToInt(input)
steps := cast.ToInt(input)
lastNumToAdd := 2017
if part == 2 {
@@ -48,6 +48,7 @@ func spinlock(input string, part int) int {
current = current.next
// progress log for part 2 brute force...
// todo optimize this slow pos
if i%1000000 == 0 {
log.Println(i, "steps done")
}
+7 -1
View File
@@ -1,6 +1,7 @@
package main
import (
"fmt"
"testing"
"github.com/alexchao26/advent-of-code-go/util"
@@ -21,7 +22,12 @@ func Test_spinlock(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Logf("Running %s", tt.name)
if tt.name == "actual_part2" {
t.Log("This one could take a while, it has 50 million steps to run")
// Print using fmt b/c it will be messaged to the terminal regardless
// of verbose flag. To warn (me) that this test takes forever...
fmt.Println("WARNING: REALLY LONG TEST, 50 million steps to run")
if testing.Short() {
t.Skip("Skipping long test in short mode")
}
}
if got := spinlock(tt.input, tt.part); got != tt.want {
t.Errorf("spinlock() = %v, want %v", got, tt.want)
+2 -3
View File
@@ -7,8 +7,7 @@ import (
"sort"
"strings"
"github.com/alexchao26/advent-of-code-go/mathutil"
"github.com/alexchao26/advent-of-code-go/mathy"
"github.com/alexchao26/advent-of-code-go/util"
)
@@ -53,7 +52,7 @@ func part1(input string) int {
}
func sumAbs(nums [3]int) int {
return mathutil.AbsInt(nums[0]) + mathutil.AbsInt(nums[1]) + mathutil.AbsInt(nums[2])
return mathy.AbsInt(nums[0]) + mathy.AbsInt(nums[1]) + mathy.AbsInt(nums[2])
}
func part2(input string) int {
+2 -2
View File
@@ -4,7 +4,7 @@ import (
"fmt"
"strings"
"github.com/alexchao26/advent-of-code-go/mathutil"
"github.com/alexchao26/advent-of-code-go/mathy"
"github.com/alexchao26/advent-of-code-go/util"
)
@@ -34,7 +34,7 @@ func part1(input string) int {
currentStateName = rulesToFollow.nextState
}
return mathutil.SumIntSlice(bigArray)
return mathy.SumIntSlice(bigArray)
}
type ruleset struct {