Compare commits
2 Commits
ab9bdc63b1
...
8f5cb6b236
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8f5cb6b236 | ||
|
|
c133fe757c |
54
2024/gareth/day01/day01.go
Normal file
54
2024/gareth/day01/day01.go
Normal 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
|
||||||
|
}
|
||||||
27
2024/gareth/day01/day01_test.go
Normal file
27
2024/gareth/day01/day01_test.go
Normal 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
1000
2024/gareth/day01/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
76
2024/gareth/day02/day02.go
Normal file
76
2024/gareth/day02/day02.go
Normal 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
|
||||||
|
}
|
||||||
27
2024/gareth/day02/day02_test.go
Normal file
27
2024/gareth/day02/day02_test.go
Normal 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
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
45
2024/gareth/day02/poc.py
Normal 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
11
2024/gareth/go.mod
Normal 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
9
2024/gareth/go.sum
Normal 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
13
2024/gareth/main.go
Normal 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)))
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user