diff --git a/day9/dayNine.go b/day9/dayNine.go index b7bc574..9fa2421 100644 --- a/day9/dayNine.go +++ b/day9/dayNine.go @@ -24,6 +24,23 @@ func Run() int { return result } +func Run2() int { + log.Println("hello day 9, part2") + filename := "day9/input" + bytes, err := os.ReadFile(filename) + if err != nil { + panic(fmt.Sprintln("error reading file: ", err)) + } + result := 0 + + for _, line := range strings.Split(string(bytes), "\n") { + seq := CreateSequence(line) + result += seq.Get(-1) + } + + return result +} + type Cell struct { IsCached bool Value func() int @@ -54,7 +71,8 @@ func CachedCell(val int) Cell { type Coord struct { Row, Col int } -func (c Coord)Equal(other Coord) bool { + +func (c Coord) Equal(other Coord) bool { return c.Col == other.Col && c.Row == other.Row } @@ -66,7 +84,7 @@ type Sequence struct { constRowVal int } -func (s *Sequence)Next() int { +func (s *Sequence) Next() int { // log.Printf(">>>> trying to get next. current last known top %d\n", s.lastReadTop) result := s.Get(s.lastReadTop + 1) s.lastReadTop += 1 @@ -105,9 +123,11 @@ func (s *Sequence) GetAny(coord Coord) Cell { } // log.Printf("creating new uncashed at %d %d\n", row, col) - cell = Cell{ - IsCached: false, - Value: func() int { + + var calcFunc func() int + + if col > 0 { + calcFunc = func() int { if cell.IsCached { return cell.SavedValue } @@ -118,7 +138,26 @@ func (s *Sequence) GetAny(coord Coord) Cell { cell.IsCached = true cell.SavedValue = value return value - }, + } + } else { + // for part 2, propagate backward from known elements on the right + calcFunc = func() int { + if cell.IsCached { + return cell.SavedValue + } + // log.Printf("calc value for %d %d cell\n", row, col) + simplerCell := s.GetAny(Coord{row + 1, col}) + nextCell := s.GetAny(Coord{row, col + 1}) + value := nextCell.Value() - simplerCell.Value() + cell.IsCached = true + cell.SavedValue = value + return value + } + } + + cell = Cell{ + IsCached: false, + Value: calcFunc, } } @@ -154,7 +193,7 @@ func ReadTopRow(line string) Sequence { return Sequence{ values: cells, zeroRowIndex: -1, - lastReadTop: maxRead, + lastReadTop: maxRead, } } @@ -176,8 +215,8 @@ func SetupLowerRows(s *Sequence) { inRowCalculation: for j := 0; true; j++ { // log.Printf("in row %d for index %d\n", i, j) - topRight := s.GetAny(Coord{i-1, j+1}) - topLeft := s.GetAny(Coord{i-1, j}) + topRight := s.GetAny(Coord{i - 1, j + 1}) + topLeft := s.GetAny(Coord{i - 1, j}) if !topRight.IsCached || !topLeft.IsCached { break inRowCalculation @@ -188,7 +227,7 @@ func SetupLowerRows(s *Sequence) { if calculatedValue != 0 { allZeroes = false } - s.values[Coord{i,j}] = cell + s.values[Coord{i, j}] = cell } if allZeroes { @@ -199,5 +238,5 @@ func SetupLowerRows(s *Sequence) { s.zeroRowIndex = zeroRow s.lastKnownRow = zeroRow - s.constRowVal = s.GetAny(Coord{zeroRow-1, 0}).SavedValue + s.constRowVal = s.GetAny(Coord{zeroRow - 1, 0}).SavedValue } diff --git a/main.go b/main.go index 78074e9..86061a0 100644 --- a/main.go +++ b/main.go @@ -9,17 +9,6 @@ import ( func main() { log.Print("> starting run:") - // line := "0 3 6 9 12 15" - // line := "1 3 6 10 15 21" - // line := "10 13 16 21 30 45" - // seq := day9.CreateSequence(line) - // fmt.Println(seq.String()) - - // fmt.Printf("got %+v \n", seq) - // fmt.Printf("%s\n", seq.String()) - - // fmt.Printf("next value is %d\n", seq.Next()) - - result := day9.Run() + result := day9.Run2() log.Printf("day9 result: %d\n****\n", result) }