From de3fdf2e01269128138783fdec7d2a8c1c448c16 Mon Sep 17 00:00:00 2001 From: Bill Thiede Date: Tue, 1 Dec 2020 19:45:38 -0800 Subject: [PATCH] Add old 2017 puzzle 3. --- 2017/3/3.go | 29 ++++++++++++++++++++++ 2017/3/3_test.go | 64 ++++++++++++++++++++++++++++++++++++++++++++++++ 2017/3/README.md | 28 +++++++++++++++++++++ 3 files changed, 121 insertions(+) create mode 100644 2017/3/3.go create mode 100644 2017/3/3_test.go create mode 100644 2017/3/README.md diff --git a/2017/3/3.go b/2017/3/3.go new file mode 100644 index 0000000..74e0696 --- /dev/null +++ b/2017/3/3.go @@ -0,0 +1,29 @@ +package main + +import ( + "fmt" + "log" + "math" +) + +// spiralIndex computes the x, y offsets for a counter-clockwise spiral. It +// assumes a cartesian coordinate system centered at 0,0 with an index of 1. +func spiralIndex(addr int) (xoff, yoff int) { + r := math.Sqrt(float64(addr)) + i, f := math.Modf(r) + log.Printf("addr = %d r = %f i = %f f = %f", addr, r, i, f) + if f == 0 { + i := int(i) + // Bottom right corner. + return (i - 1) / 2, -(i - 1) / 2 + } + return 0, 0 +} + +func stepCount(cell int) int { + cnt := 0 + return cnt +} +func main() { + fmt.Println(stepCount(265149)) +} diff --git a/2017/3/3_test.go b/2017/3/3_test.go new file mode 100644 index 0000000..f4174bc --- /dev/null +++ b/2017/3/3_test.go @@ -0,0 +1,64 @@ +package main + +import "testing" + +func abs(i int) int { + if i < 0 { + return -i + } + return i +} + +func TestSpiralIndex(t *testing.T) { + for _, ts := range []struct { + idx int + x, y int + }{ + {idx: 1, x: 0, y: 0}, + {idx: 2, x: 1, y: 0}, + {idx: 3, x: 1, y: 1}, + {idx: 4, x: 0, y: 1}, + {idx: 5, x: -1, y: 1}, + {idx: 6, x: -1, y: 0}, + {idx: 7, x: -1, y: -1}, + {idx: 8, x: 0, y: -1}, + {idx: 9, x: 1, y: -1}, + {idx: 10, x: 2, y: -1}, + {idx: 11, x: 2, y: 0}, + {idx: 12, x: 2, y: 1}, + {idx: 13, x: 2, y: 2}, + {idx: 14, x: 1, y: 2}, + {idx: 15, x: 0, y: 2}, + {idx: 16, x: -1, y: 2}, + {idx: 17, x: -2, y: 2}, + {idx: 18, x: -2, y: 1}, + {idx: 19, x: -2, y: 0}, + {idx: 20, x: -2, y: -1}, + {idx: 21, x: -2, y: -2}, + {idx: 22, x: -1, y: -2}, + {idx: 23, x: 0, y: -2}, + {idx: 24, x: 1, y: -2}, + {idx: 25, x: 2, y: -2}, + } { + if x, y := spiralIndex(ts.idx); x != ts.x || y != ts.y { + t.Errorf("spiralIndex(%2d) = %2d,%2d; want %2d,%2d d %d", ts.idx, x, y, ts.x, ts.y, abs(ts.x)+abs(ts.y)) + } + } +} + +func TestStepCount(t *testing.T) { + t.Skip("Skipping until index code works") + for _, ts := range []struct { + cell int + want int + }{ + {cell: 1, want: 0}, + {cell: 12, want: 3}, + {cell: 23, want: 2}, + {cell: 1024, want: 31}, + } { + if got := stepCount(ts.cell); got != ts.want { + t.Errorf("stepCount(%d) = %d; want %d", ts.cell, got, ts.want) + } + } +} diff --git a/2017/3/README.md b/2017/3/README.md new file mode 100644 index 0000000..eb415e3 --- /dev/null +++ b/2017/3/README.md @@ -0,0 +1,28 @@ +# Part 1 + +You come across an experimental new kind of memory stored on an infinite two-dimensional grid. + +Each square on the grid is allocated in a spiral pattern starting at a location marked 1 and then counting up while spiraling outward. For example, the first few squares are allocated like this: + +``` +17 16 15 14 13 +18 5 4 3 12 +19 6 1 2 11 +20 7 8 9 10 +21 22 23---> ... +``` + +While this is very space-efficient (no squares are skipped), requested data must be carried back to square 1 (the location of the only access port for this memory system) by programs that can only move up, down, left, or right. They always take the shortest path: the Manhattan Distance between the location of the data and square 1. + +For example: + + * Data from square 1 is carried 0 steps, since it's at the access port. + * Data from square 12 is carried 3 steps, such as: down, left, left. + * Data from square 23 is carried only 2 steps: up twice. + * Data from square 1024 must be carried 31 steps. + +How many steps are required to carry the data from the square identified in your puzzle input all the way to the access port? + +Your puzzle input is `265149`. + +