Skip to content

Commit

Permalink
solve day 3
Browse files Browse the repository at this point in the history
  • Loading branch information
SylivanKenobi committed Dec 3, 2024
1 parent b7f2127 commit eee2d9f
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 12 deletions.
3 changes: 0 additions & 3 deletions 2024/day1/day1.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package day1

import (
"adventofcode/utils"
"fmt"
"math"
"sort"
"strconv"
Expand All @@ -28,7 +27,6 @@ func Part1(file string) int {
dist := leftList[i] - rightList[i]
totalDist = int(math.Abs(float64(dist))) + totalDist
}
fmt.Println(totalDist)
return totalDist
}

Expand All @@ -54,6 +52,5 @@ func Part2(file string) int {
}
similarity += (left * matches)
}
fmt.Println(similarity)
return similarity
}
3 changes: 0 additions & 3 deletions 2024/day2/day2.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package day2

import (
"adventofcode/utils"
"fmt"
"math"
"strconv"
"strings"
Expand All @@ -22,7 +21,6 @@ func Part1(file string) int {
validLine += 1
}
}
fmt.Println(validLine)
return validLine
}

Expand All @@ -46,7 +44,6 @@ func Part2(file string) int {
validLine += 1
}
}
fmt.Println(validLine)
return validLine
}

Expand Down
62 changes: 62 additions & 0 deletions 2024/day3/day3.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package day3

import (
"adventofcode/utils"
"fmt"
"regexp"
"strconv"
"strings"
)

func Part1(file string) int {
lines := utils.ReadFileByLine(file)
content := strings.Join(lines[:], ":")

re_mul := regexp.MustCompile(`mul\([0-9]+,[0-9]+\)`)
multiplications := re_mul.FindAllString(content, -1)

re_num := regexp.MustCompile(`[0-9]+`)
result := 0
for _, mul := range multiplications {
numbers := re_num.FindAllString(mul, -1)
num1, _ := strconv.Atoi(numbers[0])
num2, _ := strconv.Atoi(numbers[1])
result += (num1 * num2)
}
fmt.Println(result)
return result
}

func Part2(file string) int {
lines := utils.ReadFileByLine(file)
content := strings.Join(lines[:], ":")

re_mul := regexp.MustCompile(`mul\([0-9]+,[0-9]+\)|do\(\)|don\'t\(\)`)
inputs := re_mul.FindAllString(content, -1)

do := true
var multiplications []string
for _, input := range inputs {
if input == "don't()" {
do = false
continue
} else if input == "do()" {
do = true
continue
}

if do {
multiplications = append(multiplications, input)
}
}

re_num := regexp.MustCompile(`[0-9]+`)
result := 0
for _, mul := range multiplications {
numbers := re_num.FindAllString(mul, -1)
num1, _ := strconv.Atoi(numbers[0])
num2, _ := strconv.Atoi(numbers[1])
result += (num1 * num2)
}
return result
}
Loading

0 comments on commit eee2d9f

Please sign in to comment.