From 50254aa2c13e89766ff880fb9b96145771a31e01 Mon Sep 17 00:00:00 2001 From: Brian Buller Date: Sun, 16 Dec 2018 06:25:50 +0000 Subject: [PATCH] 2018 Day 22 Part 1 Done --- 2018/day22/day22.go | 155 +++++++++++++++++++++++++++++++++++++++++++ 2018/day22/input | 2 + 2018/day22/testinput | 2 + 3 files changed, 159 insertions(+) create mode 100644 2018/day22/day22.go create mode 100644 2018/day22/input create mode 100644 2018/day22/testinput diff --git a/2018/day22/day22.go b/2018/day22/day22.go new file mode 100644 index 0000000..e60d206 --- /dev/null +++ b/2018/day22/day22.go @@ -0,0 +1,155 @@ +package main + +import ( + "bufio" + "fmt" + "log" + "os" + "regexp" + "strconv" + "strings" +) + +var depth int +var target *Pos + +func main() { + input := stdinToStringSlice() + reg, err := regexp.Compile("[^0-9]+") + if err != nil { + log.Fatal(err) + } + depth = Atoi(reg.ReplaceAllString(input[0], "")) + tgt := strings.Trim(input[1], "target: ") + tgtPts := strings.Split(tgt, ",") + target = NewPos(Atoi(tgtPts[0]), Atoi(tgtPts[1]), depth) + +} + +func part1() { + c := BuildCave(target) + var risk int + for y := 0; y <= target.y; y++ { + for x := 0; x <= target.x; x++ { + risk += c.risk(x, y, depth) + } + } + fmt.Println("= Part 1 =") + fmt.Println(risk) +} + +func part2() { + // Find shortest path form 0,0 to target + // taking into account cost of changing gear + // when necessary + +} + +type Cave struct { + positions map[string]*Pos +} + +func BuildCave(target *Pos) *Cave { + c := &Cave{ + positions: make(map[string]*Pos), + } + c.addPos(target) + return c +} + +func (c *Cave) addPos(p *Pos) { + c.positions[p.string()] = p +} + +func (c *Cave) getPos(x, y, z int) *Pos { + var p *Pos + var ok bool + if p, ok = c.positions[c.buildKey(x, y, z)]; !ok { + p = NewPos(x, y, z) + c.addPos(p) + } + return p +} + +func (c *Cave) buildKey(x, y, z int) string { + return fmt.Sprintf( + "(%d,%d,%d)", + x, y, z, + ) +} + +func (c *Cave) getGeoIndex(x, y, z int) int { + p := c.getPos(x, y, z) + if p.geo < 0 { + if x == 0 && y == 0 { + p.geo = 0 + } else if x == target.x && y == target.y { + p.geo = 0 + } else if y == 0 { + p.geo = x * 16807 + } else if x == 0 { + p.geo = y * 48271 + } else { + p.geo = c.erosion(x-1, y, z) * c.erosion(x, y-1, z) + } + } + return p.geo +} + +func (c *Cave) erosion(x, y, z int) int { + p := c.getPos(x, y, z) + if p.erosion < 0 { + p.erosion = (c.getGeoIndex(x, y, z) + z) % 20183 + } + return p.erosion +} + +func (c *Cave) risk(x, y, z int) int { + return c.erosion(x, y, z) % 3 +} + +type Pos struct { + x, y, z int + geo, erosion int +} + +func NewPos(x, y, z int) *Pos { + r := &Pos{ + x: x, + y: y, + z: z, + geo: -1, + erosion: -1, + } + + return r +} + +func (p *Pos) equals(n *Pos) bool { + return p.x == n.x && p.y == n.y && p.z == n.z +} + +func (p *Pos) string() string { + return fmt.Sprintf( + "(%d,%d,%d)", + p.x, p.y, p.z, + ) +} + +func stdinToStringSlice() []string { + var input []string + scanner := bufio.NewScanner(os.Stdin) + for scanner.Scan() { + input = append(input, scanner.Text()) + } + return input +} + +func Atoi(i string) int { + var ret int + var err error + if ret, err = strconv.Atoi(i); err != nil { + log.Fatal("Invalid Atoi: " + i) + } + return ret +} diff --git a/2018/day22/input b/2018/day22/input new file mode 100644 index 0000000..b08bfa0 --- /dev/null +++ b/2018/day22/input @@ -0,0 +1,2 @@ +depth: 6084 +target: 14,709 diff --git a/2018/day22/testinput b/2018/day22/testinput new file mode 100644 index 0000000..eea8c66 --- /dev/null +++ b/2018/day22/testinput @@ -0,0 +1,2 @@ +depth: 510 +target: 10,10