day9, part2

This commit is contained in:
efim 2023-12-09 16:16:18 +00:00
parent 162d0d9ebf
commit 3919a70d09
2 changed files with 51 additions and 23 deletions

View File

@ -24,6 +24,23 @@ func Run() int {
return result 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 { type Cell struct {
IsCached bool IsCached bool
Value func() int Value func() int
@ -54,7 +71,8 @@ func CachedCell(val int) Cell {
type Coord struct { type Coord struct {
Row, Col int 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 return c.Col == other.Col && c.Row == other.Row
} }
@ -66,7 +84,7 @@ type Sequence struct {
constRowVal int 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) // log.Printf(">>>> trying to get next. current last known top %d\n", s.lastReadTop)
result := s.Get(s.lastReadTop + 1) result := s.Get(s.lastReadTop + 1)
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) // log.Printf("creating new uncashed at %d %d\n", row, col)
cell = Cell{
IsCached: false, var calcFunc func() int
Value: func() int {
if col > 0 {
calcFunc = func() int {
if cell.IsCached { if cell.IsCached {
return cell.SavedValue return cell.SavedValue
} }
@ -118,7 +138,26 @@ func (s *Sequence) GetAny(coord Coord) Cell {
cell.IsCached = true cell.IsCached = true
cell.SavedValue = value cell.SavedValue = value
return 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{ return Sequence{
values: cells, values: cells,
zeroRowIndex: -1, zeroRowIndex: -1,
lastReadTop: maxRead, lastReadTop: maxRead,
} }
} }
@ -176,8 +215,8 @@ func SetupLowerRows(s *Sequence) {
inRowCalculation: inRowCalculation:
for j := 0; true; j++ { for j := 0; true; j++ {
// log.Printf("in row %d for index %d\n", i, j) // log.Printf("in row %d for index %d\n", i, j)
topRight := s.GetAny(Coord{i-1, j+1}) topRight := s.GetAny(Coord{i - 1, j + 1})
topLeft := s.GetAny(Coord{i-1, j}) topLeft := s.GetAny(Coord{i - 1, j})
if !topRight.IsCached || !topLeft.IsCached { if !topRight.IsCached || !topLeft.IsCached {
break inRowCalculation break inRowCalculation
@ -188,7 +227,7 @@ func SetupLowerRows(s *Sequence) {
if calculatedValue != 0 { if calculatedValue != 0 {
allZeroes = false allZeroes = false
} }
s.values[Coord{i,j}] = cell s.values[Coord{i, j}] = cell
} }
if allZeroes { if allZeroes {
@ -199,5 +238,5 @@ func SetupLowerRows(s *Sequence) {
s.zeroRowIndex = zeroRow s.zeroRowIndex = zeroRow
s.lastKnownRow = zeroRow s.lastKnownRow = zeroRow
s.constRowVal = s.GetAny(Coord{zeroRow-1, 0}).SavedValue s.constRowVal = s.GetAny(Coord{zeroRow - 1, 0}).SavedValue
} }

13
main.go
View File

@ -9,17 +9,6 @@ import (
func main() { func main() {
log.Print("> starting run:") log.Print("> starting run:")
// line := "0 3 6 9 12 15" result := day9.Run2()
// 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()
log.Printf("day9 result: %d\n****\n", result) log.Printf("day9 result: %d\n****\n", result)
} }