65 lines
1.3 KiB
Go
65 lines
1.3 KiB
Go
package day09
|
|
|
|
import (
|
|
"strconv"
|
|
"strings"
|
|
"tools"
|
|
)
|
|
|
|
func decompress(msg string, recurse bool) (decompLen int) {
|
|
index := 0
|
|
marker := ""
|
|
markMode := false
|
|
for index < len(msg) {
|
|
switch msg[index] {
|
|
case '(':
|
|
marker = ""
|
|
markMode = true
|
|
case ')':
|
|
if markMode {
|
|
markMode = false
|
|
parts := strings.Split(marker, "x")
|
|
iCount, _ := strconv.Atoi(parts[0])
|
|
amount, _ := strconv.Atoi(parts[1])
|
|
count := iCount
|
|
if recurse {
|
|
count = decompress(msg[index+1:index+count+1], true)
|
|
}
|
|
index += iCount
|
|
decompLen += count * amount
|
|
}
|
|
default:
|
|
if markMode {
|
|
marker += string(msg[index])
|
|
} else {
|
|
decompLen++
|
|
}
|
|
}
|
|
index++
|
|
}
|
|
|
|
return decompLen
|
|
}
|
|
|
|
func Part1(puzzle tools.AoCPuzzle) interface{} {
|
|
/* Just for -test, because that has multiple lines instead of only one
|
|
var decompLen int
|
|
for _, msg := range puzzle.GetInputArray() {
|
|
decompLen = decompress(msg, false)
|
|
fmt.Println(msg, "=>", decompLen)
|
|
}
|
|
*/
|
|
return decompress(puzzle.GetInputArray()[0], false)
|
|
}
|
|
|
|
func Part2(puzzle tools.AoCPuzzle) interface{} {
|
|
/* Just for -test, because that has multiple lines instead of only one
|
|
var decompLen int
|
|
for _, msg := range puzzle.GetInputArray() {
|
|
decompLen = decompress(msg, true)
|
|
fmt.Println(msg, "=>", decompLen)
|
|
}
|
|
*/
|
|
return decompress(puzzle.GetInputArray()[0], true)
|
|
}
|