From 37a935d03811a9e387caf6b27b0112a1c643ca5e Mon Sep 17 00:00:00 2001 From: Brian Buller Date: Thu, 3 Dec 2020 08:16:00 -0600 Subject: [PATCH] 2020 Day 3 Done! --- 2020/day03/input | 323 +++++++++++++++++++++++++++++++++++ 2020/day03/main.go | 44 +++++ 2020/day03/testinput | 11 ++ helpers/coordinateByteMap.go | 46 +++++ helpers/helpers.go | 4 + 5 files changed, 428 insertions(+) create mode 100644 2020/day03/input create mode 100644 2020/day03/main.go create mode 100644 2020/day03/testinput create mode 100644 helpers/coordinateByteMap.go diff --git a/2020/day03/input b/2020/day03/input new file mode 100644 index 0000000..3c4bd1c --- /dev/null +++ b/2020/day03/input @@ -0,0 +1,323 @@ +.#..#.....#....##.............. +...#.#...#...#.#..........#.... +#...###...#.#.....#.##.#.#...#. +#.....#.#...##....#...#...#.... +##.......##.#.....#........##.# +#..#....#......#..#......#...#. +#..#......#.......#............ +##...#.#..#...#........#....##. +#.#.#...#...#..#........#....#. +.......#...........##......#... +##.##.##......#..#............# +..#.###..#..............#...... +.##..#.....#......#.#.......... +........#.........#....#....### +#..........#........#.#.#...... +...##.....#..####.###..#.##.... +....#...###............#..#.... +...#.#...#.#...#..#.#........## +.....#...#.............#..#.... +....#.#.#.##.....##.##....#.... +..#....#............#.##.##..#. +.#..#..#................#...### +#..###.#..##..#............#... +.......#.#....#.##.#.##........ +##...###.#....#...........###.# +...#.#....#..####.........#.... +....##........#.#.#.###........ +#...#..#.....#....##..#.##...#. +##....................##..#.... +.#....##...........##...##...#. +.#.#..#.........#.........#.#.# +#.#..#.....#.#..#..#..#.#...... +...#.............#......#....## +....#.#.......#....#...#.##...# +#.#.#..###..........#...#...... +......#.....#..#..#.......##..# +.#......#......#.....#...#..... +......#..#......#.#............ +..#............#..#....#.#..... +.....#..##.......#...##.###.#.# +.....#........##....#.#...##..# +..........##.#..#.#...#..#....# +#.#.#.#.##...................#. +.....#....##.....#..#...#..#... +...#....#.............#....#.#. +.........#.##..##.............. +#...#.#....#..#...#.......#.... +.#...#......#.##.#...#.#..###.. +..#.#.#......#..#...##..##.##.. +.........#.....#......##....##. +...###.......#..#........#..... +...#....#...#.#.#......##....#. +.#.....#......#...##.##..#..... +..#.##...#....####...##........ +..#.#.###....#..##.......##.... +.....#....##...#......#.......# +.#....#......#..............#.. +.......#.#......#..#....#.#.#.. +.......#.#.........###....#.... +.#...#.......#.#..#..####....#. +..#...#.#......#..#.##.###..#.. +..##.........#............#.#.# +#.........##.##.........#.###.. +...#....#.......#..#..##....... +.#....##........##.......#..#.. +...#.....#.#.##.#.#.....##..... +.#.#........#.......#.#..#..#.. +.....####..##.##.#.#....#...... +..#.##.#.#.#....###..#....#.#.. +..##..#.#......##.#..#......... +....#..#.#.##.......#...##..... +....###.....#..###...#....###.# +..#....#.......#......#...##..# +..#..##......#....#.###..#..##. +..#..#...............#.#.#..... +...##...#.#..#.#...#......#.... +#....#...#.#.#.#.#....#....#... +....##...#....#.....##..#.....# +......##.....#...##..#.......#. +......###......#....#.##..#.... +.....#........#........#...#..# +.#..##.....##....#.#......#.#.. +#..#.#.....#........#......#.#. +.#..#.##.....#####.#....#.#.... +....##........#..........#.#... +.......#.....#.......#...#.#... +.#....#...##.###....#.#......#. +#...#...........##.#........... +#...##.......#..#........#.#..# +.....#..##..###....#.#.#....#.. +..#..#.....#............#.#.... +............#......#.....#..... +.#..#.....##.........#....###.# +#.........#....#....#.#..#...#. +##.#...##....#..#...#.#...#.... +....###..##...................# +....##...#......#...#.#...#...# +#....#....###..........#...#..# +.....##.#....###.###....#..###. +#.....#...........#...........# +##..###.##........#..#.#..#.#.. +.##...#..#.......#.#....#.....# +......##..#..#.......#.#...##.. +......#..#..#.#...###..#.#....# +#.##.#..#......#...##........## +.....#..........##.....#...#... +........#....##......#......#.# +..#..#.#...#.#.#.......#......# +.#....#........#............#.. +......##.....#...#............. +#......##..#.......##....##.#.. +.....#..#..#...#.......#..#.... +...#..##.#..#.#....##.....#..## +......#...#.#...#.#......###.#. +.#.#...#.....#..###.....#...... +#..####.#....#.......##...#.... +.##.......#.....#.........#.... +#......##.#...............#.... +.######.#...##...#...#...#..##. +....#...####....##.#..#...##... +.#...................#.#..#..#. +.#.#....##...#...#.#..#.#.#.#.. +......#......#........##.#...#. +##..#...#..#.............##.#.. +#.............#..........#.#... +...##.....#.............#...... +......###.....#................ +#.#.#....#..##.#.....#......... +.#.#........#.........#.#.##.#. +......#...##...#.#.....#....#.. +#...#.........##.##.#.......... +#..............#..#.......##... +#...#......#.#......#...#....#. +...#...#........#.#......#.###. +##.....#...#.#..#..#..#.......# +..#.##..##.........#...##.##... +#....#....#.....#..........#... +#.####..#..###.....#..#..#..... +..#.....#.##.##..####....#.#.#. +...#.#....#...#.......#..#..... +......###...#.#..#..#.......... +.........#..#.....#.#.##......# +.......#.#....##.....##.#..#.#. +.#..#.#..#......##.###...##..#. +....###...........#.....#....#. +.#.##.....#..#.....#......##... +#..##....#..........#.##.##..#. +.###.#.#..#.#.....#..##....#.#. +..##.#....#.....##..#.......... +##........#...#..#........###.# +#...#...........##.......#.#... +...###.....##.#....#...#...#... +......#....#.#.......###....#.. +...#...#.......##.......###.#.. +..............#.#..........##.. +#.#....###..#..#.........#..... +.###.#.......#.....#....#.#.... +.....###...#.#..#.#.......#.... +.........#.##.#......#.#..#.... +.......#....#....#.#....#..##.# +...............#...##.#..#.#..# +.....##........#..##........... +.##.#..#....#..#.#...#......... +.#.#..##.#..#......#....#.#...# +##....#.......##...........#... +..#...#.............#.#....#..# +..#......#..#.....#...##.....#. +....##...#.#...##...#..##...... +.....#..#..........#........... +..##....#..#.#....#..#........# +.###....#.....#.#....#..##..... +#.......##.......#..#..#....#.# +.##..#...........#..##..##..#.. +.#.................#...#....#.. +.######.......#............##.. +.#.........#......##.#.#.#.#.#. +.#.......#...#...#....###....#. +....#...##.#.#...#.....#.#..#.. +.#..#..#...#.....###....#...... +...#.##.###........#.....##.... +..#....#.#.#..........#..#..#.. +......#.....#...#..#..##..#.#.. +#.#.......##.......#....#.....# +..#...#..#.#....#.##.##........ +..#....#..##..#..##......#..... +#....#..##.....#....###........ +##...#......#..###.#.....#..... +#..###....#...#...#...#......## +.....###....#......#..#..#...#. +.##......#.......##...#........ +....#.#.....##.....#.....#..... +...##.#.....#..##...#...##.#... +..#...#.#....#....#...##....... +......#....#..#....#.#......... +..........#.#.#...##....#...... +...#....................#..#... +...#....###....#..#.....#.....# +..#....#....#..#.#..##.#...#... +..#.##....##.....#.#........#.. +#.....###..#.#.#...#..#....#... +........#..#.#..#........##.... +.##....#................##.#.## +..##...#.#.#.....##..#....#.... +....#..#....#..#........#..##.. +...#...##....#....#..##......#. +##........#...#.....#.....#...# +.#......#....##...#.........##. +##........#...#.....#..#...#.#. +...##..#..#.....#..###.#..#.... +....#..#..............#.......# +.......#.##...#......#.###..... +#........##..##....#.#.#....... +#.#..##.#.......#..##.....###.. +.....##...#..#.....#........... +...#..#..#......#...#.#........ +.#....#....#.#.....#.....#....# +...#..#...#..#.##.#......#.#.#. +..##....#..#..#.....#....#....# +...#....#.##.#..#.###......#... +.......#..#.....#.......#..#... +..###.#####..#..##.#.........#. +...#.......##...#.#..#.#......# +....#...#.###..#..........#.... +...........#...#..##........#.. +.......#...#....#....#.#..#.... +.........#..........#...#....## +.##.........##..#.......##.#... +........#......###...##...#.#.# +#.#...##.##...........#...#.#.. +.....###...#..##......#..#..... +#.#.....#.#....##..........#..# +#..#.......#.#.........####.... +#.#...#.....#........#.....#..# +.....#..#.#.###.....#.#.###.... +.###..#......##..#..#.......... +#....#.#......#...#.##......#.. +..#.........##.#.....#......... +...#....#.....##.#..#..##.#..#. +##.....#.#..#.#....#......#.... +....###.#.....#.......#..#.#... +#.....##.....##...........#.... +..........#..#......#.##...#... +#...#.###....##....#.###..###.. +##........#.#...#..#.........#. +##........##.......#.....###... +.##....###........#..##...#...# +......#..##....##.....#..#.#... +.....#..##..#.......#.......#.. +......#....#.......##.#........ +.#.####.#..#......#..#......... +.##..#....#...##.#....#....#... +..#..#..#####.........#...#.... +....#.....#.#.#.#...#.#......#. +....#...#.#..#.##...#...#...... +..#...#...#...#...#..#.#.##..#. +..#......#.#.#.##.##.##..#..... +#..###......#.##...#....#.##.#. +.#.#.......##..##....##...##.#. +.##......##....##.#.......#...# +..#...#...................#.... +.#...#.......######.....#.#..## +......#.##.....#.#............. +...........##.#........#..#.... +#.............#.#.....#....##.. +#...........#...#..###.....#... +....#.......#.#..#..#.#........ +......#...##.......#..##....#.. +......#.##.##..#........#.#...# +.#..#...##...................#. +.#.............#...#.#.#.#...#. +.........#.....#........#.#.... +#..#...#.............##.#.....# +...#.#....#...##............#.. +..#...#.##.###.#.....#......##. +...#.#..###...#.#............#. +...#....#........#.#........... +.#......#.#.#.........#.#....#. +....#..#......#.##.....#.#..... +..#..###....#....#.........###. +#..#.#....##.#....#.##..#...... +#..#.....#.#.....##..#.##...... +......#...#.#.............#..#. +#.#....#.#..#...#......#.#..... +..#.........#.#....#...#....... +.#..#.#...#....#...#......#...# +.......#........#.#..#..#...#.. +..##.#......#..##.##.#..#..#... +.##...#....##.....#.....#...##. +#.....##.#....#.#......##..#... +.......#.#..#...#.......#.#...# +..#...#.......#...#..##........ +#....##..#...#..#.#......#..#.# +##.#....#....#....#...#..#.##.. +###........#.#..#..#......#.... +.#......#.....#....#.#..#...#.. +.#.....#.....#...##.......#..## +#..##.#..#..........#.......... +...#.##.........#.#.##.#....... +.#..#...............#...#.#.#.. +.....#.#.....#...####..#.....#. +.#....#.##..##...#...##.#...#.# +....#......##...#.#.#.....#.##. +#...#..#.#...#.#.....##...#.... +..#..#....##..###......#..#.... +.........#......##.....##....#. +.......#....#...#........###... +.....#..#..#...#...#......#.... +..#..#...#.....#.....###..#.### +............#.#..#..#....#..... +...#..#...###.......#.......#.. +#.........#........#.....##.... +.#.#........#.....#........###. +....#.##.#...#.#.#.....#....#.. +.##...#..#.......#.#........... +##...#.##...#...........#.....# +##....#.#.....##..#.......#.... +##....#...#....#..#.......####. +......#...#..#.....#.#....#...# +.......#.....#..###............ +#.#.#..#.....#.............#..# +.#..#.....##.....#...#.......## +..#.##........##...........#.#. +....##.#..###.#.........#...##. diff --git a/2020/day03/main.go b/2020/day03/main.go new file mode 100644 index 0000000..ce5c10a --- /dev/null +++ b/2020/day03/main.go @@ -0,0 +1,44 @@ +package main + +import ( + "fmt" + + h "git.bullercodeworks.com/brian/adventofcode/helpers" +) + +func main() { + inp := h.StdinToStringSlice() + total := 1 + for _, slope := range []h.Coordinate{ + {X: 1, Y: 1}, + {X: 3, Y: 1}, + {X: 5, Y: 1}, + {X: 7, Y: 1}, + {X: 1, Y: 2}, + } { + field := h.StringSliceToCoordByteMap(inp) + val := countTrees(field, slope.X, slope.Y) + total = total * val + fmt.Println(slope.X, slope.Y, ":", val, "Trees") + } + fmt.Println("Total:", total) +} + +func countTrees(field h.CoordByteMap, x, y int) int { + trees := 0 + pos := h.Coordinate{X: x, Y: y} + for pos.Y < field.Height { + if pos.X > field.Width { + pos.X = pos.X - field.Width - 1 + } + if field.Get(pos) == '#' { + trees++ + field.Put(pos, 'X') + } else { + field.Put(pos, 'O') + } + pos.Y = pos.Y + y + pos.X = pos.X + x + } + return trees +} diff --git a/2020/day03/testinput b/2020/day03/testinput new file mode 100644 index 0000000..7e88cdc --- /dev/null +++ b/2020/day03/testinput @@ -0,0 +1,11 @@ +..##....... +#...#...#.. +.#....#..#. +..#.#...#.# +.#...##..#. +..#.##..... +.#.#.#....# +.#........# +#.##...#... +#...##....# +.#..#...#.# diff --git a/helpers/coordinateByteMap.go b/helpers/coordinateByteMap.go new file mode 100644 index 0000000..8ce0bb0 --- /dev/null +++ b/helpers/coordinateByteMap.go @@ -0,0 +1,46 @@ +package aoc + +type CoordByteMap struct { + Field map[Coordinate]byte + Height int + Width int +} + +func StringSliceToCoordByteMap(input []string) CoordByteMap { + ret := CoordByteMap{ + Field: make(map[Coordinate]byte), + Height: len(input), + Width: 0, + } + for y := range input { + for x := range input[y] { + ret.Field[Coordinate{X: x, Y: y}] = input[y][x] + if x > ret.Width { + ret.Width = x + } + } + } + return ret +} + +func (m *CoordByteMap) Get(pos Coordinate) byte { + if pos.X <= m.Width && pos.Y <= m.Height { + return m.Field[pos] + } + return 0 +} + +func (m *CoordByteMap) Put(pos Coordinate, val byte) { + m.Field[pos] = val +} + +func (m CoordByteMap) String() string { + var ret string + for y := 0; y <= m.Height; y++ { + for x := 0; x <= m.Width; x++ { + ret = ret + string(m.Field[Coordinate{X: x, Y: y}]) + } + ret = ret + "\n" + } + return ret +} diff --git a/helpers/helpers.go b/helpers/helpers.go index 4e5d1fc..b807420 100644 --- a/helpers/helpers.go +++ b/helpers/helpers.go @@ -101,6 +101,10 @@ func StdinToStringSlice() []string { return input } +func StdinToCoordMap() CoordByteMap { + return StringSliceToCoordByteMap(StdinToStringSlice()) +} + func Atoi(i string) int { var ret int var err error