Compare commits
4 Commits
872f7e629d
...
93c256d989
Author | SHA1 | Date | |
---|---|---|---|
93c256d989 | |||
a2cb6ff664 | |||
1d9b8196d9 | |||
c3f708cda2 |
2
.gitignore
vendored
2
.gitignore
vendored
@ -1,3 +1,3 @@
|
||||
.idea
|
||||
inputs
|
||||
test_inputs
|
||||
inputs_test
|
||||
|
18
main.go
18
main.go
@ -3,9 +3,23 @@ package main
|
||||
import (
|
||||
"advent-20/solutions"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Println("Day 01 part 1:\t", solutions.Day01Part01(false))
|
||||
fmt.Println("Day 01 part 2:\t", solutions.Day01Part02(false))
|
||||
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)
|
||||
}
|
||||
|
@ -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]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
93
solutions/day02.go
Normal file
93
solutions/day02.go
Normal file
@ -0,0 +1,93 @@
|
||||
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 + testStr + "inputs/" + day + ".txt")
|
||||
bytes, err := os.ReadFile(DIR + "inputs" + testStr + "/" + day + ".txt")
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
panic("Error reading file.")
|
||||
|
Loading…
Reference in New Issue
Block a user