Advent of Code 2022 Day 4: Camp Cleanup
By Andreas Røssland
https://adventofcode.com/2022/day/04 https://github.com/roessland/advent-of-code
Today’s problem was to compute number interval intersections and containment. Time spent: 00:14:20 for part 1, and 00:21:52 for both.
Solution for both parts
Slightly cleaned up from the initial solution. Initially I sorted [a, b, c, d]
in part 1, but then I optimized it by avoiding the explicit sort. Part 2 is unchanged.
package main
import (
"bufio"
"embed"
_ "embed"
"fmt"
"github.com/roessland/advent-of-code/2022/aocutil"
"github.com/roessland/gopkg/mathutil"
"log"
)
//go:embed input.txt input-ex.txt
var inputFiles embed.FS
func main() {
pairs := ReadInput()
part1(pairs)
part2(pairs)
}
func part1(pairs []Pair) {
count := 0
for _, p := range pairs {
a, b, c, d := p.A, p.B, p.C, p.D
if a > c {
a, b, c, d = c, d, a, b
}
if a == c || d <= b {
count++
}
}
fmt.Println(count)
}
func part2(pairs []Pair) {
count := 0
for _, p := range pairs {
m := mathutil.MaxInt(p.A, p.C)
M := mathutil.MinInt(p.B, p.D)
if m <= M {
count++
}
}
fmt.Println(count)
}
type Pair struct {
A, B, C, D int
}
func ReadInput() []Pair {
f, err := inputFiles.Open("input.txt")
if err != nil {
log.Fatal(err)
}
var pairs []Pair
scanner := bufio.NewScanner(f)
for scanner.Scan() {
line := scanner.Text()
p := aocutil.GetIntsInString(line)
pairs = append(pairs, Pair{
p[0], p[1], p[2], p[3],
})
}
return pairs
}