Compare commits

...

3 Commits

Author SHA1 Message Date
82290bad36 Day 4 part 1 2024-02-27 09:06:01 -05:00
91ce1fc9f3 Day 3 part 2 2024-02-25 16:43:55 -05:00
bcce0722fb Day 3 part 1 2024-02-25 16:37:21 -05:00
3 changed files with 94 additions and 3 deletions

12
main.go
View File

@ -10,8 +10,14 @@ func main() {
runAndBenchmark("01", "1", false, solutions.Day01Part01) runAndBenchmark("01", "1", false, solutions.Day01Part01)
runAndBenchmark("01", "2", false, solutions.Day01Part02) runAndBenchmark("01", "2", false, solutions.Day01Part02)
runAndBenchmark("02", "1", false, solutions.Day01Part01) runAndBenchmark("02", "1", false, solutions.Day02Part01)
runAndBenchmark("02", "2", false, solutions.Day01Part02) runAndBenchmark("02", "2", false, solutions.Day02Part02)
runAndBenchmark("03", "1", false, solutions.Day03Part01)
runAndBenchmark("03", "2", false, solutions.Day03Part02)
runAndBenchmark("04", "1", false, solutions.Day04Part01)
runAndBenchmark("04", "2", false, solutions.Day04Part02)
} }
type execute func(bool) int type execute func(bool) int
@ -21,5 +27,5 @@ func runAndBenchmark(day, part string, isTest bool, fn execute) {
computation := fn(isTest) computation := fn(isTest)
endMs := time.Now().UnixMicro() endMs := time.Now().UnixMicro()
duration := endMs - startMs duration := endMs - startMs
fmt.Printf("Day %s part %s:\t%10d\t%5d micros.\n", day, part, computation, duration) fmt.Printf("Day %s part %s:\t%12d\t%5d micros.\n", day, part, computation, duration)
} }

44
solutions/day03.go Normal file
View File

@ -0,0 +1,44 @@
package solutions
import "strings"
func countTrees(input string, right, down int) int {
verticalPos := down
horizontalPos := right
lines := strings.Split(input, "\n")
width := len(lines[0])
treesCount := 0
for verticalPos < len(lines) {
line := lines[verticalPos]
charAt := string(line[horizontalPos])
if charAt == "#" {
treesCount += 1
}
horizontalPos += right
horizontalPos = horizontalPos % width
verticalPos += down
}
return treesCount
}
func Day03Part01(isTest bool) int {
input := ReadInput("03", isTest)
return countTrees(input, 3, 1)
}
func Day03Part02(isTest bool) int {
input := ReadInput("03", isTest)
amount1 := countTrees(input, 1, 1)
amount2 := countTrees(input, 3, 1)
amount3 := countTrees(input, 5, 1)
amount4 := countTrees(input, 7, 1)
amount5 := countTrees(input, 1, 2)
return amount1 * amount2 * amount3 * amount4 * amount5
}

41
solutions/day04.go Normal file
View File

@ -0,0 +1,41 @@
package solutions
import (
"regexp"
"strings"
)
func Day04Part01(isTest bool) int {
input := ReadInput("04", isTest)
passports := strings.Split(input, "\n\n")
regex, _ := regexp.Compile("(\\w+:[\\w#]+)")
correctAmount := 0
for _, passport := range passports {
matches := regex.FindAllString(passport, -1)
values := make(map[string]string)
for _, match := range matches {
colonPosition := strings.Index(match, ":")
key := match[:colonPosition]
value := match[colonPosition+1:]
values[key] = value
}
_, cidMissing := values["cid"]
keysAmount := len(values)
if keysAmount == 8 || (keysAmount == 7 && !cidMissing) {
correctAmount += 1
}
}
return correctAmount
}
func Day04Part02(isTest bool) int {
return -1
}