Compare commits

...

4 Commits

Author SHA1 Message Date
Neil Hanlon
f41814cfc2
attempt 2 day 2 part 2 - works 2024-12-06 13:59:37 -05:00
Neil Hanlon
8bc9fc8517
attempt 1 at day 2 part 2 2024-12-06 12:30:33 -05:00
Neil Hanlon
fadc27b223
refactor attempt 1 2024-12-06 12:13:15 -05:00
Neil Hanlon
232a56d130
attempt 1 at day 2 part 1 2024-12-06 11:40:07 -05:00
3 changed files with 1161 additions and 0 deletions

1000
02/input Normal file

File diff suppressed because it is too large Load Diff

6
02/input-ex Normal file
View File

@ -0,0 +1,6 @@
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

155
02/main.go Normal file
View File

@ -0,0 +1,155 @@
package main
import (
"fmt"
"os"
"slices"
"strconv"
"strings"
)
type Report struct {
number int
levels []int
valid bool
direction string
flag int
dampened bool
}
// Reports stored one per line with levels in each column.
// In safe reports, the levels are either all increasing or all decreasing AND
// Any two adjacent levels differ by at least one and at most three.
func main() {
input, err := os.ReadFile("input")
if err != nil {
fmt.Println(err)
}
var reports []Report
lines := strings.Split(string(input), "\n")
for idx, val := range lines[0 : len(lines)-1] {
reportFields := strings.Fields(val)
report := Report{
number: idx,
valid: true,
direction: "ASC",
flag: -1,
dampened: false,
}
for _, field := range reportFields {
reportVal, err := strconv.Atoi(field)
if err != nil {
break
}
report.levels = append(report.levels, reportVal)
}
reports = append(reports, report)
}
var valid int
for _, report := range reports {
if report.IsValid() {
valid++
}
fmt.Printf("%v\n", report)
}
fmt.Printf("Total: %v Valid %v\n", len(reports), valid)
}
func (report *Report) IsValid() bool {
if report.flag > 0 {
report.dampened = true
}
// Determine direction
if len(report.levels) >= 1 {
report.direction = DetermineDirection(report.levels[0], report.levels[1])
report.valid = report.ValidDirection() && report.ValidDeltas()
}
if !report.valid && report.flag > 0 && !report.dampened {
// First, try and just remove the flagged problem
orig := make([]int, len(report.levels))
for idx := range report.levels {
copy(orig, report.levels)
orig := slices.Delete(orig, idx, idx+1)
dampened := Report{
number: report.number,
valid: report.valid,
direction: report.direction,
flag: report.flag,
dampened: report.dampened,
levels: orig,
}
if report.valid = dampened.IsValid(); report.valid {
break
}
}
}
return report.valid
}
func remove(slice []int, s int) []int {
return append(slice[:s], slice[s+1:]...)
}
func (report *Report) ValidDeltas() bool {
for idx, val := range report.levels {
if prev := idx - 1; prev < 0 {
continue
}
last := report.levels[idx-1]
delta := Abs(val - last)
if delta < 1 || delta > 3 {
report.flag = idx
return false
}
}
return true
}
func (report *Report) ValidDirection() bool {
for idx, val := range report.levels {
if prev := idx - 1; prev < 0 {
continue
}
if val == 0 {
break
}
last := report.levels[idx-1]
switch report.direction {
case "ASC":
if last > val {
report.flag = idx
return false
}
case "DESC":
if last < val {
report.flag = idx
return false
}
default:
panic("aaaaaa")
}
}
return true
}
func DetermineDirection(one, two int) string {
diff := one - two
if diff < 0 {
return "ASC"
}
return "DESC"
}
func Abs(input int) int {
if input < 0 {
return -input
}
return input
}