✨ Finish task for part one and two for day 4
This commit is contained in:
parent
7d66e8b910
commit
c7ce85ed63
108
day4/main.go
Normal file
108
day4/main.go
Normal file
@ -0,0 +1,108 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"math"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func main() {
|
||||
countPasswords := CountPossiblePasswords(245318, 765747, false)
|
||||
countAdditionalPasswords := CountPossiblePasswords(245318, 765747, true)
|
||||
|
||||
log.Println("Part1: " + strconv.Itoa(countPasswords))
|
||||
log.Println("Part2: " + strconv.Itoa(countAdditionalPasswords))
|
||||
}
|
||||
|
||||
func CountPossiblePasswords(start int, end int, additional bool) int {
|
||||
sum := 0
|
||||
for i := start; i < end; i++ {
|
||||
if MeetPasswordCriteria(i, additional) {
|
||||
sum++
|
||||
}
|
||||
}
|
||||
return sum
|
||||
}
|
||||
|
||||
func MeetPasswordCriteria(number int, additional bool) bool {
|
||||
|
||||
if CountDigits(number) != 6 {
|
||||
return false
|
||||
}
|
||||
|
||||
if !AreDigitsNotDecreasing(number) {
|
||||
return false
|
||||
}
|
||||
|
||||
if !HaveDoubleInputs(number) {
|
||||
return false
|
||||
}
|
||||
|
||||
if additional && !HasOneDouble(number) {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func CountDigits(i int) int {
|
||||
count := 0
|
||||
for i != 0 {
|
||||
i /= 10
|
||||
count = count + 1
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
func AreDigitsNotDecreasing(i int) bool {
|
||||
iStr := strconv.Itoa(i)
|
||||
prevInt := math.MinInt32
|
||||
|
||||
for _, a := range iStr {
|
||||
aInt, _ := strconv.Atoi(string(a))
|
||||
|
||||
if prevInt > aInt {
|
||||
return false
|
||||
}
|
||||
prevInt = aInt
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func HaveDoubleInputs(i int) bool {
|
||||
iStr := strconv.Itoa(i)
|
||||
hasDouble := false
|
||||
for i := 0; i < len(iStr)-1; i++ {
|
||||
if iStr[i] == iStr[i+1] {
|
||||
hasDouble = true
|
||||
}
|
||||
}
|
||||
return hasDouble
|
||||
}
|
||||
|
||||
func HasOneDouble(i int) bool {
|
||||
iStr := strconv.Itoa(i)
|
||||
|
||||
for len(iStr) >= 2 {
|
||||
if iStr[0] != iStr[1] {
|
||||
iStr = iStr[1:]
|
||||
continue
|
||||
}
|
||||
|
||||
i := 1
|
||||
for len(iStr) > i && iStr[0] == iStr[i] {
|
||||
i++
|
||||
}
|
||||
|
||||
if i == 2 {
|
||||
return true
|
||||
}
|
||||
|
||||
if len(iStr) <= i {
|
||||
return false
|
||||
}
|
||||
|
||||
iStr = iStr[i:]
|
||||
}
|
||||
return false
|
||||
}
|
156
day4/main_test.go
Normal file
156
day4/main_test.go
Normal file
@ -0,0 +1,156 @@
|
||||
package main
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestCountDigitsPositiveNumber(t *testing.T) {
|
||||
count := CountDigits(237841)
|
||||
solution := 6
|
||||
|
||||
if count != solution {
|
||||
t.Errorf("DigitCount was incorrect, got: {%d}, want: {%d}.", count, solution)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCountDigitsNegativeNumber(t *testing.T) {
|
||||
count := CountDigits(-2378541)
|
||||
solution := 7
|
||||
|
||||
if count != solution {
|
||||
t.Errorf("DigitCount was incorrect, got: {%d}, want: {%d}.", count, solution)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAreDigitsNotDecreasingSuccess(t *testing.T) {
|
||||
success := AreDigitsNotDecreasing(1134569)
|
||||
solution := true
|
||||
|
||||
if success != solution {
|
||||
t.Errorf("AreDigitsNotDecreasing was incorrect, got: {%t}, want: {%t}.", success, solution)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAreDigitsNotDecreasingFailure(t *testing.T) {
|
||||
success := AreDigitsNotDecreasing(12246378)
|
||||
solution := false
|
||||
|
||||
if success != solution {
|
||||
t.Errorf("AreDigitsNotDecreasing was incorrect, got: {%t}, want: {%t}.", success, solution)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHaveDoubleInputsSuccess(t *testing.T) {
|
||||
success := HaveDoubleInputs(123445)
|
||||
solution := true
|
||||
|
||||
if success != solution {
|
||||
t.Errorf("HaveDoubleInputs was incorrect, got: {%t}, want: {%t}.", success, solution)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHaveDoubleInputsFailure(t *testing.T) {
|
||||
success := HaveDoubleInputs(12345)
|
||||
solution := false
|
||||
|
||||
if success != solution {
|
||||
t.Errorf("HaveDoubleInputs was incorrect, got: {%t}, want: {%t}.", success, solution)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHaveDoubleInputsSuccessAdditional(t *testing.T) {
|
||||
success := HasOneDouble(114444)
|
||||
solution := true
|
||||
|
||||
if success != solution {
|
||||
t.Errorf("HaveDoubleInputs was incorrect, got: {%t}, want: {%t}.", success, solution)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHaveDoubleInputsSuccessAdditional2(t *testing.T) {
|
||||
success := HasOneDouble(111444)
|
||||
solution := false
|
||||
|
||||
if success != solution {
|
||||
t.Errorf("HaveDoubleInputs was incorrect, got: {%t}, want: {%t}.", success, solution)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHaveDoubleInputsSuccessAdditional3(t *testing.T) {
|
||||
success := HasOneDouble(111114)
|
||||
solution := false
|
||||
|
||||
if success != solution {
|
||||
t.Errorf("HaveDoubleInputs was incorrect, got: {%t}, want: {%t}.", success, solution)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHaveDoubleInputsSuccessAdditional4(t *testing.T) {
|
||||
success := HasOneDouble(111111)
|
||||
solution := false
|
||||
|
||||
if success != solution {
|
||||
t.Errorf("HaveDoubleInputs was incorrect, got: {%t}, want: {%t}.", success, solution)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHaveDoubleInputsFailureAdditional(t *testing.T) {
|
||||
success := HasOneDouble(1234555)
|
||||
solution := false
|
||||
|
||||
if success != solution {
|
||||
t.Errorf("HaveDoubleInputs was incorrect, got: {%t}, want: {%t}.", success, solution)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckPasswordCriteriaExample1(t *testing.T) {
|
||||
success := MeetPasswordCriteria(111111, false)
|
||||
solution := true
|
||||
|
||||
if success != solution {
|
||||
t.Errorf("PasswordCriteria was incorrect, got: {%t}, want: {%t}.", success, solution)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckPasswordCriteriaExample2(t *testing.T) {
|
||||
success := MeetPasswordCriteria(223450, false)
|
||||
solution := false
|
||||
|
||||
if success != solution {
|
||||
t.Errorf("PasswordCriteria was incorrect, got: {%t}, want: {%t}.", success, solution)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckPasswordCriteriaExample3(t *testing.T) {
|
||||
success := MeetPasswordCriteria(123789, false)
|
||||
solution := false
|
||||
|
||||
if success != solution {
|
||||
t.Errorf("PasswordCriteria was incorrect, got: {%t}, want: {%t}.", success, solution)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckPasswordAdditionalCriteriaExample1(t *testing.T) {
|
||||
success := MeetPasswordCriteria(112233, true)
|
||||
solution := true
|
||||
|
||||
if success != solution {
|
||||
t.Errorf("PasswordCriteria was incorrect, got: {%t}, want: {%t}.", success, solution)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckPasswordAdditionalCriteriaExample2(t *testing.T) {
|
||||
success := MeetPasswordCriteria(123444, true)
|
||||
solution := false
|
||||
|
||||
if success != solution {
|
||||
t.Errorf("PasswordCriteria was incorrect, got: {%t}, want: {%t}.", success, solution)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckPasswordAdditionalCriteriaExample3(t *testing.T) {
|
||||
success := MeetPasswordCriteria(111122, true)
|
||||
solution := true
|
||||
|
||||
if success != solution {
|
||||
t.Errorf("PasswordCriteria was incorrect, got: {%t}, want: {%t}.", success, solution)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user