Compare commits

...

2 Commits

Author SHA1 Message Date
Gareth
8f5cb6b236 Day02 2024-12-02 13:40:07 +00:00
Gareth
c133fe757c Day01 2024-12-02 12:52:40 +00:00
10 changed files with 2262 additions and 0 deletions

View File

@@ -0,0 +1,54 @@
package day01
import (
"math"
"sort"
"strconv"
"strings"
)
func Part1(input string) int {
var leftList, rightList []float64
total := 0.0
lines := strings.Split(input, "\n")
for _, line := range lines {
sides := strings.Fields(line)
leftNum, _ := strconv.ParseFloat(sides[0], 64)
rightNum, _ := strconv.ParseFloat(sides[1], 64)
leftList = append(leftList, leftNum)
rightList = append(rightList, rightNum)
}
sort.Float64s(leftList)
sort.Float64s(rightList)
for i := 0; i < len(leftList); i++ {
total += math.Abs(leftList[i] - rightList[i])
}
return int(total)
}
func Part2(input string) int {
var leftList, rightList []int
lines := strings.Split(input, "\n")
for _, line := range lines {
sides := strings.Fields(line)
leftNum, _ := strconv.Atoi(sides[0])
rightNum, _ := strconv.Atoi(sides[1])
leftList = append(leftList, leftNum)
rightList = append(rightList, rightNum)
}
counts := make(map[int]int)
for _, num := range rightList {
counts[num]++
}
similarityScore := 0
for _, num := range leftList {
similarityScore += num * counts[num]
}
return similarityScore
}

View File

@@ -0,0 +1,27 @@
package day01
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestPart1(t *testing.T) {
r := Part1(`3 4
4 3
2 5
1 3
3 9
3 3`)
assert.Equal(t, 11, r)
}
func TestPart2(t *testing.T) {
r := Part2(`3 4
4 3
2 5
1 3
3 9
3 3`)
assert.Equal(t, 31, r)
}

1000
2024/gareth/day01/input.txt Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,76 @@
package day02
import (
"math"
"strconv"
"strings"
)
func Part1(input string) int {
total := 0
lines := strings.Split(input, "\n")
for _, line := range lines {
var report []int
nums := strings.Fields(line)
for _, num := range nums {
intnum, _ := strconv.Atoi(num)
report = append(report, int(intnum))
}
if isSafe(report) {
total++
}
}
return total
}
func Part2(input string) int {
total := 0
lines := strings.Split(input, "\n")
for _, line := range lines {
var report []int
nums := strings.Fields(line)
for _, num := range nums {
intnum, _ := strconv.Atoi(num)
report = append(report, int(intnum))
}
if isSafeWithDampener(report) {
total++
}
}
return total
}
func isSafe(report []int) bool {
increasing := report[1] > report[0]
decreasing := report[1] < report[0]
for i := 1; i < len(report); i++ {
diff := report[i] - report[i-1]
absDiff := int(math.Abs(float64(diff)))
if absDiff < 1 || absDiff > 3 {
return false
}
if (diff > 0 && !increasing) || (diff < 0 && !decreasing) {
return false
}
}
return true
}
func isSafeWithDampener(report []int) bool {
if isSafe(report) {
return true
}
for i := 0; i < len(report); i++ {
modifiedReport := append(report[:i], report[i+1:]...)
if isSafe(modifiedReport) {
return true
}
}
return false
}

View File

@@ -0,0 +1,27 @@
package day02
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestPart1(t *testing.T) {
r := Part1(`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`)
assert.Equal(t, 2, r)
}
func TestPart2(t *testing.T) {
r := Part2(`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`)
assert.Equal(t, 4, r)
}

1000
2024/gareth/day02/input.txt Normal file

File diff suppressed because it is too large Load Diff

45
2024/gareth/day02/poc.py Normal file
View File

@@ -0,0 +1,45 @@
def is_safe(report):
increasing = report[1] > report[0]
decreasing = report[1] < report[0]
for i in range(1, len(report)):
diff = report[i] - report[i - 1]
abs_diff = abs(diff)
if abs_diff < 1 or abs_diff > 3:
return False
if (diff > 0 and not increasing) or (diff < 0 and not decreasing):
return False
return True
def is_safe_with_dampener(report):
if is_safe(report):
return True
for i in range(len(report)):
modified_report = report[:i] + report[i+1:]
if is_safe(modified_report):
return True
return False
def read_reports_from_file(filename):
reports = []
with open(filename, 'r') as file:
for line in file:
report = list(map(int, line.split()))
reports.append(report)
return reports
def main():
filename = 'input.txt'
reports = read_reports_from_file(filename)
safe_count = 0
for report in reports:
if is_safe_with_dampener(report):
safe_count += 1
print("OUTPUT:", safe_count)
if __name__ == "__main__":
main()

11
2024/gareth/go.mod Normal file
View File

@@ -0,0 +1,11 @@
module aoc2024
go 1.23.2
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
)

9
2024/gareth/go.sum Normal file
View File

@@ -0,0 +1,9 @@
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/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=

13
2024/gareth/main.go Normal file
View File

@@ -0,0 +1,13 @@
package main
import (
"aoc2024/day02"
"fmt"
"os"
)
func main() {
data, _ := os.ReadFile("day02/input.txt")
fmt.Printf("part 1: %d\n", day02.Part1(string(data)))
fmt.Printf("part 2: %d\n", day02.Part2(string(data)))
}