Day 1 and 2 done

This commit is contained in:
Melody Becker 2024-12-02 14:42:47 +01:00
commit d9d6149564
9 changed files with 2247 additions and 0 deletions

1001
day1/input Normal file

File diff suppressed because it is too large Load diff

60
day1/main.go Normal file
View file

@ -0,0 +1,60 @@
package main
import (
"fmt"
"slices"
"strconv"
"strings"
"git.mstar.dev/mstar/aoc24/util"
"git.mstar.dev/mstar/goutils/sliceutils"
)
func LineExtract(line string) (int, int) {
tmp := sliceutils.Filter(strings.Split(line, " "), func(s string) bool {
return len(s) > 0
})
if len(tmp) != 2 {
return 1, 1
}
left, err := strconv.Atoi(tmp[0])
if err != nil {
panic(err)
}
right, err := strconv.Atoi(tmp[1])
if err != nil {
panic(err)
}
return left, right
}
func main() {
data := util.LoadFileFromArgs()
lines := sliceutils.Filter(
strings.Split(string(data), "\n"),
func(t string) bool { return len(t) > 0 },
)
numbersLeft := []int{}
numberRight := []int{}
for _, line := range lines {
l, r := LineExtract(line)
numbersLeft = append(numbersLeft, l)
numberRight = append(numberRight, r)
}
slices.Sort(numberRight)
slices.Sort(numbersLeft)
diffs := make([]int, len(lines))
for i := range len(lines) {
diffs = append(diffs, util.AbsI(numbersLeft[i]-numberRight[i]))
}
total := sliceutils.Compact(diffs, func(acc, next int) int { return acc + next })
fmt.Printf("Task 1: %d\n", total)
acc := 0
for _, v := range numbersLeft {
acc += len(sliceutils.Filter(numberRight, func(t int) bool { return t == v })) * v
}
fmt.Printf("Task 2: %d\n", acc)
}

6
day1/sampleTask1 Normal file
View file

@ -0,0 +1,6 @@
3 4
4 3
2 5
1 3
3 9
3 3

1000
day2/input Normal file

File diff suppressed because it is too large Load diff

124
day2/main.go Normal file
View file

@ -0,0 +1,124 @@
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))
})
}
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
}
*/

6
day2/sample Normal file
View file

@ -0,0 +1,6 @@
7 6 4 2 1
1 2 7 8 9
9 7 6 2 1
1 3 2 4 5
8 6 4 4 1
1 3 6 7 9

7
go.mod Normal file
View file

@ -0,0 +1,7 @@
module git.mstar.dev/mstar/aoc24
go 1.23.3
require git.mstar.dev/mstar/goutils v1.5.4
require github.com/BooleanCat/go-functional/v2 v2.3.0 // indirect

8
go.sum Normal file
View file

@ -0,0 +1,8 @@
git.mstar.dev/mstar/goutils v1.5.3 h1:Wqa8H8EWnc9IJmzsi6qeVqwYHPvWiDhG/b9WtZr0Nos=
git.mstar.dev/mstar/goutils v1.5.3/go.mod h1:juxY0eZEMnA95fedRp2LVXvUBgEjz66nE8SEdGKcxMA=
git.mstar.dev/mstar/goutils v1.5.4 h1:l/4oQe/fBk9zyXplQkGXbmQndnm0aRdHuy4wgQfNrFo=
git.mstar.dev/mstar/goutils v1.5.4/go.mod h1:juxY0eZEMnA95fedRp2LVXvUBgEjz66nE8SEdGKcxMA=
github.com/BooleanCat/go-functional/v2 v2.3.0 h1:mwVrUa4MSKQNs/N4EPy0fadRLyzP+eoc0NbnzWHEzLM=
github.com/BooleanCat/go-functional/v2 v2.3.0/go.mod h1:IpUUAXAc9CiWDb+YDXkJyyUhtOVqDtyICDRg/de1IaQ=
gitlab.com/mstarongitlab/goutils v1.5.1 h1:UlQL90DctJ7QbfJ5NOPheMK3PdFt6sBIjXR3fJpd3Ms=
gitlab.com/mstarongitlab/goutils v1.5.1/go.mod h1:f71xLeTv05GHiRHKkgDRXfxOPRkjXNteXqLZyg02xhs=

35
util/loadFile.go Normal file
View file

@ -0,0 +1,35 @@
package util
import (
"flag"
"os"
"strings"
"git.mstar.dev/mstar/goutils/sliceutils"
)
func LoadFileFromArgs() []byte {
if !flag.Parsed() {
flag.Parse()
}
data, err := os.ReadFile(flag.CommandLine.Arg(0))
if err != nil {
panic(err)
}
return data
}
func AbsI(a int) int {
if a > 0 {
return a
} else {
return -a
}
}
func FileContentToNonEmptyLines(data []byte) []string {
return sliceutils.Filter(
strings.Split(string(data), "\n"),
func(t string) bool { return len(t) > 0 },
)
}