From 666518f507d71761c1bd68f265e3012e36dfc0f1 Mon Sep 17 00:00:00 2001 From: alexchao26 Date: Sun, 27 Dec 2020 17:21:33 -0500 Subject: [PATCH] !changed mathutil package to mathy, removed int/string casting from mathy --- 2015/day02/main.go | 6 +- 2015/day07/main.go | 1 - 2015/day09/main.go | 11 +-- 2015/day11/main.go | 1 - 2015/day12/main.go | 1 - 2015/day13/main.go | 4 +- 2015/day14/main.go | 7 +- 2015/day15/main.go | 14 +-- 2015/day17/main.go | 4 +- 2015/day20/main.go | 1 - 2015/day21/main.go | 145 +++++++++++++++++++++++++++++ 2015/day21/main_test.go | 29 ++++++ 2015/day22/main.go | 46 +++++++++ 2015/day22/main_test.go | 39 ++++++++ 2015/day23/main.go | 46 +++++++++ 2015/day23/main_test.go | 39 ++++++++ 2015/day24/main.go | 46 +++++++++ 2015/day24/main_test.go | 39 ++++++++ 2015/day25/main.go | 46 +++++++++ 2015/day25/main_test.go | 39 ++++++++ 2016/day01/main.go | 7 +- 2016/day14/main_test.go | 4 + 2016/day20/main.go | 4 +- 2016/day22/main.go | 6 +- 2016/day24/main.go | 4 +- 2016/day25/main_test.go | 17 ---- 2017/day01/main.go | 4 +- 2017/day02/main.go | 7 +- 2017/day03/main.go | 9 +- 2017/day05/main.go | 4 +- 2017/day06/main.go | 4 +- 2017/day07/main.go | 6 +- 2017/day08/main.go | 12 +-- 2017/day10/main.go | 4 +- 2017/day11/main.go | 11 +-- 2017/day12/main.go | 6 +- 2017/day15/main.go | 4 +- 2017/day16/main.go | 5 +- 2017/day17/main.go | 5 +- 2017/day17/main_test.go | 8 +- 2017/day20/main.go | 5 +- 2017/day25/main.go | 4 +- 2018/day01/part1/main.go | 3 +- 2018/day01/part2/main.go | 3 +- 2018/day02/part1/main.go | 3 +- 2018/day02/part2/main.go | 3 +- 2018/day03/part1/main.go | 3 +- 2018/day03/part2/main.go | 3 +- 2018/day04/part1/main.go | 3 +- 2018/day04/part2/main.go | 3 +- 2018/day06/main.go | 11 ++- 2018/day07/main.go | 4 +- 2018/day08/main.go | 4 +- 2018/day09/main.go | 9 +- 2018/day10/main.go | 6 +- 2018/day11/main.go | 4 +- 2018/day14/main.go | 4 +- 2019/day02/part1/main.go | 3 +- 2019/day02/part2/main.go | 3 +- 2019/day03/part1/main.go | 3 +- 2019/day03/part2/main.go | 3 +- 2019/day05/part1/main.go | 3 +- 2019/day05/part2/main.go | 3 +- 2019/day06/part1/main.go | 3 +- 2019/day06/part2/main.go | 3 +- 2019/day08/part1/main.go | 3 +- 2019/day08/part2/main.go | 3 +- 2019/day10/part1/main.go | 3 +- 2019/day11/part2/main.go | 4 +- 2019/day12/part1/main.go | 3 +- 2019/day12/part2/main.go | 3 +- 2019/day13/part1/main.go | 3 +- 2019/day13/part2/main.go | 3 +- 2019/day14/part1/main.go | 3 +- 2019/day14/part2/main.go | 3 +- 2019/day15/part1/main.go | 12 +-- 2019/day15/part2/main.go | 4 +- 2019/day16/part1/main.go | 3 +- 2019/day16/part2/main.go | 3 +- 2019/day17/part1/main.go | 10 +- 2019/day17/part2/main.go | 8 +- 2019/day18/part1/main.go | 3 +- 2019/day18/part2/main.go | 3 +- 2019/day19/part1/main.go | 9 +- 2019/day19/part2/main.go | 9 +- 2019/day20/part1/main.go | 3 +- 2019/day20/part2/main.go | 3 +- 2019/day21/part1/main.go | 8 +- 2019/day21/part2/main.go | 9 +- 2019/day22/part1/main.go | 3 +- 2019/day23/part1/main.go | 3 +- 2019/day23/part2/main.go | 3 +- 2019/day24/part1/main.go | 3 +- 2019/day24/part2/main.go | 3 +- 2019/day25/part1/main.go | 8 +- 2019/day25/part2/main.go | 3 +- 2020/day01/main.go | 4 +- 2020/day07/main.go | 4 +- 2020/day10/main.go | 15 +-- 2020/day12/main.go | 9 +- 2020/day13/main.go | 6 +- 2020/day15/main.go | 4 +- 2020/day16/main.go | 7 +- 2020/day18/main.go | 20 ++-- 2020/day19/main.go | 7 +- 2020/day20/main.go | 1 - 2020/day22/main.go | 4 +- 2020/day24/main.go | 6 +- algos/random-nums.go | 13 +++ {mathutil => mathy}/distances.go | 2 +- {mathutil => mathy}/math.go | 19 +--- {mathutil => mathy}/primes.go | 2 +- {mathutil => mathy}/primes_test.go | 2 +- scripts/template/template.go | 4 +- 114 files changed, 780 insertions(+), 287 deletions(-) create mode 100644 2015/day21/main.go create mode 100644 2015/day21/main_test.go create mode 100644 2015/day22/main.go create mode 100644 2015/day22/main_test.go create mode 100644 2015/day23/main.go create mode 100644 2015/day23/main_test.go create mode 100644 2015/day24/main.go create mode 100644 2015/day24/main_test.go create mode 100644 2015/day25/main.go create mode 100644 2015/day25/main_test.go create mode 100644 algos/random-nums.go rename {mathutil => mathy}/distances.go (95%) rename {mathutil => mathy}/math.go (62%) rename {mathutil => mathy}/primes.go (97%) rename {mathutil => mathy}/primes_test.go (98%) diff --git a/2015/day02/main.go b/2015/day02/main.go index a433b4d..4c46f03 100644 --- a/2015/day02/main.go +++ b/2015/day02/main.go @@ -5,7 +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" ) @@ -35,7 +35,7 @@ func part1(input string) int { totalSqFt += x * y * 2 totalSqFt += x * z * 2 totalSqFt += z * y * 2 - totalSqFt += mathutil.MinInt(x*y, y*z, x*z) // slack in wrapping paper... + totalSqFt += mathy.MinInt(x*y, y*z, x*z) // slack in wrapping paper... } return totalSqFt @@ -56,7 +56,7 @@ func part2(input string) int { 2 * (y + z), 2 * (x + z), } - totalLen += mathutil.MinInt(sides...) + totalLen += mathy.MinInt(sides...) } return totalLen } diff --git a/2015/day07/main.go b/2015/day07/main.go index 9d7fdb5..f4632f6 100644 --- a/2015/day07/main.go +++ b/2015/day07/main.go @@ -8,7 +8,6 @@ import ( "strings" "github.com/alexchao26/advent-of-code-go/cast" - "github.com/alexchao26/advent-of-code-go/util" ) diff --git a/2015/day09/main.go b/2015/day09/main.go index 0c1e0d3..0ea7cae 100644 --- a/2015/day09/main.go +++ b/2015/day09/main.go @@ -5,9 +5,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" ) @@ -23,8 +22,8 @@ func travelingSalesman(input string) (int, int) { max := 0 for k := range graph { dfsMin, dfsMax := dfsTotalDistance(graph, k, map[string]bool{k: true}) - min = mathutil.MinInt(min, dfsMin) - max = mathutil.MaxInt(max, dfsMax) + min = mathy.MinInt(min, dfsMin) + max = mathy.MaxInt(max, dfsMax) } return min, max @@ -45,8 +44,8 @@ func dfsTotalDistance(graph mapGraph, entry string, visited map[string]bool) (mi weight := graph[entry][k] minRecurse, maxRecurse := dfsTotalDistance(graph, k, visited) - minDistance = mathutil.MinInt(minDistance, weight+minRecurse) - maxDistance = mathutil.MaxInt(maxDistance, weight+maxRecurse) + minDistance = mathy.MinInt(minDistance, weight+minRecurse) + maxDistance = mathy.MaxInt(maxDistance, weight+maxRecurse) // backtrack // delete to so length of visited is accurate diff --git a/2015/day11/main.go b/2015/day11/main.go index 34beb37..0354262 100644 --- a/2015/day11/main.go +++ b/2015/day11/main.go @@ -7,7 +7,6 @@ import ( "strings" "github.com/alexchao26/advent-of-code-go/cast" - "github.com/alexchao26/advent-of-code-go/util" ) diff --git a/2015/day12/main.go b/2015/day12/main.go index 5f7f307..8dc45bf 100644 --- a/2015/day12/main.go +++ b/2015/day12/main.go @@ -8,7 +8,6 @@ import ( "strings" "github.com/alexchao26/advent-of-code-go/cast" - "github.com/alexchao26/advent-of-code-go/util" ) diff --git a/2015/day13/main.go b/2015/day13/main.go index 5cd424f..9ac2a27 100644 --- a/2015/day13/main.go +++ b/2015/day13/main.go @@ -7,7 +7,7 @@ import ( "strings" "github.com/alexchao26/advent-of-code-go/algos" - "github.com/alexchao26/advent-of-code-go/mathutil" + "github.com/alexchao26/advent-of-code-go/mathy" "github.com/alexchao26/advent-of-code-go/util" ) @@ -40,7 +40,7 @@ func knightsOfTheDinnerTable(input string, part int) int { maxDiff := math.MinInt32 for _, p := range perms { - maxDiff = mathutil.MaxInt(maxDiff, calcHappinessDiff(graph, p)) + maxDiff = mathy.MaxInt(maxDiff, calcHappinessDiff(graph, p)) } return maxDiff diff --git a/2015/day14/main.go b/2015/day14/main.go index 120e23e..1e2a6bd 100644 --- a/2015/day14/main.go +++ b/2015/day14/main.go @@ -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" ) @@ -56,7 +55,7 @@ func reindeerOlympics(input string, part int) int { if part == 1 { var furthest int for _, distSli := range reindeerToDistanceMap { - furthest = mathutil.MaxInt(distSli[2503], furthest) + furthest = mathy.MaxInt(distSli[2503], furthest) } return furthest } @@ -82,7 +81,7 @@ func reindeerOlympics(input string, part int) int { var bestScore int for _, v := range reindeerScores { - bestScore = mathutil.MaxInt(bestScore, v) + bestScore = mathy.MaxInt(bestScore, v) } return bestScore diff --git a/2015/day15/main.go b/2015/day15/main.go index d2a4cab..a1c66e3 100644 --- a/2015/day15/main.go +++ b/2015/day15/main.go @@ -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" ) @@ -46,17 +46,17 @@ func cookieScience(input string) (int, int) { // make negatives zero, without this two negative scores could // make a very large positive - cap = mathutil.MaxInt(0, cap) - dur = mathutil.MaxInt(0, dur) - fla = mathutil.MaxInt(0, fla) - tex = mathutil.MaxInt(0, tex) + cap = mathy.MaxInt(0, cap) + dur = mathy.MaxInt(0, dur) + fla = mathy.MaxInt(0, fla) + tex = mathy.MaxInt(0, tex) score := cap * dur * fla * tex if cal == 500 { - best500CalScore = mathutil.MaxInt(best500CalScore, score) + best500CalScore = mathy.MaxInt(best500CalScore, score) } - bestScore = mathutil.MaxInt(bestScore, score) + bestScore = mathy.MaxInt(bestScore, score) } } } diff --git a/2015/day17/main.go b/2015/day17/main.go index bbf3a05..699fdf9 100644 --- a/2015/day17/main.go +++ b/2015/day17/main.go @@ -7,7 +7,7 @@ import ( "strings" "github.com/alexchao26/advent-of-code-go/cast" - "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 +36,7 @@ func eggnogCombinations(input string, target int, part int) int { // part 2, get the number of combinations w/ the lowest length minLen := math.MaxInt32 for _, comb := range allIndexCombinations { - minLen = mathutil.MinInt(minLen, len(comb)) + minLen = mathy.MinInt(minLen, len(comb)) } var count int diff --git a/2015/day20/main.go b/2015/day20/main.go index fb44b81..d4f8d18 100644 --- a/2015/day20/main.go +++ b/2015/day20/main.go @@ -6,7 +6,6 @@ import ( "math" "github.com/alexchao26/advent-of-code-go/cast" - "github.com/alexchao26/advent-of-code-go/util" ) diff --git a/2015/day21/main.go b/2015/day21/main.go new file mode 100644 index 0000000..96c2b6c --- /dev/null +++ b/2015/day21/main.go @@ -0,0 +1,145 @@ +package main + +import ( + "fmt" + "math" + "strings" + + "github.com/alexchao26/advent-of-code-go/cast" + "github.com/alexchao26/advent-of-code-go/mathy" + "github.com/alexchao26/advent-of-code-go/util" +) + +func main() { + ans1, ans2 := rpgSimulator(util.ReadFile("./input.txt")) + fmt.Printf("Part1: %d\nPart2: %d\n", ans1, ans2) +} + +func rpgSimulator(input string) (rpgSimulator, part2 int) { + bossHP, bossDamage, bossArmor, shopWeapons, shopArmor, shopRings := parseInput(input) + + // all attacks do at least 1 damage + // damage equal to attack - defenders armor + // armor is optional, limit 1 + // 0-2 rings allowed + // player attacks first + // must buy exactly 1 weapon + var combinations [][]item + for weapon := 0; weapon < len(shopWeapons); weapon++ { + for armor := -1; armor < len(shopArmor); armor++ { + for ring1 := -1; ring1 < len(shopRings); ring1++ { + for ring2 := -1; ring2 < len(shopRings); ring2++ { + comb := []item{shopWeapons[weapon]} + if armor != -1 { + comb = append(comb, shopArmor[armor]) + } + if ring1 != -1 { + comb = append(comb, shopRings[ring1]) + } + if ring2 != -1 && ring2 != ring1 { + comb = append(comb, shopRings[ring2]) + + } + + combinations = append(combinations, comb) + } + } + } + } + + minCost := math.MaxInt32 + var maxCost int + for _, comb := range combinations { + myHP := 100 + var myDamage, myArmor, cost int + for _, it := range comb { + myDamage += it.damage + myArmor += it.armor + cost += it.cost + } + playerWins := simulateBattle(bossHP, bossDamage, bossArmor, myHP, myDamage, myArmor) + if playerWins { + // part 1, min cost to win + minCost = mathy.MinInt(minCost, cost) + } else { + // part 2, max cost to still lose + maxCost = mathy.MaxInt(maxCost, cost) + } + } + + return minCost, maxCost +} + +func simulateBattle(bossHP, bossDamage, bossArmor, myHP, myDamage, myArmor int) (playerWins bool) { + attackOnBoss := myDamage - bossArmor + attackOnPlayer := bossDamage - myArmor + attackOnBoss = mathy.MaxInt(attackOnBoss, 1) + attackOnPlayer = mathy.MaxInt(attackOnPlayer, 1) + for bossHP > 0 && myHP > 0 { + bossHP -= attackOnBoss + myHP -= attackOnPlayer + } + + // the boss takes damage first, so if it hit zero or less, then the player + // won the round (potentially with very little HP left) + return bossHP <= 0 +} + +type item struct { + name string + cost, damage, armor int +} + +var shop = `Weapons: Cost Damage Armor +Dagger 8 4 0 +Shortsword 10 5 0 +Warhammer 25 6 0 +Longsword 40 7 0 +Greataxe 74 8 0 + +Armor: Cost Damage Armor +Leather 13 0 1 +Chainmail 31 0 2 +Splintmail 53 0 3 +Bandedmail 75 0 4 +Platemail 102 0 5 + +Rings: Cost Damage Armor +Damage +1 25 1 0 +Damage +2 50 2 0 +Damage +3 100 3 0 +Defense +1 20 0 1 +Defense +2 40 0 2 +Defense +3 80 0 3` + +func parseInput(input string) (hp, damage, armor int, shopWeapons, shopArmor, shopRings []item) { + lines := strings.Split(input, "\n") + hp = cast.ToInt(strings.Split(lines[0], ": ")[1]) + damage = cast.ToInt(strings.Split(lines[1], ": ")[1]) + armor = cast.ToInt(strings.Split(lines[2], ": ")[1]) + + shopBlocks := strings.Split(shop, "\n\n") + for blockIndex := range shopBlocks { + for _, line := range strings.Split(shopBlocks[blockIndex], "\n")[1:] { + it := item{} + if blockIndex == 2 { + // gross, have to get rid of whitespace in name for the rings + line = strings.ReplaceAll(line, "e +", "e+") + } + _, err := fmt.Sscanf(line, "%s %d %d %d", &it.name, &it.cost, &it.damage, &it.armor) + if err != nil { + panic(err) + } + switch blockIndex { + case 0: + shopWeapons = append(shopWeapons, it) + case 1: + shopArmor = append(shopArmor, it) + case 2: + shopRings = append(shopRings, it) + } + } + } + + return hp, damage, armor, shopWeapons, shopArmor, shopRings +} diff --git a/2015/day21/main_test.go b/2015/day21/main_test.go new file mode 100644 index 0000000..7df3684 --- /dev/null +++ b/2015/day21/main_test.go @@ -0,0 +1,29 @@ +package main + +import ( + "testing" + + "github.com/alexchao26/advent-of-code-go/util" +) + +func Test_rpgSimulator(t *testing.T) { + tests := []struct { + name string + input string + want1 int + want2 int + }{ + {"actual", util.ReadFile("input.txt"), 121, 201}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got1, got2 := rpgSimulator(tt.input) + if got1 != tt.want1 { + t.Errorf("rpgSimulator() = %v, want1 %v", got1, tt.want1) + } + if got2 != tt.want2 { + t.Errorf("rpgSimulator() = %v, want2 %v", got2, tt.want2) + } + }) + } +} diff --git a/2015/day22/main.go b/2015/day22/main.go new file mode 100644 index 0000000..4901f3c --- /dev/null +++ b/2015/day22/main.go @@ -0,0 +1,46 @@ +package main + +import ( + "flag" + "fmt" + "strings" + + "github.com/alexchao26/advent-of-code-go/cast" + "github.com/alexchao26/advent-of-code-go/util" +) + +func main() { + var part int + flag.IntVar(&part, "part", 1, "part 1 or 2") + flag.Parse() + fmt.Println("Running part", part) + + 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) + } +} + +func part1(input string) int { + parsed := parseInput(input) + _ = parsed + + return 0 +} + +func part2(input string) int { + return 0 +} + +func parseInput(input string) (ans []int) { + lines := strings.Split(input, "\n") + for _, l := range lines { + ans = append(ans, cast.ToInt(l)) + } + return ans +} diff --git a/2015/day22/main_test.go b/2015/day22/main_test.go new file mode 100644 index 0000000..22a4487 --- /dev/null +++ b/2015/day22/main_test.go @@ -0,0 +1,39 @@ +package main + +import ( + "testing" +) + +func Test_part1(t *testing.T) { + tests := []struct { + name string + input string + want int + }{ + // {"actual", util.ReadFile("input.txt"), ACTUAL_ANSWER}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := part1(tt.input); got != tt.want { + t.Errorf("part1() = %v, want %v", got, tt.want) + } + }) + } +} + +func Test_part2(t *testing.T) { + tests := []struct { + name string + input string + want int + }{ + // {"actual", util.ReadFile("input.txt"), ACTUAL_ANSWER}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := part2(tt.input); got != tt.want { + t.Errorf("part2() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/2015/day23/main.go b/2015/day23/main.go new file mode 100644 index 0000000..4901f3c --- /dev/null +++ b/2015/day23/main.go @@ -0,0 +1,46 @@ +package main + +import ( + "flag" + "fmt" + "strings" + + "github.com/alexchao26/advent-of-code-go/cast" + "github.com/alexchao26/advent-of-code-go/util" +) + +func main() { + var part int + flag.IntVar(&part, "part", 1, "part 1 or 2") + flag.Parse() + fmt.Println("Running part", part) + + 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) + } +} + +func part1(input string) int { + parsed := parseInput(input) + _ = parsed + + return 0 +} + +func part2(input string) int { + return 0 +} + +func parseInput(input string) (ans []int) { + lines := strings.Split(input, "\n") + for _, l := range lines { + ans = append(ans, cast.ToInt(l)) + } + return ans +} diff --git a/2015/day23/main_test.go b/2015/day23/main_test.go new file mode 100644 index 0000000..22a4487 --- /dev/null +++ b/2015/day23/main_test.go @@ -0,0 +1,39 @@ +package main + +import ( + "testing" +) + +func Test_part1(t *testing.T) { + tests := []struct { + name string + input string + want int + }{ + // {"actual", util.ReadFile("input.txt"), ACTUAL_ANSWER}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := part1(tt.input); got != tt.want { + t.Errorf("part1() = %v, want %v", got, tt.want) + } + }) + } +} + +func Test_part2(t *testing.T) { + tests := []struct { + name string + input string + want int + }{ + // {"actual", util.ReadFile("input.txt"), ACTUAL_ANSWER}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := part2(tt.input); got != tt.want { + t.Errorf("part2() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/2015/day24/main.go b/2015/day24/main.go new file mode 100644 index 0000000..4901f3c --- /dev/null +++ b/2015/day24/main.go @@ -0,0 +1,46 @@ +package main + +import ( + "flag" + "fmt" + "strings" + + "github.com/alexchao26/advent-of-code-go/cast" + "github.com/alexchao26/advent-of-code-go/util" +) + +func main() { + var part int + flag.IntVar(&part, "part", 1, "part 1 or 2") + flag.Parse() + fmt.Println("Running part", part) + + 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) + } +} + +func part1(input string) int { + parsed := parseInput(input) + _ = parsed + + return 0 +} + +func part2(input string) int { + return 0 +} + +func parseInput(input string) (ans []int) { + lines := strings.Split(input, "\n") + for _, l := range lines { + ans = append(ans, cast.ToInt(l)) + } + return ans +} diff --git a/2015/day24/main_test.go b/2015/day24/main_test.go new file mode 100644 index 0000000..22a4487 --- /dev/null +++ b/2015/day24/main_test.go @@ -0,0 +1,39 @@ +package main + +import ( + "testing" +) + +func Test_part1(t *testing.T) { + tests := []struct { + name string + input string + want int + }{ + // {"actual", util.ReadFile("input.txt"), ACTUAL_ANSWER}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := part1(tt.input); got != tt.want { + t.Errorf("part1() = %v, want %v", got, tt.want) + } + }) + } +} + +func Test_part2(t *testing.T) { + tests := []struct { + name string + input string + want int + }{ + // {"actual", util.ReadFile("input.txt"), ACTUAL_ANSWER}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := part2(tt.input); got != tt.want { + t.Errorf("part2() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/2015/day25/main.go b/2015/day25/main.go new file mode 100644 index 0000000..4901f3c --- /dev/null +++ b/2015/day25/main.go @@ -0,0 +1,46 @@ +package main + +import ( + "flag" + "fmt" + "strings" + + "github.com/alexchao26/advent-of-code-go/cast" + "github.com/alexchao26/advent-of-code-go/util" +) + +func main() { + var part int + flag.IntVar(&part, "part", 1, "part 1 or 2") + flag.Parse() + fmt.Println("Running part", part) + + 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) + } +} + +func part1(input string) int { + parsed := parseInput(input) + _ = parsed + + return 0 +} + +func part2(input string) int { + return 0 +} + +func parseInput(input string) (ans []int) { + lines := strings.Split(input, "\n") + for _, l := range lines { + ans = append(ans, cast.ToInt(l)) + } + return ans +} diff --git a/2015/day25/main_test.go b/2015/day25/main_test.go new file mode 100644 index 0000000..22a4487 --- /dev/null +++ b/2015/day25/main_test.go @@ -0,0 +1,39 @@ +package main + +import ( + "testing" +) + +func Test_part1(t *testing.T) { + tests := []struct { + name string + input string + want int + }{ + // {"actual", util.ReadFile("input.txt"), ACTUAL_ANSWER}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := part1(tt.input); got != tt.want { + t.Errorf("part1() = %v, want %v", got, tt.want) + } + }) + } +} + +func Test_part2(t *testing.T) { + tests := []struct { + name string + input string + want int + }{ + // {"actual", util.ReadFile("input.txt"), ACTUAL_ANSWER}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := part2(tt.input); got != tt.want { + t.Errorf("part2() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/2016/day01/main.go b/2016/day01/main.go index 03f820b..8b2efb5 100644 --- a/2016/day01/main.go +++ b/2016/day01/main.go @@ -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" ) @@ -52,11 +51,11 @@ func taxicab(input string, part int) int { row += dirs[dirIndex][0] col += dirs[dirIndex][1] if visited[[2]int{row, col}] && part == 2 { - return mathutil.ManhattanDistance(0, 0, row, col) + return mathy.ManhattanDistance(0, 0, row, col) } visited[[2]int{row, col}] = true } } - return mathutil.ManhattanDistance(0, 0, row, col) + return mathy.ManhattanDistance(0, 0, row, col) } diff --git a/2016/day14/main_test.go b/2016/day14/main_test.go index 1165e43..e6b29b9 100644 --- a/2016/day14/main_test.go +++ b/2016/day14/main_test.go @@ -1,6 +1,7 @@ package main import ( + "strings" "testing" "github.com/alexchao26/advent-of-code-go/util" @@ -20,6 +21,9 @@ func Test_part1(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { + if strings.Contains(tt.name, "part2") && testing.Short() { + t.Skip("Skipping long test, 2016/day14, a lot of MD5 hashes") + } if got := oneTimePad(tt.input, tt.part); got != tt.want { t.Errorf("part1() = %v, want %v", got, tt.want) } diff --git a/2016/day20/main.go b/2016/day20/main.go index 4103f72..cd3229a 100644 --- a/2016/day20/main.go +++ b/2016/day20/main.go @@ -7,7 +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" ) @@ -40,7 +40,7 @@ func firewall(input string, part int) int { for _, r := range allBlockedRanges { endOfLastRange := merged[len(merged)-1][1] if endOfLastRange >= r[0]-1 { - merged[len(merged)-1][1] = mathutil.MaxInt(endOfLastRange, r[1]) + merged[len(merged)-1][1] = mathy.MaxInt(endOfLastRange, r[1]) } else { merged = append(merged, r) } diff --git a/2016/day22/main.go b/2016/day22/main.go index c870eed..17d74fd 100644 --- a/2016/day22/main.go +++ b/2016/day22/main.go @@ -6,7 +6,7 @@ import ( "regexp" "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" ) @@ -54,8 +54,8 @@ func part2(input string) int { var maxX, maxY int var x, y int for c, n := range nodes { - maxX = mathutil.MaxInt(c[0], maxX) - maxY = mathutil.MaxInt(c[1], maxY) + maxX = mathy.MaxInt(c[0], maxX) + maxY = mathy.MaxInt(c[1], maxY) // getting the starting node, i.e. has zero used space if n.used == 0 { x = n.coord[1] diff --git a/2016/day24/main.go b/2016/day24/main.go index 64483cf..9b58668 100644 --- a/2016/day24/main.go +++ b/2016/day24/main.go @@ -8,7 +8,7 @@ import ( "strings" "github.com/alexchao26/advent-of-code-go/cast" - "github.com/alexchao26/advent-of-code-go/mathutil" + "github.com/alexchao26/advent-of-code-go/mathy" "github.com/alexchao26/advent-of-code-go/util" ) @@ -127,7 +127,7 @@ func dfs(graph [][]int, entryIndex int, visited map[int]bool, returnToZero bool) visited[i] = true dist := val + dfs(graph, i, visited, returnToZero) - minDistance = mathutil.MinInt(minDistance, dist) + minDistance = mathy.MinInt(minDistance, dist) delete(visited, i) } diff --git a/2016/day25/main_test.go b/2016/day25/main_test.go index 22a4487..77fb2fd 100644 --- a/2016/day25/main_test.go +++ b/2016/day25/main_test.go @@ -20,20 +20,3 @@ func Test_part1(t *testing.T) { }) } } - -func Test_part2(t *testing.T) { - tests := []struct { - name string - input string - want int - }{ - // {"actual", util.ReadFile("input.txt"), ACTUAL_ANSWER}, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if got := part2(tt.input); got != tt.want { - t.Errorf("part2() = %v, want %v", got, tt.want) - } - }) - } -} diff --git a/2017/day01/main.go b/2017/day01/main.go index 8b37fac..ddcc5b9 100644 --- a/2017/day01/main.go +++ b/2017/day01/main.go @@ -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 } diff --git a/2017/day02/main.go b/2017/day02/main.go index ff30877..b458dfd 100644 --- a/2017/day02/main.go +++ b/2017/day02/main.go @@ -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 diff --git a/2017/day03/main.go b/2017/day03/main.go index 6719724..6d4bb32 100644 --- a/2017/day03/main.go +++ b/2017/day03/main.go @@ -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 diff --git a/2017/day05/main.go b/2017/day05/main.go index a5d99b6..b2179c4 100644 --- a/2017/day05/main.go +++ b/2017/day05/main.go @@ -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 } diff --git a/2017/day06/main.go b/2017/day06/main.go index 83e07ef..e7dca93 100644 --- a/2017/day06/main.go +++ b/2017/day06/main.go @@ -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 } diff --git a/2017/day07/main.go b/2017/day07/main.go index f83cd76..4be1a6f 100644 --- a/2017/day07/main.go +++ b/2017/day07/main.go @@ -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 { diff --git a/2017/day08/main.go b/2017/day08/main.go index 124fc54..e66dac5 100644 --- a/2017/day08/main.go +++ b/2017/day08/main.go @@ -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" { diff --git a/2017/day10/main.go b/2017/day10/main.go index 6ba0f7c..0137cf1 100644 --- a/2017/day10/main.go +++ b/2017/day10/main.go @@ -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 } diff --git a/2017/day11/main.go b/2017/day11/main.go index c805415..cd0c0c5 100644 --- a/2017/day11/main.go +++ b/2017/day11/main.go @@ -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 } diff --git a/2017/day12/main.go b/2017/day12/main.go index 8c22e42..89c05db 100644 --- a/2017/day12/main.go +++ b/2017/day12/main.go @@ -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 diff --git a/2017/day15/main.go b/2017/day15/main.go index 23b5bb7..7bbdabc 100644 --- a/2017/day15/main.go +++ b/2017/day15/main.go @@ -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 } diff --git a/2017/day16/main.go b/2017/day16/main.go index a8e1ab4..56dd565 100644 --- a/2017/day16/main.go +++ b/2017/day16/main.go @@ -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...) diff --git a/2017/day17/main.go b/2017/day17/main.go index c2d6c59..a62396a 100644 --- a/2017/day17/main.go +++ b/2017/day17/main.go @@ -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") } diff --git a/2017/day17/main_test.go b/2017/day17/main_test.go index 2f2fc76..0aab217 100644 --- a/2017/day17/main_test.go +++ b/2017/day17/main_test.go @@ -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) diff --git a/2017/day20/main.go b/2017/day20/main.go index 1ae8fdd..dfc3f0e 100644 --- a/2017/day20/main.go +++ b/2017/day20/main.go @@ -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 { diff --git a/2017/day25/main.go b/2017/day25/main.go index 9ef1746..5b7475e 100644 --- a/2017/day25/main.go +++ b/2017/day25/main.go @@ -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 { diff --git a/2018/day01/part1/main.go b/2018/day01/part1/main.go index e424b20..07d7c6f 100644 --- a/2018/day01/part1/main.go +++ b/2018/day01/part1/main.go @@ -1,10 +1,11 @@ package main import ( - "github.com/alexchao26/advent-of-code-go/util" "fmt" "strconv" "strings" + + "github.com/alexchao26/advent-of-code-go/util" ) func main() { diff --git a/2018/day01/part2/main.go b/2018/day01/part2/main.go index bba55f6..acf1008 100644 --- a/2018/day01/part2/main.go +++ b/2018/day01/part2/main.go @@ -1,10 +1,11 @@ package main import ( - "github.com/alexchao26/advent-of-code-go/util" "fmt" "strconv" "strings" + + "github.com/alexchao26/advent-of-code-go/util" ) func main() { diff --git a/2018/day02/part1/main.go b/2018/day02/part1/main.go index adc23f9..67d20f4 100644 --- a/2018/day02/part1/main.go +++ b/2018/day02/part1/main.go @@ -1,9 +1,10 @@ package main import ( - "github.com/alexchao26/advent-of-code-go/util" "fmt" "strings" + + "github.com/alexchao26/advent-of-code-go/util" ) func main() { diff --git a/2018/day02/part2/main.go b/2018/day02/part2/main.go index 6f48542..62cdcc5 100644 --- a/2018/day02/part2/main.go +++ b/2018/day02/part2/main.go @@ -1,9 +1,10 @@ package main import ( - "github.com/alexchao26/advent-of-code-go/util" "fmt" "strings" + + "github.com/alexchao26/advent-of-code-go/util" ) // brute force it... diff --git a/2018/day03/part1/main.go b/2018/day03/part1/main.go index cb948a6..ec01a93 100644 --- a/2018/day03/part1/main.go +++ b/2018/day03/part1/main.go @@ -1,10 +1,11 @@ package main import ( - "github.com/alexchao26/advent-of-code-go/util" "fmt" "strconv" "strings" + + "github.com/alexchao26/advent-of-code-go/util" ) func main() { diff --git a/2018/day03/part2/main.go b/2018/day03/part2/main.go index 513876c..9d7ba30 100644 --- a/2018/day03/part2/main.go +++ b/2018/day03/part2/main.go @@ -1,10 +1,11 @@ package main import ( - "github.com/alexchao26/advent-of-code-go/util" "fmt" "strconv" "strings" + + "github.com/alexchao26/advent-of-code-go/util" ) func main() { diff --git a/2018/day04/part1/main.go b/2018/day04/part1/main.go index 7bd2a89..e686635 100644 --- a/2018/day04/part1/main.go +++ b/2018/day04/part1/main.go @@ -1,11 +1,12 @@ package main import ( - "github.com/alexchao26/advent-of-code-go/util" "fmt" "sort" "strconv" "strings" + + "github.com/alexchao26/advent-of-code-go/util" ) func main() { diff --git a/2018/day04/part2/main.go b/2018/day04/part2/main.go index aa36c64..60df4a3 100644 --- a/2018/day04/part2/main.go +++ b/2018/day04/part2/main.go @@ -1,11 +1,12 @@ package main import ( - "github.com/alexchao26/advent-of-code-go/util" "fmt" "sort" "strconv" "strings" + + "github.com/alexchao26/advent-of-code-go/util" ) func main() { diff --git a/2018/day06/main.go b/2018/day06/main.go index 7d3d808..42bb821 100644 --- a/2018/day06/main.go +++ b/2018/day06/main.go @@ -6,7 +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" ) @@ -39,7 +40,7 @@ func part1(input string) int { var coordsToBest [2]int distCounts := map[int]int{} // dedeupe equidistant cells for _, coord := range coords { - man := mathutil.ManhattanDistance(r, c, coord[0], coord[1]) + man := mathy.ManhattanDistance(r, c, coord[0], coord[1]) if man <= bestManhattan { bestManhattan = man coordsToBest = coord @@ -82,7 +83,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] += mathutil.ManhattanDistance(point[0], point[1], coord[0], coord[1]) + coordsToTotalDist[point] += mathy.ManhattanDistance(point[0], point[1], coord[0], coord[1]) } if coordsToTotalDist[point] < dist { area++ @@ -100,8 +101,8 @@ func parseInputCoords(input string) [][2]int { c := strings.Split(l, ", ") if len(c) == 2 { coords = append(coords, [2]int{ - mathutil.StrToInt(c[0]), - mathutil.StrToInt(c[1]), + cast.ToInt(c[0]), + cast.ToInt(c[1]), }) } } diff --git a/2018/day07/main.go b/2018/day07/main.go index 27e9226..83adbb2 100644 --- a/2018/day07/main.go +++ b/2018/day07/main.go @@ -7,7 +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" ) @@ -101,7 +101,7 @@ func part2(input string, workers, fudgeTime int) int { if len(prereqsCompletionTimes) == 0 { readySteps = append(readySteps, name) } else { - earliestScheduleTime := mathutil.MaxInt(prereqsCompletionTimes...) + earliestScheduleTime := mathy.MaxInt(prereqsCompletionTimes...) if earliestScheduleTime <= time { readySteps = append(readySteps, name) } diff --git a/2018/day08/main.go b/2018/day08/main.go index a601077..2812ebe 100644 --- a/2018/day08/main.go +++ b/2018/day08/main.go @@ -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" ) @@ -51,7 +51,7 @@ func parseInput(input string) []int { parsed := make([]int, len(split)) for i, v := range split { - parsed[i] = mathutil.StrToInt(v) + parsed[i] = cast.ToInt(v) } return parsed diff --git a/2018/day09/main.go b/2018/day09/main.go index e373587..3e2da30 100644 --- a/2018/day09/main.go +++ b/2018/day09/main.go @@ -6,7 +6,8 @@ import ( "strconv" "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" ) @@ -54,13 +55,13 @@ func part1(input string) int { playerTurn %= players } - return mathutil.MaxInt(playerScores...) + return mathy.MaxInt(playerScores...) } func part2(input string) int { // lazily modify input... split := strings.Split(input, " ") - steps := mathutil.StrToInt(split[6]) * 100 + steps := cast.ToInt(split[6]) * 100 split[6] = strconv.Itoa(steps) return part1(strings.Join(split, " ")) @@ -68,7 +69,7 @@ func part2(input string) int { func parseInput(input string) (players int, lastPoints int) { split := strings.Split(input, " ") - return mathutil.StrToInt(split[0]), mathutil.StrToInt(split[6]) + return cast.ToInt(split[0]), cast.ToInt(split[6]) } type CircularLinkedListNode struct { diff --git a/2018/day10/main.go b/2018/day10/main.go index ff3e8fb..46360bc 100644 --- a/2018/day10/main.go +++ b/2018/day10/main.go @@ -7,7 +7,7 @@ import ( "strings" "time" - "github.com/alexchao26/advent-of-code-go/mathutil" + "github.com/alexchao26/advent-of-code-go/cast" "github.com/alexchao26/advent-of-code-go/util" ) @@ -63,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{mathutil.StrToInt(posX), mathutil.StrToInt(posY)}) - velocities = append(velocities, [2]int{mathutil.StrToInt(velX), mathutil.StrToInt(velY)}) + positions = append(positions, [2]int{cast.ToInt(posX), cast.ToInt(posY)}) + velocities = append(velocities, [2]int{cast.ToInt(velX), cast.ToInt(velY)}) } return positions, velocities diff --git a/2018/day11/main.go b/2018/day11/main.go index 30ba848..964e356 100644 --- a/2018/day11/main.go +++ b/2018/day11/main.go @@ -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,7 +89,7 @@ func part2(input string) string { } func parseInputs(input string) int { - return mathutil.StrToInt(strings.TrimSpace(input)) + return cast.ToInt(strings.TrimSpace(input)) } func generateGrid(gridSN int) [][]int { diff --git a/2018/day14/main.go b/2018/day14/main.go index 420f0cb..ad85087 100644 --- a/2018/day14/main.go +++ b/2018/day14/main.go @@ -6,7 +6,7 @@ import ( "strconv" "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" ) @@ -66,7 +66,7 @@ func part2(input string) int { func parseInput(input string) int { lines := strings.Split(input, "\n") - return mathutil.StrToInt(lines[0]) + return cast.ToInt(lines[0]) } func step(recipes []int, elf1, elf2 int) ([]int, int, int) { diff --git a/2019/day02/part1/main.go b/2019/day02/part1/main.go index 4ee42c1..674ddd8 100644 --- a/2019/day02/part1/main.go +++ b/2019/day02/part1/main.go @@ -1,10 +1,11 @@ package main import ( - "github.com/alexchao26/advent-of-code-go/util" "fmt" "strconv" "strings" + + "github.com/alexchao26/advent-of-code-go/util" ) func main() { diff --git a/2019/day02/part2/main.go b/2019/day02/part2/main.go index 0522abb..e1d47c8 100644 --- a/2019/day02/part2/main.go +++ b/2019/day02/part2/main.go @@ -1,10 +1,11 @@ package main import ( - "github.com/alexchao26/advent-of-code-go/util" "fmt" "strconv" "strings" + + "github.com/alexchao26/advent-of-code-go/util" ) func main() { diff --git a/2019/day03/part1/main.go b/2019/day03/part1/main.go index a621e7d..0617bda 100644 --- a/2019/day03/part1/main.go +++ b/2019/day03/part1/main.go @@ -1,11 +1,12 @@ package main import ( - "github.com/alexchao26/advent-of-code-go/util" "fmt" "math" "strconv" "strings" + + "github.com/alexchao26/advent-of-code-go/util" ) func main() { diff --git a/2019/day03/part2/main.go b/2019/day03/part2/main.go index 54638fa..a2ece11 100644 --- a/2019/day03/part2/main.go +++ b/2019/day03/part2/main.go @@ -1,11 +1,12 @@ package main import ( - "github.com/alexchao26/advent-of-code-go/util" "fmt" "math" "strconv" "strings" + + "github.com/alexchao26/advent-of-code-go/util" ) func main() { diff --git a/2019/day05/part1/main.go b/2019/day05/part1/main.go index 18ca150..4e3a0a2 100644 --- a/2019/day05/part1/main.go +++ b/2019/day05/part1/main.go @@ -1,11 +1,12 @@ package main import ( - "github.com/alexchao26/advent-of-code-go/util" "fmt" "log" "strconv" "strings" + + "github.com/alexchao26/advent-of-code-go/util" ) func main() { diff --git a/2019/day05/part2/main.go b/2019/day05/part2/main.go index 165ac51..7cbf8f2 100644 --- a/2019/day05/part2/main.go +++ b/2019/day05/part2/main.go @@ -1,11 +1,12 @@ package main import ( - "github.com/alexchao26/advent-of-code-go/util" "fmt" "log" "strconv" "strings" + + "github.com/alexchao26/advent-of-code-go/util" ) func main() { diff --git a/2019/day06/part1/main.go b/2019/day06/part1/main.go index 6e1127f..4a84513 100644 --- a/2019/day06/part1/main.go +++ b/2019/day06/part1/main.go @@ -1,9 +1,10 @@ package main import ( - "github.com/alexchao26/advent-of-code-go/util" "fmt" "strings" + + "github.com/alexchao26/advent-of-code-go/util" ) func main() { diff --git a/2019/day06/part2/main.go b/2019/day06/part2/main.go index c4d602c..7afa205 100644 --- a/2019/day06/part2/main.go +++ b/2019/day06/part2/main.go @@ -1,9 +1,10 @@ package main import ( - "github.com/alexchao26/advent-of-code-go/util" "fmt" "strings" + + "github.com/alexchao26/advent-of-code-go/util" ) func main() { diff --git a/2019/day08/part1/main.go b/2019/day08/part1/main.go index c71be20..7b461e8 100644 --- a/2019/day08/part1/main.go +++ b/2019/day08/part1/main.go @@ -1,9 +1,10 @@ package main import ( - "github.com/alexchao26/advent-of-code-go/util" "fmt" "strings" + + "github.com/alexchao26/advent-of-code-go/util" ) func main() { diff --git a/2019/day08/part2/main.go b/2019/day08/part2/main.go index 924d100..f0d0f2d 100644 --- a/2019/day08/part2/main.go +++ b/2019/day08/part2/main.go @@ -1,9 +1,10 @@ package main import ( - "github.com/alexchao26/advent-of-code-go/util" "fmt" "strings" + + "github.com/alexchao26/advent-of-code-go/util" ) func main() { diff --git a/2019/day10/part1/main.go b/2019/day10/part1/main.go index 3a05c40..162be25 100644 --- a/2019/day10/part1/main.go +++ b/2019/day10/part1/main.go @@ -1,9 +1,10 @@ package main import ( - "github.com/alexchao26/advent-of-code-go/util" "fmt" "strings" + + "github.com/alexchao26/advent-of-code-go/util" ) func main() { diff --git a/2019/day11/part2/main.go b/2019/day11/part2/main.go index 63cd685..7ed86e8 100644 --- a/2019/day11/part2/main.go +++ b/2019/day11/part2/main.go @@ -15,7 +15,7 @@ import ( "strings" "github.com/alexchao26/advent-of-code-go/algos" - "github.com/alexchao26/advent-of-code-go/mathutil" + "github.com/alexchao26/advent-of-code-go/mathy" "github.com/alexchao26/advent-of-code-go/util" ) @@ -82,7 +82,7 @@ func Draw(mapCoordsToColor map[string]int) string { } // Determine the bounds of the grid - edgeLength := 2 * mathutil.MaxInt(-lowY, -lowX, highY, highX) + edgeLength := 2 * mathy.MaxInt(-lowY, -lowX, highY, highX) grid := make([][]string, edgeLength) for i := 0; i < edgeLength; i++ { diff --git a/2019/day12/part1/main.go b/2019/day12/part1/main.go index 2d76291..fc10cf2 100644 --- a/2019/day12/part1/main.go +++ b/2019/day12/part1/main.go @@ -1,10 +1,11 @@ package main import ( - "github.com/alexchao26/advent-of-code-go/util" "fmt" "strconv" "strings" + + "github.com/alexchao26/advent-of-code-go/util" ) // Moon stores coordinates and velocities of a moon diff --git a/2019/day12/part2/main.go b/2019/day12/part2/main.go index af1c679..f9b86f6 100644 --- a/2019/day12/part2/main.go +++ b/2019/day12/part2/main.go @@ -1,10 +1,11 @@ package main import ( - "github.com/alexchao26/advent-of-code-go/util" "fmt" "strconv" "strings" + + "github.com/alexchao26/advent-of-code-go/util" ) // Moon stores coordinates and velocities of a moon diff --git a/2019/day13/part1/main.go b/2019/day13/part1/main.go index 5077f06..4eda582 100644 --- a/2019/day13/part1/main.go +++ b/2019/day13/part1/main.go @@ -9,11 +9,12 @@ 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/util" ) func main() { diff --git a/2019/day13/part2/main.go b/2019/day13/part2/main.go index ea03639..f4fff4c 100644 --- a/2019/day13/part2/main.go +++ b/2019/day13/part2/main.go @@ -10,11 +10,12 @@ NOTE: The "hold for an input" value was changed from -1 to -2 in the Intcode com package main import ( - "github.com/alexchao26/advent-of-code-go/util" "fmt" "log" "strconv" "strings" + + "github.com/alexchao26/advent-of-code-go/util" ) func main() { diff --git a/2019/day14/part1/main.go b/2019/day14/part1/main.go index ec8d238..03894f9 100644 --- a/2019/day14/part1/main.go +++ b/2019/day14/part1/main.go @@ -1,10 +1,11 @@ package main import ( - "github.com/alexchao26/advent-of-code-go/util" "fmt" "strconv" "strings" + + "github.com/alexchao26/advent-of-code-go/util" ) func main() { diff --git a/2019/day14/part2/main.go b/2019/day14/part2/main.go index 03cf80b..fad08ce 100644 --- a/2019/day14/part2/main.go +++ b/2019/day14/part2/main.go @@ -1,10 +1,11 @@ package main import ( - "github.com/alexchao26/advent-of-code-go/util" "fmt" "strconv" "strings" + + "github.com/alexchao26/advent-of-code-go/util" ) // const will be comparable to any int type? diff --git a/2019/day15/part1/main.go b/2019/day15/part1/main.go index 4a55109..4735b07 100644 --- a/2019/day15/part1/main.go +++ b/2019/day15/part1/main.go @@ -1,11 +1,3 @@ -/* -Intcode struct is defined within this file -Robot struct houses an Intcode computer and its RecursiveMove method populates a map of - coordinates to the floor type (-1: wall, 1: hallway, 2: O2 tank, 5: origin) - That map is converted into a 2D grid (slice) - 2D slice is passed to a backtracking & searching algorithm to find the shortest path -*/ - package main import ( @@ -16,7 +8,7 @@ import ( "strings" "github.com/alexchao26/advent-of-code-go/algos" - "github.com/alexchao26/advent-of-code-go/mathutil" + "github.com/alexchao26/advent-of-code-go/mathy" "github.com/alexchao26/advent-of-code-go/util" ) @@ -336,7 +328,7 @@ func Draw(mapCoordsToType map[string]int) [][]int { } // Determine the bounds of the grid - edgeLength := 2 * mathutil.MaxInt(-lowY, -lowX, highY, highX) + edgeLength := 2 * mathy.MaxInt(-lowY, -lowX, highY, highX) grid := make([][]int, edgeLength) for i := 0; i < edgeLength; i++ { diff --git a/2019/day15/part2/main.go b/2019/day15/part2/main.go index 7945df1..f30af07 100644 --- a/2019/day15/part2/main.go +++ b/2019/day15/part2/main.go @@ -18,7 +18,7 @@ import ( "strings" "github.com/alexchao26/advent-of-code-go/algos" - "github.com/alexchao26/advent-of-code-go/mathutil" + "github.com/alexchao26/advent-of-code-go/mathy" "github.com/alexchao26/advent-of-code-go/util" ) @@ -390,7 +390,7 @@ func Draw(mapCoordsToType map[string]int) [][]int { } // Determine the bounds of the grid - edgeLength := 2 * mathutil.MaxInt(-lowY, -lowX, highY, highX) + edgeLength := 2 * mathy.MaxInt(-lowY, -lowX, highY, highX) grid := make([][]int, edgeLength) for i := 0; i < edgeLength; i++ { diff --git a/2019/day16/part1/main.go b/2019/day16/part1/main.go index 34b745d..faaaaa5 100644 --- a/2019/day16/part1/main.go +++ b/2019/day16/part1/main.go @@ -1,11 +1,12 @@ package main import ( - "github.com/alexchao26/advent-of-code-go/util" "fmt" "log" "strconv" "strings" + + "github.com/alexchao26/advent-of-code-go/util" ) func main() { diff --git a/2019/day16/part2/main.go b/2019/day16/part2/main.go index 86246e2..77226d7 100644 --- a/2019/day16/part2/main.go +++ b/2019/day16/part2/main.go @@ -1,11 +1,12 @@ package main import ( - "github.com/alexchao26/advent-of-code-go/util" "fmt" "strconv" "strings" "time" + + "github.com/alexchao26/advent-of-code-go/util" ) func main() { diff --git a/2019/day17/part1/main.go b/2019/day17/part1/main.go index 1340b1a..0feb13f 100644 --- a/2019/day17/part1/main.go +++ b/2019/day17/part1/main.go @@ -1,18 +1,12 @@ -/* -Intcode struct is defined within this file -Robot struct houses an Intcode computer and a method to initialize the floor details - - an algorithm in the main function traverses all tiles and checks all of its neighbors - - for all intersections that are found, their alignment parameters are calculated & added to a sum -*/ - package main import ( - "github.com/alexchao26/advent-of-code-go/util" "fmt" "log" "strconv" "strings" + + "github.com/alexchao26/advent-of-code-go/util" ) func main() { diff --git a/2019/day17/part2/main.go b/2019/day17/part2/main.go index 64fb544..02d0349 100644 --- a/2019/day17/part2/main.go +++ b/2019/day17/part2/main.go @@ -1,17 +1,13 @@ -/* -Intcode struct is defined within this file -Robot struct houses an Intcode computer and a method to initialize the floor details -*/ - package main import ( - "github.com/alexchao26/advent-of-code-go/util" "fmt" "log" "strconv" "strings" "time" + + "github.com/alexchao26/advent-of-code-go/util" ) func main() { diff --git a/2019/day18/part1/main.go b/2019/day18/part1/main.go index 40fc0dc..c7ad756 100644 --- a/2019/day18/part1/main.go +++ b/2019/day18/part1/main.go @@ -1,11 +1,12 @@ package main import ( - "github.com/alexchao26/advent-of-code-go/util" "fmt" "math" "strings" "time" + + "github.com/alexchao26/advent-of-code-go/util" ) func main() { diff --git a/2019/day18/part2/main.go b/2019/day18/part2/main.go index 9a86d7d..0aa25c5 100644 --- a/2019/day18/part2/main.go +++ b/2019/day18/part2/main.go @@ -1,11 +1,12 @@ package main import ( - "github.com/alexchao26/advent-of-code-go/util" "fmt" "math" "sort" "strings" + + "github.com/alexchao26/advent-of-code-go/util" ) func main() { diff --git a/2019/day19/part1/main.go b/2019/day19/part1/main.go index bd33510..8dbf94f 100644 --- a/2019/day19/part1/main.go +++ b/2019/day19/part1/main.go @@ -1,17 +1,12 @@ -/* -Intcode struct is defined within this file - - Every drone needs its own computer made, I let them get garbage collected as often - as possible, not sure how extendable this is going to be to part 2 -*/ - package main import ( - "github.com/alexchao26/advent-of-code-go/util" "fmt" "log" "strconv" "strings" + + "github.com/alexchao26/advent-of-code-go/util" ) func main() { diff --git a/2019/day19/part2/main.go b/2019/day19/part2/main.go index 9d7b18d..0b97b3b 100644 --- a/2019/day19/part2/main.go +++ b/2019/day19/part2/main.go @@ -1,17 +1,12 @@ -/* -Intcode struct is defined within this file - - Every drone needs its own computer made, I let them get garbage collected as often - as possible, not sure how extendable this is going to be to part 2 -*/ - package main import ( - "github.com/alexchao26/advent-of-code-go/util" "fmt" "log" "strconv" "strings" + + "github.com/alexchao26/advent-of-code-go/util" ) func main() { diff --git a/2019/day20/part1/main.go b/2019/day20/part1/main.go index d964e64..fc7f71f 100644 --- a/2019/day20/part1/main.go +++ b/2019/day20/part1/main.go @@ -1,10 +1,11 @@ package main import ( - "github.com/alexchao26/advent-of-code-go/util" "fmt" "math" "strings" + + "github.com/alexchao26/advent-of-code-go/util" ) func main() { diff --git a/2019/day20/part2/main.go b/2019/day20/part2/main.go index d433fa5..25326d8 100644 --- a/2019/day20/part2/main.go +++ b/2019/day20/part2/main.go @@ -1,10 +1,11 @@ package main import ( - "github.com/alexchao26/advent-of-code-go/util" "fmt" "math" "strings" + + "github.com/alexchao26/advent-of-code-go/util" ) func main() { diff --git a/2019/day21/part1/main.go b/2019/day21/part1/main.go index 66df800..5c5a97c 100644 --- a/2019/day21/part1/main.go +++ b/2019/day21/part1/main.go @@ -1,16 +1,12 @@ -/* -Intcode struct is defined within this file - -*/ - package main import ( - "github.com/alexchao26/advent-of-code-go/util" "fmt" "log" "strconv" "strings" + + "github.com/alexchao26/advent-of-code-go/util" ) func main() { diff --git a/2019/day21/part2/main.go b/2019/day21/part2/main.go index d36058a..2288ca9 100644 --- a/2019/day21/part2/main.go +++ b/2019/day21/part2/main.go @@ -1,17 +1,12 @@ -/* -Intcode struct is defined within this file -Helper function that converts strings to ASCII codes to be written to the computer - it could all be combined together into a new computer... -*/ - package main import ( - "github.com/alexchao26/advent-of-code-go/util" "fmt" "log" "strconv" "strings" + + "github.com/alexchao26/advent-of-code-go/util" ) func main() { diff --git a/2019/day22/part1/main.go b/2019/day22/part1/main.go index 0fd4fd1..5661cf6 100644 --- a/2019/day22/part1/main.go +++ b/2019/day22/part1/main.go @@ -1,10 +1,11 @@ package main import ( - "github.com/alexchao26/advent-of-code-go/util" "fmt" "strconv" "strings" + + "github.com/alexchao26/advent-of-code-go/util" ) type instruction struct { diff --git a/2019/day23/part1/main.go b/2019/day23/part1/main.go index fcb6983..447ef07 100644 --- a/2019/day23/part1/main.go +++ b/2019/day23/part1/main.go @@ -6,11 +6,12 @@ Intcode struct is defined within this file package main import ( - "github.com/alexchao26/advent-of-code-go/util" "fmt" "log" "strconv" "strings" + + "github.com/alexchao26/advent-of-code-go/util" ) func main() { diff --git a/2019/day23/part2/main.go b/2019/day23/part2/main.go index 48ab1d9..e68be11 100644 --- a/2019/day23/part2/main.go +++ b/2019/day23/part2/main.go @@ -7,11 +7,12 @@ Network struct to store 50 instances of Intcode computers and 50 queues for thei package main import ( - "github.com/alexchao26/advent-of-code-go/util" "fmt" "log" "strconv" "strings" + + "github.com/alexchao26/advent-of-code-go/util" ) func main() { diff --git a/2019/day24/part1/main.go b/2019/day24/part1/main.go index 6e02aeb..9c86a56 100644 --- a/2019/day24/part1/main.go +++ b/2019/day24/part1/main.go @@ -1,9 +1,10 @@ package main import ( - "github.com/alexchao26/advent-of-code-go/util" "fmt" "strings" + + "github.com/alexchao26/advent-of-code-go/util" ) func main() { diff --git a/2019/day24/part2/main.go b/2019/day24/part2/main.go index 8362369..ec4c0e1 100644 --- a/2019/day24/part2/main.go +++ b/2019/day24/part2/main.go @@ -1,9 +1,10 @@ package main import ( - "github.com/alexchao26/advent-of-code-go/util" "fmt" "strings" + + "github.com/alexchao26/advent-of-code-go/util" ) // RecursiveWorld stores a big 3D matrix & will have associated methods diff --git a/2019/day25/part1/main.go b/2019/day25/part1/main.go index da556e2..a7b0cdf 100644 --- a/2019/day25/part1/main.go +++ b/2019/day25/part1/main.go @@ -1,18 +1,14 @@ -/* -Intcode struct is defined within this file -Helper function that converts strings to ASCII codes to be written to the computer -*/ - package main import ( - "github.com/alexchao26/advent-of-code-go/util" "bufio" "fmt" "log" "os" "strconv" "strings" + + "github.com/alexchao26/advent-of-code-go/util" ) func main() { diff --git a/2019/day25/part2/main.go b/2019/day25/part2/main.go index 700a617..996a6c1 100644 --- a/2019/day25/part2/main.go +++ b/2019/day25/part2/main.go @@ -1,6 +1,7 @@ package main +import "fmt" + func main() { fmt.Println("There is no part2, the 50th star is the sun!") } - diff --git a/2020/day01/main.go b/2020/day01/main.go index 423fbad..0907907 100644 --- a/2020/day01/main.go +++ b/2020/day01/main.go @@ -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" ) @@ -61,7 +61,7 @@ func parseInputs(input string) []int { nums := []int{} for _, n := range split { - nums = append(nums, mathutil.StrToInt(n)) + nums = append(nums, cast.ToInt(n)) } return nums diff --git a/2020/day07/main.go b/2020/day07/main.go index f71d49f..884abeb 100644 --- a/2020/day07/main.go +++ b/2020/day07/main.go @@ -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" ) @@ -59,7 +59,7 @@ func parseInput(input string) map[string]map[string]int { continue } parts := strings.Split(content, " ") - graph[color][parts[1]+" "+parts[2]] = mathutil.StrToInt(parts[0]) + graph[color][parts[1]+" "+parts[2]] = cast.ToInt(parts[0]) } } return graph diff --git a/2020/day10/main.go b/2020/day10/main.go index 45cb4f5..a95e967 100644 --- a/2020/day10/main.go +++ b/2020/day10/main.go @@ -6,7 +6,8 @@ import ( "sort" "strings" - "github.com/alexchao26/advent-of-code-go/algo" + "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 +30,7 @@ func main() { func part1(input string) int { nums := parseInput(input) - nums = append(nums, mathutil.MaxInt(nums...)+3) + nums = append(nums, mathy.MaxInt(nums...)+3) sort.Ints(nums) var oneDiff, threeDiff int @@ -51,7 +52,7 @@ func part1(input string) int { func part2(input string) int { nums := parseInput(input) - nums = append(nums, mathutil.MaxInt(nums...)+3) + nums = append(nums, mathy.MaxInt(nums...)+3) sort.Ints(nums) // return dynamicProgramming(input) @@ -63,7 +64,7 @@ func parseInput(input string) []int { lines := strings.Split(input, "\n") for _, l := range lines { - ans = append(ans, mathutil.StrToInt(l)) + ans = append(ans, cast.ToInt(l)) } return ans @@ -100,16 +101,16 @@ func memoCountPossibilities(nums []int, lastJolt int) int { return count } func makeMemoKey(nums []int, lastJolt int) string { - ans := algo.IntToStr(lastJolt) + "x" + ans := cast.ToString(lastJolt) + "x" for _, v := range nums { - ans += algo.IntToStr(v) + ans += cast.ToString(v) } return ans } func dynamicProgramming(input string) int { nums := parseInput(input) - nums = append(nums, mathutil.MaxInt(nums...)+3, 0) + nums = append(nums, mathy.MaxInt(nums...)+3, 0) sort.Ints(nums) // initialize table with "1 way" to get to zero jolts diff --git a/2020/day12/main.go b/2020/day12/main.go index c396f7e..7aa0581 100644 --- a/2020/day12/main.go +++ b/2020/day12/main.go @@ -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" ) @@ -67,7 +68,7 @@ func part1(input string) int { } } - return mathutil.ManhattanDistance(0, 0, shipX, shipY) + return mathy.ManhattanDistance(0, 0, shipX, shipY) } func part2(input string) int { @@ -111,7 +112,7 @@ func part2(input string) int { } } - return mathutil.ManhattanDistance(0, 0, shipX, shipY) + return mathy.ManhattanDistance(0, 0, shipX, shipY) } type instruction struct { @@ -126,7 +127,7 @@ func parseInput(input string) []instruction { for _, l := range lines { inst := instruction{ action: l[:1], - value: mathutil.StrToInt(l[1:]), + value: cast.ToInt(l[1:]), } ans = append(ans, inst) } diff --git a/2020/day13/main.go b/2020/day13/main.go index c148a63..3259992 100644 --- a/2020/day13/main.go +++ b/2020/day13/main.go @@ -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" ) @@ -69,10 +69,10 @@ func part2(input string) int { // busses are [2]int{index, busID}, not the best way to parse stuff but it works func parseInput(input string) (estimate int, busses [][2]int) { lines := strings.Split(input, "\n") - estimate = mathutil.StrToInt(lines[0]) + estimate = cast.ToInt(lines[0]) for index, busID := range strings.Split(lines[1], ",") { if busID != "x" { - busses = append(busses, [2]int{index, mathutil.StrToInt(busID)}) + busses = append(busses, [2]int{index, cast.ToInt(busID)}) } } return estimate, busses diff --git a/2020/day15/main.go b/2020/day15/main.go index b3ffce7..8432a6d 100644 --- a/2020/day15/main.go +++ b/2020/day15/main.go @@ -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" ) @@ -28,7 +28,7 @@ func main() { func rambunctiousRecitation(input string, turnToReturn int) int { var startingNums []int for _, num := range strings.Split(input, ",") { - startingNums = append(startingNums, mathutil.StrToInt(num)) + startingNums = append(startingNums, cast.ToInt(num)) } said := map[int][]int{} diff --git a/2020/day16/main.go b/2020/day16/main.go index 3e6a747..2cff046 100644 --- a/2020/day16/main.go +++ b/2020/day16/main.go @@ -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" ) @@ -133,7 +132,7 @@ func parseInput(input string) (map[string][2][2]int, []int, [][]int) { splitTicket := strings.Split(blocks[1], "\n") var myTicket []int for _, v := range strings.Split(splitTicket[1], ",") { - myTicket = append(myTicket, mathutil.StrToInt(v)) + myTicket = append(myTicket, cast.ToInt(v)) } // all values for nearby tickets @@ -141,7 +140,7 @@ func parseInput(input string) (map[string][2][2]int, []int, [][]int) { for _, nearby := range strings.Split(blocks[2], "\n")[1:] { var near []int for _, v := range strings.Split(nearby, ",") { - near = append(near, mathutil.StrToInt(v)) + near = append(near, cast.ToInt(v)) } nearbyTickets = append(nearbyTickets, near) } diff --git a/2020/day18/main.go b/2020/day18/main.go index d301111..25369f4 100644 --- a/2020/day18/main.go +++ b/2020/day18/main.go @@ -6,7 +6,7 @@ import ( "regexp" "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" ) @@ -88,7 +88,7 @@ func doMaths(input []string, flatteningFunc func([]string) string) int { } // slice should now be flat - return mathutil.StrToInt(flatteningFunc(stackFlattened)) + return cast.ToInt(flatteningFunc(stackFlattened)) } func calcFlatSlicePart1(input []string) string { @@ -98,20 +98,20 @@ func calcFlatSlicePart1(input []string) string { } } - result := mathutil.StrToInt(input[0]) + result := cast.ToInt(input[0]) for i := range input { if i+2 < len(input) { switch input[i+1] { case "+": - result += mathutil.StrToInt(input[i+2]) + result += cast.ToInt(input[i+2]) case "*": - result *= mathutil.StrToInt(input[i+2]) + result *= cast.ToInt(input[i+2]) } } } - return mathutil.IntToStr(result) + return cast.ToString(result) } func calcFlatSlicePart2(input []string) string { @@ -159,16 +159,16 @@ func isNum(str string) bool { func addStrings(strs ...string) string { var sum int for _, str := range strs { - sum += mathutil.StrToInt(str) + sum += cast.ToInt(str) } - return mathutil.IntToStr(sum) + return cast.ToString(sum) } func multiplyStrings(strs ...string) string { sum := 1 for _, str := range strs { - sum *= mathutil.StrToInt(str) + sum *= cast.ToInt(str) } - return mathutil.IntToStr(sum) + return cast.ToString(sum) } // removes a particular number of elements from the middle of the slice diff --git a/2020/day19/main.go b/2020/day19/main.go index 950a404..59f7f3e 100644 --- a/2020/day19/main.go +++ b/2020/day19/main.go @@ -6,8 +6,7 @@ import ( "regexp" "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" ) @@ -140,13 +139,13 @@ func parseInput(input string) (rules map[int]*rule, messages []string) { rules[num] = &rule{resolved: []string{char}} } else { split := strings.Split(r, ": ") - key := mathutil.StrToInt(split[0]) + key := cast.ToInt(split[0]) newRule := rule{} for _, ruleNums := range strings.Split(split[1], " | ") { nums := strings.Split(ruleNums, " ") var option []int for _, n := range nums { - option = append(option, mathutil.StrToInt(n)) + option = append(option, cast.ToInt(n)) } newRule.options = append(newRule.options, option) } diff --git a/2020/day20/main.go b/2020/day20/main.go index 088dc76..a438911 100644 --- a/2020/day20/main.go +++ b/2020/day20/main.go @@ -7,7 +7,6 @@ import ( "strings" "github.com/alexchao26/advent-of-code-go/algos" - "github.com/alexchao26/advent-of-code-go/util" ) diff --git a/2020/day22/main.go b/2020/day22/main.go index e2041af..bd0ccd3 100644 --- a/2020/day22/main.go +++ b/2020/day22/main.go @@ -5,7 +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/cast" "github.com/alexchao26/advent-of-code-go/util" @@ -68,7 +68,7 @@ func recursiveGame(deck1, deck2 []int, isMainGame bool) (finalScore int, player1 // and player 1 can never lose that card, so at some point, a pattern will // repeat which leads to player 1 winning if !isMainGame { - max1, max2 := mathutil.MaxInt(deck1...), mathutil.MaxInt(deck2...) + max1, max2 := mathy.MaxInt(deck1...), mathy.MaxInt(deck2...) if max1 > max2 && max1 >= len(deck1)+len(deck2)-2 { return 0, true } diff --git a/2020/day24/main.go b/2020/day24/main.go index df1cfad..64ecd8e 100644 --- a/2020/day24/main.go +++ b/2020/day24/main.go @@ -5,7 +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" ) @@ -120,7 +120,7 @@ func zeroOutHexDirections(tally [6]int) [6]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 } @@ -132,7 +132,7 @@ func zeroOutHexDirections(tally [6]int) [6]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 diff --git a/algos/random-nums.go b/algos/random-nums.go new file mode 100644 index 0000000..696cc51 --- /dev/null +++ b/algos/random-nums.go @@ -0,0 +1,13 @@ +package algos + +import ( + "math/rand" + "time" +) + +var rn = rand.New(rand.NewSource(time.Now().UnixNano())) + +// RandomInt returns a random int from zero through upper - 1 +func RandomInt(upper int) int { + return rn.Intn(upper) +} diff --git a/mathutil/distances.go b/mathy/distances.go similarity index 95% rename from mathutil/distances.go rename to mathy/distances.go index 496d06f..820f242 100644 --- a/mathutil/distances.go +++ b/mathy/distances.go @@ -1,4 +1,4 @@ -package mathutil +package mathy import ( "math" diff --git a/mathutil/math.go b/mathy/math.go similarity index 62% rename from mathutil/math.go rename to mathy/math.go index 35eae22..863232a 100644 --- a/mathutil/math.go +++ b/mathy/math.go @@ -1,9 +1,4 @@ -package mathutil - -import ( - "fmt" - "strconv" -) +package mathy func MaxInt(nums ...int) int { maxNum := nums[0] @@ -24,18 +19,6 @@ func MinInt(nums ...int) int { 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) -} - func AbsInt(in int) int { if in < 0 { return -in diff --git a/mathutil/primes.go b/mathy/primes.go similarity index 97% rename from mathutil/primes.go rename to mathy/primes.go index 4ca225e..94bc499 100644 --- a/mathutil/primes.go +++ b/mathy/primes.go @@ -1,4 +1,4 @@ -package mathutil +package mathy import ( "math" diff --git a/mathutil/primes_test.go b/mathy/primes_test.go similarity index 98% rename from mathutil/primes_test.go rename to mathy/primes_test.go index 919159a..4531527 100644 --- a/mathutil/primes_test.go +++ b/mathy/primes_test.go @@ -1,4 +1,4 @@ -package mathutil +package mathy import ( "fmt" diff --git a/scripts/template/template.go b/scripts/template/template.go index a894743..36e1775 100644 --- a/scripts/template/template.go +++ b/scripts/template/template.go @@ -63,7 +63,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" ) @@ -98,7 +98,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 }