-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
141 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
input: | ||
http "https://adventofcode.com/2024/day/19/input" "Cookie:session=${AOC_SESSION};" >input | ||
|
||
main1: | ||
go build -o main1 main1.go common.go | ||
|
||
main2: | ||
go build -o main2 main2.go common.go | ||
|
||
.PHONY: run1 run2 clean | ||
|
||
run1: main1 input | ||
./main1 <input | ||
|
||
run2: main2 input | ||
./main2 <input | ||
|
||
clean: | ||
rm -f main1 main2 input | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"os" | ||
"strings" | ||
) | ||
|
||
func parseInput() ([]string, []string) { | ||
scanner := bufio.NewScanner(os.Stdin) | ||
scanner.Scan() | ||
patterns := strings.Split(scanner.Text(), ", ") | ||
scanner.Scan() | ||
designs := make([]string, 0) | ||
for scanner.Scan() { | ||
designs = append(designs, scanner.Text()) | ||
} | ||
return patterns, designs | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
func main() { | ||
patterns, designs := parseInput() | ||
|
||
cache := map[string]bool{} | ||
|
||
sum := 0 | ||
for _, d := range designs { | ||
if possible(d, patterns, cache) { | ||
sum++ | ||
} | ||
} | ||
fmt.Println(sum) | ||
} | ||
|
||
func possible(d string, patterns []string, cache map[string]bool) bool { | ||
if len(d) == 0 { | ||
return true | ||
} | ||
|
||
if t, ok := cache[d]; ok { | ||
return t | ||
} | ||
|
||
for _, p := range patterns { | ||
if strings.HasPrefix(d, p) { | ||
found := possible(d[len(p):], patterns, cache) | ||
if found { | ||
cache[d] = true | ||
return true | ||
} | ||
} | ||
} | ||
|
||
cache[d] = false | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
func main() { | ||
patterns, designs := parseInput() | ||
|
||
rgxp := regexp.MustCompile(fmt.Sprintf("^(%s)+$", strings.Join(patterns, "|"))) | ||
|
||
sum := 0 | ||
for _, d := range designs { | ||
if rgxp.MatchString(d) { | ||
sum++ | ||
} | ||
} | ||
fmt.Println(sum) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
func main() { | ||
patterns, designs := parseInput() | ||
|
||
cache := map[string]int{} | ||
|
||
sum := 0 | ||
for _, d := range designs { | ||
sum += ways(d, patterns, cache) | ||
} | ||
fmt.Println(sum) | ||
} | ||
|
||
func ways(d string, patterns []string, cache map[string]int) int { | ||
if len(d) == 0 { | ||
return 1 | ||
} | ||
|
||
if w, ok := cache[d]; ok { | ||
return w | ||
} | ||
|
||
w := 0 | ||
for _, p := range patterns { | ||
if strings.HasPrefix(d, p) { | ||
w += ways(d[len(p):], patterns, cache) | ||
} | ||
} | ||
|
||
cache[d] = w | ||
return w | ||
} |