package main import ( "bufio" "fmt" "os" "strconv" ) func main() { // iterate over all IDs joltages := Decode("input.txt") var joltagessum int = 0 for _, i := range joltages { var tensColumn int = 0 var onesColumn int = 0 for iteration, x := range i { num, err := strconv.Atoi(string(x)) if err != nil { panic(err) } if tensColumn == 0 { tensColumn = num } else if num > tensColumn && iteration != len(i)-1 { tensColumn = num onesColumn = 0 } else if num > onesColumn && onesColumn >= tensColumn { tensColumn = onesColumn onesColumn = num } else if num > onesColumn { onesColumn = num } } sum := tensColumn*10 + onesColumn fmt.Printf("joltage found for %s, %d%d %d\n", i, tensColumn, onesColumn, sum) joltagessum = joltagessum + sum } fmt.Println("total joltage sum", joltagessum) } func Decode(filename string) []string { f, err := os.Open(filename) if err != nil { panic(err) } defer f.Close() scanner := bufio.NewScanner(f) var joltages []string for scanner.Scan() { // extract joltage numbers into struct to go over joltages = append(joltages, scanner.Text()) } return joltages }