Skip to content

Commit

Permalink
chore: preparing for advent of code 2024
Browse files Browse the repository at this point in the history
  • Loading branch information
believer committed Nov 28, 2024
1 parent 631a479 commit c90453d
Show file tree
Hide file tree
Showing 10 changed files with 183 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
npm-debug.log
node_modules
rust/**/target

input.txt
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ This is challenging for me, but I also really really like it. I don't do these t

| Year | Language | Main solution | Link |
| ---- | ---------------- | ------------- | ------------------------ |
| 2024 | Go | X | [Link](/go/2024) |
| 2023 | Rust | X | [Link](/rust/2023) |
| 2023 | Go | | [Link](/go/2023) |
| 2023 | TypeScript (Bun) | | [Link](/typescript/2023) |
Expand Down
10 changes: 10 additions & 0 deletions go/2024/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
PADDED_DAY = $(shell printf "%02d" $(DAY))

create-day:
@cp -r ./template puzzles/day$(PADDED_DAY)

test:
@go test ./puzzles/day$(PADDED_DAY)

run:
@go run ./puzzles/day$(PADDED_DAY)/main.go
12 changes: 12 additions & 0 deletions go/2024/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Advent of Code 2024

I've been enjoying Go this year and thought I might learn more by doing AoC in it this year!

## Advent of Code 2024 Story

TBD

## Days

| Day | #1 | #1 Answer | #2 | #2 Answer |
| --- | --- | --------: | --- | --------: |
11 changes: 11 additions & 0 deletions go/2024/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module github.com/believer/aoc-2024

go 1.23.3

require github.com/stretchr/testify v1.10.0

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
10 changes: 10 additions & 0 deletions go/2024/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
18 changes: 18 additions & 0 deletions go/2024/template/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package main

import (
"fmt"
)

func main() {
fmt.Println("Part 1: ", part1("input.txt"))
fmt.Println("Part 2: ", part2("input.txt"))
}

func part1(name string) int {
return 0
}

func part2(name string) int {
return 0
}
23 changes: 23 additions & 0 deletions go/2024/template/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package main

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestPart1(t *testing.T) {
t.Run("Part 1", func(t *testing.T) {
expected := 0
actual := part1("test-input.txt")
assert.Equal(t, expected, actual)
})
}

func TestPart2(t *testing.T) {
t.Run("Part 2", func(t *testing.T) {
expected := 0
actual := part2("test-input.txt")
assert.Equal(t, expected, actual)
})
}
Empty file added go/2024/template/test-input.txt
Empty file.
97 changes: 97 additions & 0 deletions go/2024/utils/files/files.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package files

import (
"bufio"
"os"
"path"
"runtime"
)

// Borrowed from https://github.com/kylehoehns/aoc-2023-go/blob/main/pkg/files/filereader.go
// Seems like a great starting point for me to learn and adjust according to my needs.

// ReadLines reads a file and returns a slice of strings, one for each line
func ReadLines(name string) []string {
_, callingFile, _, ok := runtime.Caller(1)

if !ok {
panic("unable to find caller so cannot build path to read file")
}

return readLines(name, callingFile)
}

// Read reads a file and returns a string containing the entire file
func Read(name string) string {
_, callingFile, _, ok := runtime.Caller(1)

if !ok {
panic("unable to find caller so cannot build path to read file")
}

b, err := os.ReadFile(path.Join(path.Dir(callingFile), name))

if err != nil {
panic(err)
}

return string(b)
}

// ReadParagraphs reads a file and returns a slice of slices of strings, one for each line
// A gap of one or more blank lines is used to split the file into groups
func ReadParagraphs(name string) [][]string {
_, callingFile, _, ok := runtime.Caller(1)

if !ok {
panic("unable to find caller so cannot build path to read file")
}

lines := readLines(name, callingFile)

var groups [][]string

curGroup := make([]string, 0)

for _, line := range lines {
if line == "" {
groups = append(groups, curGroup)
curGroup = make([]string, 0)
} else {
curGroup = append(curGroup, line)
}
}

if len(curGroup) > 0 {
groups = append(groups, curGroup)
}

return groups
}

func readLines(name string, callingFile string) []string {
inputFile, err := os.Open(path.Join(path.Dir(callingFile), name))

if err != nil {
panic(err)
}

defer func(inputFile *os.File) {
err := inputFile.Close()

if err != nil {
panic(err)
}
}(inputFile)

scanner := bufio.NewScanner(inputFile)
scanner.Split(bufio.ScanLines)

var lines []string

for scanner.Scan() {
lines = append(lines, scanner.Text())
}

return lines
}

0 comments on commit c90453d

Please sign in to comment.