package main import ( "fmt" "slices" "strconv" "strings" helpers "git.bullercodeworks.com/brian/adventofcode/helpers" ) func main() { inp := helpers.StdinToStringSlice() part1(inp) part2(inp) } func buildLists(inp []string) ([]int, []int) { var list1, list2 []int for i := range inp { var pt string for j := range inp[i] { if inp[i][j] != ' ' { pt = pt + string(inp[i][j]) } else if strings.TrimSpace(pt) != "" { wrk, err := strconv.Atoi(pt) if err != nil { panic(err) } list1 = append(list1, wrk) pt = "" } } // End of line wrk, err := strconv.Atoi(pt) if err != nil { panic(err) } list2 = append(list2, wrk) pt = "" } return list1, list2 } func part1(inp []string) { var diffs []int list1, list2 := buildLists(inp) slices.Sort(list1) slices.Sort(list2) for i := 0; i < len(list1); i++ { diffs = append(diffs, (list1[i] - list2[i])) } var total int for i := range diffs { if diffs[i] < 0 { total = total - diffs[i] } else { total = total + diffs[i] } } fmt.Println("# Part 1") fmt.Println("Total Distance:", total) } func part2(inp []string) { var score int occ := make(map[int]int) list1, list2 := buildLists(inp) for i := range list2 { occ[list2[i]]++ } for i := range list1 { score = score + (list1[i] * occ[list1[i]]) } fmt.Println("# Part 2") fmt.Println("Similarity Score:", score) }