Skip to content

Commit

Permalink
Day 12
Browse files Browse the repository at this point in the history
  • Loading branch information
dmytro-spivakov committed Oct 22, 2024
1 parent 76f601f commit 0238e7b
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ A small practice project created to get familiar with Go syntax.
- [x] day 9
- [x] day 10
- [x] day 11
- [ ] day 12
- [x] day 12
- [ ] day 13
- [ ] day 14
- [ ] day 15
Expand Down
51 changes: 37 additions & 14 deletions day12/day12.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,41 @@ import (
)

type Row struct {
rawSeq []string
damagedGroups []int
seq []string
dmgGroups []int
}

var calcCache = make(map[string]int)

func Solution1(filepath string) int {
rows := parseInput(filepath)

result := 0
for _, r := range rows {
result += countCombs(r.rawSeq, r.damagedGroups)
result += countCombs(r.seq, r.dmgGroups)
}
return result
}

func Solution2(filepath string) int {
rows := parseInput(filepath)

result := 0
for _, r := range rows {
// x5 unfold
newSeq := make([]string, 0, len(r.seq)*5+4)
newDmgGroups := make([]int, 0, len(r.dmgGroups)*5)
for i := 0; i < 5; i++ {
newSeq = append(newSeq, r.seq...)
if i < 4 {
newSeq = append(newSeq, "?")
}
newDmgGroups = append(newDmgGroups, r.dmgGroups...)
}

result += countCombs(newSeq, newDmgGroups)
}

return result
}

Expand All @@ -45,6 +69,12 @@ func countCombs(s []string, g []int) int {
}
}

// try cache
key := cacheKey(s, g)
if cachedRes, ok := calcCache[key]; ok {
return cachedRes
}

// ? eq . or # branching:
// ? eq .
result := 0
Expand All @@ -70,11 +100,12 @@ func countCombs(s []string, g []int) int {

}

calcCache[key] = result
return result
}

func Solution2(filepath string) int {
return -1
func cacheKey(s []string, n []int) string {
return fmt.Sprintf("%v;%v", s, n)
}

func parseInput(filepath string) []Row {
Expand All @@ -89,7 +120,7 @@ func parseInput(filepath string) []Row {
divInput := strings.Split(scanner.Text(), " ")
seq := strings.Split(divInput[0], "")
dmgGroups := strings.Split(divInput[1], ",")
m = append(m, Row{rawSeq: seq, damagedGroups: parseInts(dmgGroups)})
m = append(m, Row{seq: seq, dmgGroups: parseInts(dmgGroups)})
}
if err = scanner.Err(); err != nil {
log.Fatalf("Error during input file read: %v\n", err.Error())
Expand All @@ -98,14 +129,6 @@ func parseInput(filepath string) []Row {
return m
}

func printRows(rows []Row) {
fmt.Println("-----MATRIX START-----")
for i, row := range rows {
fmt.Printf("Row %d: %v | %v\n", i, strings.Join(row.rawSeq, ""), row.damagedGroups)
}
fmt.Println("------MATRIX END-----")
}

func parseInts(sNums []string) []int {
var nums []int
for _, sNum := range sNums {
Expand Down
16 changes: 16 additions & 0 deletions day12/day12_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,19 @@ func TestSolutio1(t *testing.T) {
}
}
}

func TestSolutio2(t *testing.T) {
cases := map[string]int{
"test_input11": 525152,
}

for input, expectedResult := range cases {
result := Solution2(input)

if result == expectedResult {
fmt.Printf("Solution2()=%d, OK\n", result)
} else {
t.Fatalf("Solution2()=%d, expecting %d, FAIL\n", result, expectedResult)
}
}
}

0 comments on commit 0238e7b

Please sign in to comment.