Compare commits
3 Commits
93c256d989
...
82290bad36
Author | SHA1 | Date | |
---|---|---|---|
82290bad36 | |||
91ce1fc9f3 | |||
bcce0722fb |
12
main.go
12
main.go
@ -10,8 +10,14 @@ func main() {
|
||||
runAndBenchmark("01", "1", false, solutions.Day01Part01)
|
||||
runAndBenchmark("01", "2", false, solutions.Day01Part02)
|
||||
|
||||
runAndBenchmark("02", "1", false, solutions.Day01Part01)
|
||||
runAndBenchmark("02", "2", false, solutions.Day01Part02)
|
||||
runAndBenchmark("02", "1", false, solutions.Day02Part01)
|
||||
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
|
||||
@ -21,5 +27,5 @@ func runAndBenchmark(day, part string, isTest bool, fn execute) {
|
||||
computation := fn(isTest)
|
||||
endMs := time.Now().UnixMicro()
|
||||
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
44
solutions/day03.go
Normal 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
41
solutions/day04.go
Normal 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
|
||||
}
|
Loading…
Reference in New Issue
Block a user