aoc24/day2/main.go

127 lines
3 KiB
Go
Raw Permalink Normal View History

2024-12-02 13:42:47 +00:00
package main
import (
"fmt"
"strconv"
"strings"
"git.mstar.dev/mstar/aoc24/util"
"git.mstar.dev/mstar/goutils/maputils"
"git.mstar.dev/mstar/goutils/other"
"git.mstar.dev/mstar/goutils/sliceutils"
)
func parseLine(line string) []int {
strElems := sliceutils.Filter(
strings.Split(line, " "),
func(t string) bool { return len(t) > 0 },
)
return sliceutils.Map(strElems, func(t string) int {
return other.Must(strconv.Atoi(t))
})
}
2024-12-03 11:13:28 +00:00
// This is ugly. I'm sure there is a way to use some cool filter for when to exclude something
// or not. But I'm not smart enough for that
2024-12-02 13:42:47 +00:00
func generateCombinations(in []int) [][]int {
variations := [][]int{in}
for pos := range len(in) {
tmp := []int{}
for i, v := range in {
if i == pos {
continue
}
tmp = append(tmp, v)
}
variations = append(variations, tmp)
}
return variations
}
func isSafe(in []int) bool {
isIncrementing := in[0] < in[1]
lastVal := in[0]
for _, elem := range in[1:] {
if util.AbsI(elem-lastVal) > 3 {
return false
}
if elem-lastVal == 0 {
return false
}
if isIncrementing && elem < lastVal || !isIncrementing && elem > lastVal {
return false
}
lastVal = elem
}
return true
}
func main() {
lines := util.FileContentToNonEmptyLines(util.LoadFileFromArgs())
safeLines1 := map[int]bool{}
for i, line := range lines {
elems := parseLine(line)
safeLines1[i] = isSafe(elems)
}
filteredTask1 := maputils.FilterMap(safeLines1, func(k int, v bool) bool {
return v
})
fmt.Printf("Task 1: %d\n", len(filteredTask1))
safeLines2 := map[int]bool{}
for i, line := range lines {
elems := parseLine(line)
variations := generateCombinations(elems)
for _, variation := range variations {
if isSafe(variation) {
safeLines2[i] = true
}
}
}
filteredTask2 := maputils.FilterMap(safeLines2, func(k int, v bool) bool {
return v
})
fmt.Printf("Task 2: %d\n", len(filteredTask2))
}
/* for _, elem := range elems[1:] {
if util.AbsI(elem-lastVal) > 3 {
// fmt.Printf("Line %d failed max diff with a value of %d\n", i, util.AbsI(elem-lastVal))
if !firstDiscordDone {
// fmt.Printf("Discard for line %d used up\n", i)
firstDiscordDone = true
continue
}
fmt.Printf("Line %d failed max diff with a value of %d\n", i, util.AbsI(elem-lastVal))
log.Printf("%d: %s\n", i, line)
continue lineloop2
}
if elem-lastVal == 0 {
// fmt.Printf("Line %d failed no equal vals\n", i)
if !firstDiscordDone {
// fmt.Printf("Discard for line %d used up\n", i)
firstDiscordDone = true
continue
}
log.Printf("%d: %s\n", i, line)
fmt.Printf("Line %d failed no equal vals\n", i)
continue lineloop2
}
if isInc && elem < lastVal || !isInc && elem > lastVal {
// fmt.Printf("Line %d failed same directionm\n", i)
if !firstDiscordDone {
// fmt.Printf("Discard for line %d used up\n", i)
firstDiscordDone = true
continue
}
log.Printf("%d: %s\n", i, line)
fmt.Printf("Line %d failed same directionm\n", i)
continue lineloop2
}
lastVal = elem
}
*/