Commit 0a347de 1 parent c61d436 commit 0a347de Copy full SHA for 0a347de
File tree 5 files changed +141
-0
lines changed
5 files changed +141
-0
lines changed Original file line number Diff line number Diff line change
1
+ input :
2
+ http " https://adventofcode.com/2024/day/19/input" " Cookie:session=${AOC_SESSION} ;" > input
3
+
4
+ main1 :
5
+ go build -o main1 main1.go common.go
6
+
7
+ main2 :
8
+ go build -o main2 main2.go common.go
9
+
10
+ .PHONY : run1 run2 clean
11
+
12
+ run1 : main1 input
13
+ ./main1 < input
14
+
15
+ run2 : main2 input
16
+ ./main2 < input
17
+
18
+ clean :
19
+ rm -f main1 main2 input
20
+
Original file line number Diff line number Diff line change
1
+ package main
2
+
3
+ import (
4
+ "bufio"
5
+ "os"
6
+ "strings"
7
+ )
8
+
9
+ func parseInput () ([]string , []string ) {
10
+ scanner := bufio .NewScanner (os .Stdin )
11
+ scanner .Scan ()
12
+ patterns := strings .Split (scanner .Text (), ", " )
13
+ scanner .Scan ()
14
+ designs := make ([]string , 0 )
15
+ for scanner .Scan () {
16
+ designs = append (designs , scanner .Text ())
17
+ }
18
+ return patterns , designs
19
+ }
Original file line number Diff line number Diff line change
1
+ package main
2
+
3
+ import (
4
+ "fmt"
5
+ "strings"
6
+ )
7
+
8
+ func main () {
9
+ patterns , designs := parseInput ()
10
+
11
+ cache := map [string ]bool {}
12
+
13
+ sum := 0
14
+ for _ , d := range designs {
15
+ if possible (d , patterns , cache ) {
16
+ sum ++
17
+ }
18
+ }
19
+ fmt .Println (sum )
20
+ }
21
+
22
+ func possible (d string , patterns []string , cache map [string ]bool ) bool {
23
+ if len (d ) == 0 {
24
+ return true
25
+ }
26
+
27
+ if t , ok := cache [d ]; ok {
28
+ return t
29
+ }
30
+
31
+ for _ , p := range patterns {
32
+ if strings .HasPrefix (d , p ) {
33
+ found := possible (d [len (p ):], patterns , cache )
34
+ if found {
35
+ cache [d ] = true
36
+ return true
37
+ }
38
+ }
39
+ }
40
+
41
+ cache [d ] = false
42
+ return false
43
+ }
Original file line number Diff line number Diff line change
1
+ package main
2
+
3
+ import (
4
+ "fmt"
5
+ "regexp"
6
+ "strings"
7
+ )
8
+
9
+ func main () {
10
+ patterns , designs := parseInput ()
11
+
12
+ rgxp := regexp .MustCompile (fmt .Sprintf ("^(%s)+$" , strings .Join (patterns , "|" )))
13
+
14
+ sum := 0
15
+ for _ , d := range designs {
16
+ if rgxp .MatchString (d ) {
17
+ sum ++
18
+ }
19
+ }
20
+ fmt .Println (sum )
21
+ }
Original file line number Diff line number Diff line change
1
+ package main
2
+
3
+ import (
4
+ "fmt"
5
+ "strings"
6
+ )
7
+
8
+ func main () {
9
+ patterns , designs := parseInput ()
10
+
11
+ cache := map [string ]int {}
12
+
13
+ sum := 0
14
+ for _ , d := range designs {
15
+ sum += ways (d , patterns , cache )
16
+ }
17
+ fmt .Println (sum )
18
+ }
19
+
20
+ func ways (d string , patterns []string , cache map [string ]int ) int {
21
+ if len (d ) == 0 {
22
+ return 1
23
+ }
24
+
25
+ if w , ok := cache [d ]; ok {
26
+ return w
27
+ }
28
+
29
+ w := 0
30
+ for _ , p := range patterns {
31
+ if strings .HasPrefix (d , p ) {
32
+ w += ways (d [len (p ):], patterns , cache )
33
+ }
34
+ }
35
+
36
+ cache [d ] = w
37
+ return w
38
+ }
You can’t perform that action at this time.
0 commit comments