add 2024/day2
This commit is contained in:
parent
9cc8248821
commit
e492d553aa
5 changed files with 1125 additions and 9 deletions
|
@ -25,19 +25,11 @@ func list_distance(list_a []int, list_b []int) (dist int) {
|
|||
sort.Ints(list_b)
|
||||
|
||||
for i := range list_a {
|
||||
dist += abs(list_a[i] - list_b[i])
|
||||
dist += helper.Abs(list_a[i] - list_b[i])
|
||||
}
|
||||
return dist
|
||||
}
|
||||
|
||||
func abs(x int) int {
|
||||
if x < 0 {
|
||||
return -x
|
||||
}
|
||||
|
||||
return x
|
||||
}
|
||||
|
||||
func list_similarity(list_a []int, list_b []int) (sim int) {
|
||||
for _, val := range list_a {
|
||||
valcount := countIntInList(list_b, val)
|
||||
|
|
1000
2024/02/input
Normal file
1000
2024/02/input
Normal file
File diff suppressed because it is too large
Load diff
101
2024/02/main.go
Normal file
101
2024/02/main.go
Normal file
|
@ -0,0 +1,101 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"aoc2024/helper"
|
||||
"fmt"
|
||||
"sort"
|
||||
)
|
||||
|
||||
var reports [][]int
|
||||
|
||||
func main() {
|
||||
reports = helper.ReadLinesToIntArrays()
|
||||
|
||||
// part 1
|
||||
save_count := 0
|
||||
for _, r := range reports {
|
||||
if isReportSave(r) {
|
||||
save_count++
|
||||
}
|
||||
}
|
||||
fmt.Printf("Save reports: %d\n", save_count)
|
||||
|
||||
// part 2
|
||||
save_count = 0
|
||||
for _, r := range reports {
|
||||
if isReportSaveWithDamper(r) {
|
||||
save_count++
|
||||
}
|
||||
}
|
||||
fmt.Printf("Save reports (with damper): %d\n", save_count)
|
||||
}
|
||||
|
||||
func isReportSaveWithDamper(report []int) bool {
|
||||
|
||||
if isReportSave(report) {
|
||||
return true
|
||||
}
|
||||
|
||||
for i := range report {
|
||||
// remove one item from report and try to validate
|
||||
new_report := removeFromSlice(report, i)
|
||||
if isReportSave(new_report) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func removeFromSlice(slice []int, dropIndex int) []int {
|
||||
var newslice []int
|
||||
for i, v := range slice {
|
||||
if i != dropIndex {
|
||||
newslice = append(newslice, v)
|
||||
}
|
||||
}
|
||||
|
||||
return newslice
|
||||
}
|
||||
|
||||
func isReportSave(report []int) bool {
|
||||
|
||||
if !isReportSorted(report) {
|
||||
return false
|
||||
}
|
||||
|
||||
for i := range report {
|
||||
if (i + 1) < len(report) {
|
||||
if !isPairSave(report[i], report[i+1]) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func isReportSorted(slice []int) bool {
|
||||
ascSorted := sort.SliceIsSorted(
|
||||
slice,
|
||||
func(i, j int) bool { return slice[i] < slice[j] },
|
||||
)
|
||||
descSorted := sort.SliceIsSorted(
|
||||
slice,
|
||||
func(i, j int) bool { return slice[i] > slice[j] },
|
||||
)
|
||||
|
||||
return ascSorted || descSorted
|
||||
|
||||
}
|
||||
|
||||
func isPairSave(x int, y int) bool {
|
||||
if x == y {
|
||||
return false
|
||||
}
|
||||
diff := helper.Abs(x - y)
|
||||
if diff > 3 {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
|
@ -37,3 +37,17 @@ func ReadLinesTwoIntSlices() (list_a []int, list_b []int) {
|
|||
|
||||
return list_a, list_b
|
||||
}
|
||||
|
||||
func ReadLinesToIntArrays() (lines [][]int) {
|
||||
scanner := GetLines()
|
||||
for scanner.Scan() {
|
||||
string_line := strings.Fields(scanner.Text())
|
||||
int_line := make([]int, len(string_line))
|
||||
for i, val := range string_line {
|
||||
int_line[i], _ = strconv.Atoi(val)
|
||||
}
|
||||
lines = append(lines, int_line)
|
||||
}
|
||||
|
||||
return lines
|
||||
}
|
||||
|
|
9
2024/helper/math.go
Normal file
9
2024/helper/math.go
Normal file
|
@ -0,0 +1,9 @@
|
|||
package helper
|
||||
|
||||
func Abs(x int) int {
|
||||
if x < 0 {
|
||||
return -x
|
||||
}
|
||||
|
||||
return x
|
||||
}
|
Loading…
Reference in a new issue