Skip to content
This repository was archived by the owner on Dec 28, 2024. It is now read-only.

Commit f07cbd9

Browse files
committed
Add solution day 19 part 1
1 parent 9ab0697 commit f07cbd9

File tree

3 files changed

+61
-2
lines changed

3 files changed

+61
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ automatically rebuilt and redeployed every time the `common` module or their own
5151
| 03 | ⭐ ⭐ | 16 | ⭐ 🥸 |
5252
| 04 | ⭐ ⭐ | 17 | ⭐ ⭐ |
5353
| 05 | ⭐ ⭐ | 18 | ⭐ ⭐ |
54-
| 06 | ⭐ ⭐ | 19 | |
54+
| 06 | ⭐ ⭐ | 19 | |
5555
| 07 | ⭐ ⭐ | 20 | |
5656
| 08 | ⭐ ⭐ | 21 | |
5757
| 09 | ⭐ ⭐ | 22 | |

solutions/day19/main.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,33 @@
11
package main
22

33
import (
4+
"fmt"
45
"github.com/terminalnode/adventofcode2024/common"
6+
"regexp"
7+
"strings"
58
)
69

710
func main() {
8-
common.Setup(19, nil, nil)
11+
common.Setup(19, part1, nil)
12+
}
13+
14+
func part1(
15+
input string,
16+
) string {
17+
p, err := parse(input)
18+
if err != nil {
19+
return fmt.Sprintf("Failed to parse input: %v", err)
20+
}
21+
22+
join := strings.Join(p.available, "|")
23+
r := regexp.MustCompile(fmt.Sprintf("^(%s)+$", join))
24+
25+
count := 0
26+
for _, desired := range p.desired {
27+
if r.MatchString(desired) {
28+
count++
29+
}
30+
}
31+
32+
return fmt.Sprintf("%d of the %d desired designs are possible", count, len(p.desired))
933
}

solutions/day19/parse.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
)
7+
8+
type parsedInput struct {
9+
available []string
10+
desired []string
11+
}
12+
13+
func parse(
14+
input string,
15+
) (parsedInput, error) {
16+
split := strings.Split(input, "\n\n")
17+
if len(split) != 2 {
18+
return parsedInput{}, fmt.Errorf("expected two parts in input, got %d", len(split))
19+
}
20+
21+
available := strings.Split(split[0], ", ")
22+
if len(available) == 0 {
23+
return parsedInput{}, fmt.Errorf("no available patterns found")
24+
}
25+
26+
desired := strings.Split(split[1], "\n")
27+
if len(desired) == 0 {
28+
return parsedInput{}, fmt.Errorf("no desired patterns found")
29+
}
30+
31+
return parsedInput{
32+
available: available,
33+
desired: desired,
34+
}, nil
35+
}

0 commit comments

Comments
 (0)