Skip to content

Commit 0900e7e

Browse files
committed
[2024] Solution for Day 1
1 parent 966caf8 commit 0900e7e

File tree

15 files changed

+443
-1
lines changed

15 files changed

+443
-1
lines changed

.github/workflows/2024.yml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
name: Advent of Code 2024
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
paths:
8+
- .github/workflows/2024.yml
9+
- 2024/**
10+
pull_request:
11+
paths:
12+
- .github/workflows/2024.yml
13+
- 2024/**
14+
15+
jobs:
16+
build-2024:
17+
runs-on: ubuntu-latest
18+
defaults:
19+
run:
20+
working-directory: "2024"
21+
22+
steps:
23+
- uses: actions/checkout@v3
24+
with:
25+
token: ${{ secrets.PAT }}
26+
submodules: true
27+
28+
- uses: actions/setup-go@v2
29+
with:
30+
go-version: ^1.19
31+
32+
- name: Get dependencies
33+
run: go get -v -t -d ./...
34+
35+
- name: Build
36+
run: go build -v ./...
37+
38+
- name: Test
39+
run: go test -v ./...

2024/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Advent of Code 2024
2+
3+
https://adventofcode.com/2024
4+
5+
## Language
6+
7+
Implemented in [Go](https://golang.org/).
8+
9+
## Preparation
10+
11+
Install [Go](https://golang.org/doc/install) for your platform.
12+
13+
## Running tests
14+
15+
Run tests with `go test`:
16+
17+
```console
18+
go test ./...
19+
```

2024/day01/main.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"os"
7+
"strings"
8+
9+
"github.com/kfarnung/advent-of-code/2020/lib"
10+
)
11+
12+
func part1(input string) int64 {
13+
first, second, err := parseInput(input)
14+
if err != nil {
15+
log.Fatal(err)
16+
}
17+
18+
lib.SortSliceInt64(first)
19+
lib.SortSliceInt64(second)
20+
21+
sum := int64(0)
22+
for i := 0; i < len(first); i++ {
23+
sum += lib.AbsInt64(first[i] - second[i])
24+
}
25+
26+
return sum
27+
}
28+
29+
func part2(input string) int64 {
30+
first, second, err := parseInput(input)
31+
if err != nil {
32+
log.Fatal(err)
33+
}
34+
35+
sum := int64(0)
36+
for _, value := range first {
37+
count := int64(0)
38+
for _, otherValue := range second {
39+
if value == otherValue {
40+
count++
41+
}
42+
}
43+
44+
sum += value * count
45+
}
46+
47+
return sum
48+
}
49+
50+
func parseInput(input string) ([]int64, []int64, error) {
51+
var first []int64
52+
var second []int64
53+
lines := lib.SplitLines(input)
54+
for _, line := range lines {
55+
if (len(line)) == 0 {
56+
continue
57+
}
58+
59+
splitLine := strings.Split(line, " ")
60+
firstValue, err := lib.ParseInt64(splitLine[0])
61+
if err != nil {
62+
return nil, nil, err
63+
}
64+
65+
secondValue, err := lib.ParseInt64(splitLine[len(splitLine)-1])
66+
if err != nil {
67+
return nil, nil, err
68+
}
69+
70+
first = append(first, firstValue)
71+
second = append(second, secondValue)
72+
}
73+
74+
return first, second, nil
75+
}
76+
77+
func main() {
78+
name := os.Args[1]
79+
content, err := lib.LoadFileContent(name)
80+
if err != nil {
81+
log.Fatal(err)
82+
}
83+
84+
fmt.Printf("Part 1: %d\n", part1(content))
85+
fmt.Printf("Part 2: %d\n", part2(content))
86+
}

2024/day01/main_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package main
2+
3+
import (
4+
"strings"
5+
"testing"
6+
7+
"github.com/kfarnung/advent-of-code/2020/lib"
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
var input string = strings.Join([]string{
12+
"3 4",
13+
"4 3",
14+
"2 5",
15+
"1 3",
16+
"3 9",
17+
"3 3",
18+
}, "\n")
19+
20+
func TestPart1(t *testing.T) {
21+
assert.Equal(t, int64(11), part1(input))
22+
23+
inputContent, err := lib.GetInputContent()
24+
if err != nil {
25+
t.Fatal(err)
26+
}
27+
28+
assert.Equal(t, int64(1603498), part1(inputContent))
29+
}
30+
31+
func TestPart2(t *testing.T) {
32+
assert.Equal(t, int64(31), part2(input))
33+
34+
inputContent, err := lib.GetInputContent()
35+
if err != nil {
36+
t.Fatal(err)
37+
}
38+
39+
assert.Equal(t, int64(25574739), part2(inputContent))
40+
}

2024/go.mod

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module github.com/kfarnung/advent-of-code/2020
2+
3+
go 1.19
4+
5+
require github.com/stretchr/testify v1.10.0
6+
7+
require (
8+
github.com/davecgh/go-spew v1.1.1 // indirect
9+
github.com/pmezard/go-difflib v1.0.0 // indirect
10+
gopkg.in/yaml.v3 v3.0.1 // indirect
11+
)

2024/go.sum

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
2+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
4+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
5+
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
6+
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
7+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
8+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
9+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
10+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

2024/lib/input.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package lib
2+
3+
import (
4+
"os"
5+
"strings"
6+
)
7+
8+
func LoadFileContent(name string) (string, error) {
9+
content, err := os.ReadFile(name)
10+
if err != nil {
11+
return "", err
12+
}
13+
14+
return string(content), nil
15+
}
16+
17+
func SplitLines(input string) []string {
18+
return strings.Split(input, "\n")
19+
}

2024/lib/intmath.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package lib
2+
3+
// AbsInt64 calculates the absolute value of a given int
4+
func AbsInt64(n int64) int64 {
5+
if n < 0 {
6+
return -n
7+
}
8+
9+
return n
10+
}
11+
12+
// ModInt64 calculates the modulus value (as opposed to the remainder)
13+
// https://github.com/golang/go/issues/448
14+
func ModInt64(x, y int64) int64 {
15+
return ((x % y) + y) % y
16+
}

2024/lib/parse.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package lib
2+
3+
import "strconv"
4+
5+
// ParseInt32 parses the string as a base-10 int
6+
func ParseInt32(text string) (int32, error) {
7+
value, err := strconv.ParseInt(text, 10, 32)
8+
if err != nil {
9+
return 0, err
10+
}
11+
12+
return int32(value), nil
13+
}
14+
15+
// ParseInt64 parses the string as a base-10 int64
16+
func ParseInt64(text string) (int64, error) {
17+
return strconv.ParseInt(text, 10, 64)
18+
}

2024/lib/point2d.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package lib
2+
3+
import "fmt"
4+
5+
// Point2D represents a point in 2D space
6+
type Point2D struct {
7+
X, Y int64
8+
}
9+
10+
// NewPoint2D intitializes a Point2D struct
11+
func NewPoint2D(x, y int64) Point2D {
12+
return Point2D{
13+
X: x,
14+
Y: y,
15+
}
16+
}
17+
18+
// Add to the coordinates of the point
19+
func (p *Point2D) Add(x, y int64) {
20+
p.X += x
21+
p.Y += y
22+
}
23+
24+
// Rotate90DegreesClockwise rotates the point 90 degrees clockwise about the origin
25+
func (p *Point2D) Rotate90DegreesClockwise(count int) {
26+
for i := 0; i < count; i++ {
27+
x := p.X
28+
p.X = p.Y
29+
p.Y = -x
30+
}
31+
}
32+
33+
// Rotate90DegreesCounterClockwise rotates the point 90 degrees counter-clockwise about the origin
34+
func (p *Point2D) Rotate90DegreesCounterClockwise(count int) {
35+
for i := 0; i < count; i++ {
36+
x := p.X
37+
p.X = -p.Y
38+
p.Y = x
39+
}
40+
}
41+
42+
// ManhattanDistance calculates the manhattan distance between two points
43+
func (p Point2D) ManhattanDistance(other Point2D) int64 {
44+
return AbsInt64(other.X-p.X) + AbsInt64(other.Y-p.Y)
45+
}
46+
47+
func (p Point2D) String() string {
48+
return fmt.Sprintf("(%d, %d)", p.X, p.Y)
49+
}

0 commit comments

Comments
 (0)