2020-day18: balanced parens calculator... gross initial solution...

This commit is contained in:
alexchao26
2020-12-18 01:16:23 -05:00
parent bf9d5a97e7
commit a5e3c80111
2 changed files with 219 additions and 13 deletions
+162 -11
View File
@@ -3,9 +3,11 @@ package main
import (
"flag"
"fmt"
"regexp"
"strings"
"github.com/alexchao26/advent-of-code-go/mathutil"
"github.com/alexchao26/advent-of-code-go/util"
)
@@ -15,32 +17,181 @@ func main() {
flag.Parse()
fmt.Println("Running part", part)
var ans int
if part == 1 {
ans := part1(util.ReadFile("./input.txt"))
util.CopyToClipboard(fmt.Sprintf("%v", ans))
fmt.Println("Output:", ans)
ans = part1(util.ReadFile("./input.txt"))
} else {
ans := part2(util.ReadFile("./input.txt"))
util.CopyToClipboard(fmt.Sprintf("%v", ans))
fmt.Println("Output:", ans)
ans = part2(util.ReadFile("./input.txt"))
}
fmt.Println("Output:", ans)
}
// 392
func part1(input string) int {
parsed := parseInput(input)
_ = parsed
return 0
var total int
for _, line := range parsed {
// sum up this line, addressing things inside parens first...
total += doMaths(line)
}
return total
}
// ~1700 :(
func part2(input string) int {
return 0
lines := parseInput(input)
var total int
for _, line := range lines {
total += doMaths2(line)
}
return total
}
func parseInput(input string) (ans []int) {
// 370?
func parseInput(input string) (ans [][]string) {
lines := strings.Split(input, "\n")
for _, l := range lines {
ans = append(ans, mathutil.StrToInt(l))
ans = append(ans, strings.Split(strings.ReplaceAll(l, " ", ""), ""))
}
return ans
}
func doMaths(input []string) int {
var parensBalance int
var sum int
lastOperator := "+"
var nested []string
for _, val := range input {
switch val {
case "+", "*":
if parensBalance == 0 {
lastOperator = val
} else {
nested = append(nested, val)
}
case "(":
if parensBalance != 0 {
nested = append(nested, "(")
}
parensBalance++
case ")":
parensBalance--
if parensBalance == 0 {
if lastOperator == "+" {
sum += doMaths(nested)
} else {
sum *= doMaths(nested)
}
nested = []string{}
} else {
nested = append(nested, ")")
}
default:
if parensBalance == 0 {
if lastOperator == "+" {
sum += mathutil.StrToInt(val)
} else {
sum *= mathutil.StrToInt(val)
}
} else {
nested = append(nested, val)
}
}
}
return sum
}
var numReg = regexp.MustCompile("[0-9]")
func isNum(str string) bool {
return numReg.MatchString(str)
}
func doMaths2(input []string) int {
var stack []int
for i := 0; i < len(input); i++ {
// iterate throuhg input
// track open and close parens
switch val := input[i]; val {
case "(":
stack = append(stack, i)
case ")":
// on close parens, pass a section tohandleFlat
// then remove a bunch of shit from input,
openIndex := stack[len(stack)-1]
stack = stack[:len(stack)-1]
if input[openIndex] == "+" || input[openIndex] == "*" {
openIndex++
}
sliToFlatten := input[openIndex+1 : i] // will not include the closing paren
flatVal := handleFlat(sliToFlatten)
input[openIndex] = flatVal
copy(input[openIndex+1:], input[i+1:])
input = input[:len(input)-(i-openIndex)]
i = openIndex
default:
// continue along
}
}
return mathutil.StrToInt(handleFlat(input))
}
func handleFlat(input []string) string {
for _, v := range input {
if v == "(" || v == ")" {
panic("unexpected paren in flat input")
}
}
// handle all additions
for i := 1; i < len(input)-1; i++ {
if input[i] == "+" {
toLeft := input[i-1]
toRight := input[i+1]
if numReg.MatchString(toLeft) && numReg.MatchString(toRight) {
input[i-1] = mathutil.IntToStr(mathutil.StrToInt(toLeft) + mathutil.StrToInt(toRight))
// remove two things
for j := i; j < len(input)-2; j++ {
input[j] = input[j+2]
}
input = input[:len(input)-2]
i--
}
}
}
// then handle all multiplications
for i := 1; i < len(input)-1; i++ {
if input[i] == "*" {
toLeft := input[i-1]
toRight := input[i+1]
if numReg.MatchString(toLeft) && numReg.MatchString(toRight) {
input[i-1] = mathutil.IntToStr(mathutil.StrToInt(toLeft) * mathutil.StrToInt(toRight))
// remove two things
for j := i; j < len(input)-2; j++ {
input[j] = input[j+2]
}
input = input[:len(input)-2]
i--
}
}
}
return input[0]
}
// removes a particular number of elements from the middle of the slice
func splice(sli []string, startIndex, items int) []string {
copy(sli[startIndex:], sli[startIndex+items:])
sli = sli[:len(sli)-items]
return sli
}
+57 -2
View File
@@ -1,7 +1,10 @@
package main
import (
"reflect"
"testing"
"github.com/alexchao26/advent-of-code-go/util"
)
func Test_part1(t *testing.T) {
@@ -10,7 +13,9 @@ func Test_part1(t *testing.T) {
input string
want int
}{
// {"actual", util.ReadFile("input.txt"), ACTUAL_ANSWER},
// {"example1", "2 * 3 + (4 * 5)", 26},
{"example2", "((2 + 4 * 9) * (6 + 9 * 8 + 6) + 6) + 2 + 4 * 2", 13632},
{"actual", util.ReadFile("input.txt"), 53660285675207},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
@@ -27,7 +32,7 @@ func Test_part2(t *testing.T) {
input string
want int
}{
// {"actual", util.ReadFile("input.txt"), ACTUAL_ANSWER},
{"actual", util.ReadFile("input.txt"), 141993988282687},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
@@ -37,3 +42,53 @@ func Test_part2(t *testing.T) {
})
}
}
func Test_handleFlat(t *testing.T) {
type args struct {
input []string
}
tests := []struct {
name string
args args
want string
}{
{"example", args{
input: []string{"1", "+", "3", "*", "4", "+", "5"},
},
"36",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := handleFlat(tt.args.input); !reflect.DeepEqual(got, tt.want) {
t.Errorf("handleFlat() = %v, want %v", got, tt.want)
}
})
}
}
func Test_splice(t *testing.T) {
type args struct {
sli []string
startIndex int
items int
}
tests := []struct {
name string
args args
want []string
}{
{"example1", args{[]string{"a", "b", "c", "d", "e"}, 1, 1}, []string{"a", "c", "d", "e"}},
{"example1", args{[]string{"a", "b", "c", "d", "e"}, 1, 4}, []string{"a"}},
{"example1", args{[]string{"a", "b", "c", "d", "e"}, 0, 1}, []string{"b", "c", "d", "e"}},
{"example1", args{[]string{"a", "b", "c", "d", "e"}, 0, 5}, []string{}},
{"example1", args{[]string{"a", "b", "c", "d", "e"}, 3, 1}, []string{"a", "b", "c", "e"}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := splice(tt.args.sli, tt.args.startIndex, tt.args.items); !reflect.DeepEqual(got, tt.want) {
t.Errorf("splice() = %v, want %v", got, tt.want)
}
})
}
}