small pixel drawing of a pufferfish aoc

2024/day1/puzzle1.go

package main

import (
	"bufio"
	"fmt"
	"os"
	"slices"
	"strconv"
	"strings"
)

func main() {
	var left []int
	var right []int

	file, _ := os.Open("input1.txt")
	defer file.Close()

	scanner := bufio.NewScanner(file)
	for scanner.Scan() {
		line := scanner.Text()
		fields := strings.Split(line, "   ")

		l, _ := strconv.Atoi(fields[0])
		left = append(left, l)
		r, _ := strconv.Atoi(fields[1])
		right = append(right, r)

		slices.Sort(left)
		slices.Sort(right)
	}

	var total int
	for i := range left {
		d := difference(left[i], right[i])
		total += d
	}
	fmt.Println(total)
}

// calc difference between 2 inties
func difference(int1 int, int2 int) int {
	s := []int{int1, int2}
	slices.Sort(s)
	return s[1] - s[0]
}