diff --git a/day4/main.go b/day4/main.go new file mode 100644 index 0000000..a491165 --- /dev/null +++ b/day4/main.go @@ -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 +} diff --git a/day4/main_test.go b/day4/main_test.go new file mode 100644 index 0000000..88503f1 --- /dev/null +++ b/day4/main_test.go @@ -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) + } +}