From e0b4f292c2d8f55b01fcd80764c3871646753048 Mon Sep 17 00:00:00 2001 From: Alan R Evans Date: Tue, 3 Dec 2024 07:19:28 +0000 Subject: [PATCH] day3 --- 2024/go/day01/day01.go | 2 +- 2024/go/day03/day03.go | 64 +++++++++++++++++++++++++++++++++++++ 2024/go/day03/day03_test.go | 17 ++++++++++ 2024/go/main.go | 6 +++- 4 files changed, 87 insertions(+), 2 deletions(-) create mode 100644 2024/go/day03/day03.go create mode 100644 2024/go/day03/day03_test.go diff --git a/2024/go/day01/day01.go b/2024/go/day01/day01.go index 8d706d0..233392c 100644 --- a/2024/go/day01/day01.go +++ b/2024/go/day01/day01.go @@ -35,7 +35,7 @@ func Part2(input string) int { list2 := make(map[int]int) - num := 0 + num := 1 lines := strings.Split(input, "\n") for _, line := range lines { fields := strings.Fields(line) diff --git a/2024/go/day03/day03.go b/2024/go/day03/day03.go new file mode 100644 index 0000000..a92e610 --- /dev/null +++ b/2024/go/day03/day03.go @@ -0,0 +1,64 @@ +package day03 + +import ( + "adventofcode2024/utils" + "fmt" + "regexp" +) + +func Part1(input string) int { + val := 0 + + pattern := `mul\((\d{1,3}),(\d{1,3})\)` + re := regexp.MustCompile(pattern) + matches := re.FindAllStringSubmatch(input, -1) + + fmt.Println("Matches found:") + for _, match := range matches { + if len(match) == 3 { // match[1] is x, match[2] is y + x := utils.MustAtoi(match[1]) + y := utils.MustAtoi(match[2]) + val += x * y + } + } + return val +} + +func Part2(input string) int { + val := 0 + pattern := `mul\((\d{1,3}),(\d{1,3})\)` + re := regexp.MustCompile(pattern) + matches := re.FindAllStringSubmatchIndex(input, -1) + + fmt.Println("Matches found:") + for _, match := range matches { + if len(match) == 6 { + xStart, xEnd := match[2], match[3] + yStart, yEnd := match[4], match[5] + x := utils.MustAtoi(input[xStart:xEnd]) + y := utils.MustAtoi(input[yStart:yEnd]) + if do(input[:xStart]) { + val += x * y + } + } + } + return val +} + +func do(input string) bool { + pattern := `don't\(\)|do\(\)` + re := regexp.MustCompile(pattern) + + matches := re.FindAllString(input, -1) + + if len(matches) > 0 { + // Get the last match + lastMatch := matches[len(matches)-1] + if lastMatch == "don't()" { + return false + } else if lastMatch == "do()" { + return true + } + } + return true +} diff --git a/2024/go/day03/day03_test.go b/2024/go/day03/day03_test.go new file mode 100644 index 0000000..f0ecf4a --- /dev/null +++ b/2024/go/day03/day03_test.go @@ -0,0 +1,17 @@ +package day03 + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestPart1(t *testing.T) { + r := Part1("") + require.Equal(t, 0, r) +} + +func TestPart2(t *testing.T) { + r := Part2("xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))") + require.Equal(t, 48, r) +} diff --git a/2024/go/main.go b/2024/go/main.go index 96a06c6..3c8c20d 100644 --- a/2024/go/main.go +++ b/2024/go/main.go @@ -9,6 +9,7 @@ import ( "adventofcode2024/utils" "adventofcode2024/day01" "adventofcode2024/day02" + "adventofcode2024/day03" ) // Usage: go run main.go // assumes input is in day/input.txt @@ -23,6 +24,9 @@ func main() { case 2: fmt.Printf("part 1: %d\n", day02.Part1(utils.Readfile(d))) fmt.Printf("part 2: %d\n", day02.Part2(utils.Readfile(d))) + case 3: + fmt.Printf("part 1: %d\n", day03.Part1(utils.Readfile(d))) + fmt.Printf("part 2: %d\n", day03.Part2(utils.Readfile(d))) default: panic(fmt.Errorf("no such day: %d", d)) } @@ -30,7 +34,7 @@ func main() { // Reads day from os.Args. func day() int { - latest := 1 + latest := 2 if len(os.Args) == 1 { return latest }