Skip to content

Commit

Permalink
Add day3 part2 solution
Browse files Browse the repository at this point in the history
  • Loading branch information
MaskedSyntax committed Dec 3, 2024
1 parent fbc0c05 commit 57c7c62
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
60 changes: 60 additions & 0 deletions day-3/part2/part2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package part2

import (
"bufio"
"fmt"
"os"
"path/filepath"
"regexp"
"strconv"
)

func check(e error) {
if e != nil {
panic(e)
}
}

func getFileData() (*os.File, *bufio.Scanner) {
wd, err := os.Getwd()
check(err)

inputPath := filepath.Join(wd, "input.txt")

file, err := os.Open(inputPath)
check(err)

return file, bufio.NewScanner(file)
}

func Run() {

file, scanner := getFileData()
defer file.Close()

ans := 0
enabled := true
re := regexp.MustCompile(`mul\((\d+),(\d+)\)|(do\(\))|don't\(\)`)

for scanner.Scan() {
line := scanner.Text()
matches := re.FindAllStringSubmatch(line, -1)

for _, match := range matches {
if match[1] != "" && match[2] != "" {
x, _ := strconv.Atoi(match[1])
y, _ := strconv.Atoi(match[2])
if enabled {
ans += (x * y)
}
} else if match[0] == "do()" {
enabled = true
} else if match[0] == "don't()" {
enabled = false
}
}

}

fmt.Println(ans)
}
1 change: 1 addition & 0 deletions day-3/test2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))

0 comments on commit 57c7c62

Please sign in to comment.