~xenrox/aoc2019

903ce3079502b2458e207316a98a823a7a256aa0 — Thorben Günther 4 years ago e87aa25
day4: solved
1 files changed, 38 insertions(+), 0 deletions(-)

A 4/main.go
A 4/main.go => 4/main.go +38 -0
@@ 0,0 1,38 @@
package main

import "fmt"

func calcCombinations(lower int, higher int) (int, int) {
	combinations := 0
	isolatedCombinations := 0
	for i := lower; i <= higher; i++ {
		arr := []byte(fmt.Sprintf("%06d", i))
		twoMatching := false
		isIncreasing := true
		twoIsolatedMatching := false
		for j := 0; j < len(arr)-1; j++ {
			if arr[j] == arr[j+1] {
				twoMatching = true
				if (j < 1 || arr[j-1] != arr[j]) && (j >= len(arr)-2 || arr[j+2] != arr[j]) {
					twoIsolatedMatching = true
				}
			}
			if arr[j] > arr[j+1] {
				isIncreasing = false
			}
		}
		if twoMatching == true && isIncreasing == true {
			combinations++
		}
		if twoIsolatedMatching == true && isIncreasing == true {
			isolatedCombinations++
		}
	}
	return combinations, isolatedCombinations
}

func main() {
	lower := 130254
	higher := 678275
	fmt.Println(calcCombinations(lower, higher))
}