From 44af947ccfe113120e788e59713ebaca14791c43 Mon Sep 17 00:00:00 2001 From: artist Date: Tue, 24 Dec 2024 18:58:46 -0600 Subject: [PATCH] feat: init commit. added all prior solutions --- day1/day1.go | 33 +++++++++++++++++++++++++++++ day1/go.mod | 3 +++ day2/day2.go | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++ day2/go.mod | 3 +++ day4/day4.go | 50 +++++++++++++++++++++++++++++++++++++++++++ day4/go.mod | 3 +++ 6 files changed, 152 insertions(+) create mode 100644 day1/day1.go create mode 100644 day1/go.mod create mode 100644 day2/day2.go create mode 100644 day2/go.mod create mode 100644 day4/day4.go create mode 100644 day4/go.mod diff --git a/day1/day1.go b/day1/day1.go new file mode 100644 index 0000000..788dec0 --- /dev/null +++ b/day1/day1.go @@ -0,0 +1,33 @@ +package main + +import ( + "fmt" + "sort" +) + +func abs(num int) int { + if (num < 0) { + return -num + } else { + return num + } +} + +func main() { + var list1 = []int{3,4,2,1,3,3} + var list2 = []int{4,3,5,3,9,3} + var distanceTotal int = 0 + sort.Ints(list1) + sort.Ints(list2) + + // finding the difference between each number + if len(list1) == len(list2) { + for i := range list1 { + distanceTotal += abs(list1[i]-list2[i]) + } + } + + fmt.Println(list1) + fmt.Println(list2) + fmt.Println(distanceTotal) +} diff --git a/day1/go.mod b/day1/go.mod new file mode 100644 index 0000000..98513af --- /dev/null +++ b/day1/go.mod @@ -0,0 +1,3 @@ +module day1 + +go 1.23.0 diff --git a/day2/day2.go b/day2/day2.go new file mode 100644 index 0000000..3c6483c --- /dev/null +++ b/day2/day2.go @@ -0,0 +1,60 @@ +package main + +import ( + "fmt" +) + +// https://adventofcode.com/2024/day/2 +// 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 +var reports = [][]int{{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}} + +func abs(num int) int { + if (num < 0) { + return -num + } else { + return num + } +} + +// lvl == level +func compareLevels(report []int) (bool) { + var isReportSafe bool = false + var incrOrDecr bool = report[1] > report[0] // incr: true | decr: false + for i := 0; i < len(report)-1; i++ { + var lvlDifference int = abs(report[i]-report[i+1]) + + if lvlDifference > 3 || lvlDifference == 0 { + return false + } else if incrOrDecr != (report[i+1] > report[i]) { + return false + } else { + isReportSafe = true + continue + } + } + return isReportSafe +} + +func main() { + var safeReportCount int + + for _, report := range reports { + isReportSafe := compareLevels(report) + if isReportSafe { + safeReportCount++ + } + } + + fmt.Printf("List of reports: %v\n", reports) + fmt.Printf("Number of safe reports: %v\n", safeReportCount) +} diff --git a/day2/go.mod b/day2/go.mod new file mode 100644 index 0000000..f2dfde4 --- /dev/null +++ b/day2/go.mod @@ -0,0 +1,3 @@ +module day2 + +go 1.23.0 diff --git a/day4/day4.go b/day4/day4.go new file mode 100644 index 0000000..d2043a9 --- /dev/null +++ b/day4/day4.go @@ -0,0 +1,50 @@ +package main + +import ( + "fmt" +) + +var wordSearch = [][]string{{"M","M","M","S","X","X","M","A","S","M"}, + {"M","S","A","M","X","M","S","M","S","A"}, + {"A","M","X","S","X","M","A","A","M","M"}, + {"M","S","A","M","A","S","M","S","M","X"}, + {"X","M","A","S","A","M","X","A","M","M"}, + {"X","X","A","M","M","X","X","A","M","A"}, + {"S","M","S","M","S","A","S","X","S","S"}, + {"S","A","X","A","M","A","S","A","A","A"}, + {"M","A","M","M","M","X","M","M","M","M"}, + {"M","X","M","X","A","X","M","A","S","X"}} + +// func letterSearch(wordSearch string) (string, bool) { + +// } + +func main() { + fmt.Println(wordSearch) + + for y, letterRow := range wordSearch { + for x, letter := range letterRow { + if letter == "X" { + /* + +----------+----------+----------+ + | y-1, x-1 | y-1, x | y-1, x+1 | + +----------+----------+----------+ + | y, x-1 | letter | y, x+1 | + +----------+----------+----------+ + | y+1, x-1 | y+1, x | y+1, x+1 | + +----------+----------+----------+ + */ + // var letterNeighbors []string + // letterNeighbors[0] = board[y-1][x-1] + // letterNeighbors[1] = board[y-1][x] + // letterNeighbors[2] = board[y-1][x+1] + // letterNeighbors[3] = board[y][x-1] + // letterNeighbors[4] = board[y][x+1] + // letterNeighbors[5] = board[y+1][x-1] + // letterNeighbors[6] = board[y+1][x] + // letterNeighbors[7] = board[y+1][x+1] + } + fmt.Println(letter) + } + } +} diff --git a/day4/go.mod b/day4/go.mod new file mode 100644 index 0000000..a6e10cc --- /dev/null +++ b/day4/go.mod @@ -0,0 +1,3 @@ +module day4 + +go 1.23.0