Compare commits
No commits in common. "93c256d989dc084357ddec77af763de50c8517fd" and "872f7e629d7eac3039d1238facca926449864765" have entirely different histories.
93c256d989
...
872f7e629d
2
.gitignore
vendored
2
.gitignore
vendored
@ -1,3 +1,3 @@
|
||||
.idea
|
||||
inputs
|
||||
inputs_test
|
||||
test_inputs
|
||||
|
18
main.go
18
main.go
@ -3,23 +3,9 @@ package main
|
||||
import (
|
||||
"advent-20/solutions"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
type execute func(bool) int
|
||||
|
||||
func runAndBenchmark(day, part string, isTest bool, fn execute) {
|
||||
startMs := time.Now().UnixMicro()
|
||||
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.Println("Day 01 part 1:\t", solutions.Day01Part01(false))
|
||||
fmt.Println("Day 01 part 2:\t", solutions.Day01Part02(false))
|
||||
}
|
||||
|
@ -45,7 +45,7 @@ func Day01Part02(isTest bool) int {
|
||||
for j := i + 1; j < arrLen; j += 1 {
|
||||
for k := j + 1; k < arrLen; k += 1 {
|
||||
if values[i]+values[j]+values[k] == 2020 {
|
||||
return values[i] * values[j] * values[k]
|
||||
return (values[i] * values[j] * values[k])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,93 +0,0 @@
|
||||
package solutions
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func passwordPolicy1(input string) bool {
|
||||
dashPosition := strings.Index(input, "-")
|
||||
spacePosition := strings.Index(input, " ")
|
||||
colonPosition := strings.Index(input, ":")
|
||||
|
||||
amountMin, err := strconv.Atoi(input[:dashPosition])
|
||||
if err != nil {
|
||||
panic("Error converting to int")
|
||||
}
|
||||
amountMax, err := strconv.Atoi(input[dashPosition+1 : spacePosition])
|
||||
if err != nil {
|
||||
panic("Error converting to int")
|
||||
}
|
||||
|
||||
letter := input[spacePosition+1 : colonPosition]
|
||||
rest := input[colonPosition+2:]
|
||||
|
||||
charMap := make(map[string]int)
|
||||
|
||||
for _, char := range strings.Split(rest, "") {
|
||||
if _, ok := charMap[char]; ok {
|
||||
charMap[char] += 1
|
||||
} else {
|
||||
charMap[char] = 1
|
||||
}
|
||||
}
|
||||
|
||||
charAmount := charMap[letter]
|
||||
|
||||
if charAmount >= amountMin && charAmount <= amountMax {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func Day02Part01(isTest bool) int {
|
||||
input := ReadInput("02", isTest)
|
||||
values := strings.Split(input, "\n")
|
||||
correctPasswords := 0
|
||||
|
||||
for _, i := range values {
|
||||
if passwordPolicy1(i) {
|
||||
correctPasswords += 1
|
||||
}
|
||||
}
|
||||
|
||||
return correctPasswords
|
||||
}
|
||||
|
||||
func passwordPolicy2(input string) bool {
|
||||
dashPosition := strings.Index(input, "-")
|
||||
spacePosition := strings.Index(input, " ")
|
||||
colonPosition := strings.Index(input, ":")
|
||||
firstIdx, err := strconv.Atoi(input[:dashPosition])
|
||||
if err != nil {
|
||||
panic("Error converting to int")
|
||||
}
|
||||
firstIdx -= 1
|
||||
secondIdx, err := strconv.Atoi(input[dashPosition+1 : spacePosition])
|
||||
if err != nil {
|
||||
panic("Error converting to int")
|
||||
}
|
||||
secondIdx -= 1
|
||||
letter := input[spacePosition+1 : colonPosition]
|
||||
rest := input[colonPosition+2:]
|
||||
|
||||
firstIdxIsTrue := string(rest[firstIdx]) == letter
|
||||
secondIdxIsTrue := string(rest[secondIdx]) == letter
|
||||
|
||||
return (firstIdxIsTrue && !secondIdxIsTrue) || (!firstIdxIsTrue && secondIdxIsTrue)
|
||||
}
|
||||
|
||||
func Day02Part02(isTest bool) int {
|
||||
input := ReadInput("02", isTest)
|
||||
values := strings.Split(input, "\n")
|
||||
correctPasswords := 0
|
||||
|
||||
for _, i := range values {
|
||||
if passwordPolicy2(i) {
|
||||
correctPasswords += 1
|
||||
}
|
||||
}
|
||||
|
||||
return correctPasswords
|
||||
}
|
@ -10,10 +10,10 @@ const DIR = "/home/fernando/GolandProjects/advent-20/"
|
||||
func ReadInput(day string, isTest bool) string {
|
||||
testStr := ""
|
||||
if isTest {
|
||||
testStr = "_test"
|
||||
testStr = "test_"
|
||||
}
|
||||
|
||||
bytes, err := os.ReadFile(DIR + "inputs" + testStr + "/" + day + ".txt")
|
||||
bytes, err := os.ReadFile(DIR + testStr + "inputs/" + day + ".txt")
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
panic("Error reading file.")
|
||||
|
Loading…
Reference in New Issue
Block a user