This commit is contained in:
Stefan Harmuth 2021-02-01 08:17:56 +01:00
parent 202647d6f0
commit beecd2c76a
4 changed files with 75 additions and 1 deletions

64
day09/day.go Normal file
View File

@ -0,0 +1,64 @@
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)
}

1
inputs/9 Normal file

File diff suppressed because one or more lines are too long

8
inputs/9_test Normal file
View File

@ -0,0 +1,8 @@
ADVENT
A(1x5)BC
(3x3)XYZ
A(2x2)BCD(2x2)EFG
(6x1)(1x3)A
X(8x2)(3x3)ABCY
(27x12)(20x12)(13x14)(7x10)(1x12)A
(25x3)(3x3)ABC(2x3)XY(5x2)PQRSTX(18x9)(3x2)TWO(5x7)SEVEN

View File

@ -9,6 +9,7 @@ import (
"aoc2016/day06"
"aoc2016/day07"
"aoc2016/day08"
"aoc2016/day09"
"flag"
"fmt"
"os"
@ -34,7 +35,7 @@ func initDayFunctions() {
6: {1: day06.Part1, 2: day06.Part2},
7: {1: day07.Part1, 2: day07.Part2},
8: {1: day08.Part1, 2: day08.Part2},
// 9: {1: day09.Part1, 2: day09.Part2},
9: {1: day09.Part1, 2: day09.Part2},
// 10: {1: day10.Part1, 2: day10.Part2},
// 11: {1: day11.Part1, 2: day11.Part2},
// 12: {1: day12.Part1, 2: day12.Part2},