2015-day25: thank goodness this wasn't modular arithmetic

This commit is contained in:
alexchao26
2020-12-28 00:06:30 -05:00
parent f57ebe956e
commit 077378f12c
2 changed files with 46 additions and 46 deletions
+39 -24
View File
@@ -3,6 +3,7 @@ package main
import ( import (
"flag" "flag"
"fmt" "fmt"
"regexp"
"strings" "strings"
"github.com/alexchao26/advent-of-code-go/cast" "github.com/alexchao26/advent-of-code-go/cast"
@@ -15,32 +16,46 @@ func main() {
flag.Parse() flag.Parse()
fmt.Println("Running part", part) fmt.Println("Running part", part)
if part == 1 { ans := letItSnow(util.ReadFile("./input.txt"))
ans := part1(util.ReadFile("./input.txt"))
util.CopyToClipboard(fmt.Sprintf("%v", ans))
fmt.Println("Output:", ans) fmt.Println("Output:", ans)
}
func letItSnow(input string) int {
var row, col int
input = strings.ReplaceAll(input, ",", "")
input = strings.ReplaceAll(input, ".", "")
for _, part := range strings.Split(input, " ") {
if regexp.MustCompile("[0-9]").MatchString(part) {
if row == 0 { // jeez i am getting lazy
row = cast.ToInt(part)
} else { } else {
ans := part2(util.ReadFile("./input.txt")) col = cast.ToInt(part)
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 }
// the number of iterations to run can be calculated by:
// - finding the number of cells that in the triangle that is formed by each
// diagonal prior to the incomplete one that the target cell is on
// This triangle is generated from adding 1+2+3+4...+(row+col) 0-indexed
// - then the number of iterations for the incomplete diagonal, is equal to
// the current column number (1-indexed)
var triangleBefore int
for i := 1; i <= row+col-2; i++ {
triangleBefore += i
}
numberOnThisDiagonal := col
// subtract one for starting cell
iterations := triangleBefore + numberOnThisDiagonal - 1
// and thankfully this runs quickly
code := 20151125
for i := 0; i < iterations; i++ {
code *= 252533
code %= 33554393
}
return code
} }
+6 -21
View File
@@ -2,37 +2,22 @@ package main
import ( import (
"testing" "testing"
"github.com/alexchao26/advent-of-code-go/util"
) )
func Test_part1(t *testing.T) { func Test_letItSnow(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
input string input string
want int want int
}{ }{
// {"actual", util.ReadFile("input.txt"), ACTUAL_ANSWER}, {"actual", util.ReadFile("input.txt"), 19980801},
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
if got := part1(tt.input); got != tt.want { if got := letItSnow(tt.input); got != tt.want {
t.Errorf("part1() = %v, want %v", got, tt.want) t.Errorf("letItSnow() = %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)
} }
}) })
} }