diff --git a/main.go b/main.go index 308ef94..c8039ee 100644 --- a/main.go +++ b/main.go @@ -3,7 +3,7 @@ package main import ( "fmt" - "github.com/thatguygriff/aoc2020/two" + "github.com/thatguygriff/aoc2020/three" ) func main() { @@ -12,6 +12,10 @@ func main() { // fmt.Println(one.PartTwo()) // Day 2 - fmt.Println(two.PartOne()) - fmt.Println(two.PartTwo()) + // fmt.Println(two.PartOne()) + // fmt.Println(two.PartTwo()) + + // Day 3 + fmt.Println(three.PartOne()) + fmt.Println(three.PartTwo()) } diff --git a/three/day_three.go b/three/day_three.go new file mode 100644 index 0000000..c013182 --- /dev/null +++ b/three/day_three.go @@ -0,0 +1,71 @@ +package three + +import ( + "bufio" + "fmt" + "os" +) + +type forest struct { + trees [][]bool + rows, width int +} + +func (f *forest) load(filename string) error { + file, err := os.Open(filename) + if err != nil { + return err + } + defer file.Close() + + scanner := bufio.NewScanner(file) + for scanner.Scan() { + if f.rows == 0 { + f.width = len(scanner.Text()) + } + + row := make([]bool, f.width) + for position, possibleTree := range scanner.Text() { + row[position] = (string(possibleTree) == "#") + } + f.trees = append(f.trees, row) + f.rows++ + } + + return nil +} + +func (f *forest) toboggan(right, down int) int { + x, y, trees := 0, 0, 0 + for y < len(f.trees)-1 { + y = y + down + x = x + right + if x > (f.width - 1) { + x = x % f.width + } + + if f.trees[y][x] { + trees++ + } + } + + return trees +} + +func PartOne() string { + forest := forest{} + forest.load("three/trees.txt") + trees := forest.toboggan(3, 1) + return fmt.Sprintf("Found %d trees on right 3, down 1", trees) +} + +func PartTwo() string { + forest := forest{} + forest.load("three/trees.txt") + product := forest.toboggan(1, 1) * + forest.toboggan(3, 1) * + forest.toboggan(5, 1) * + forest.toboggan(7, 1) * + forest.toboggan(1, 2) + return fmt.Sprintf("Found product %d of trees over 5 runs", product) +} diff --git a/three/day_three_test.go b/three/day_three_test.go new file mode 100644 index 0000000..d0c6b71 --- /dev/null +++ b/three/day_three_test.go @@ -0,0 +1,33 @@ +package three + +import "testing" + +func Test_forest_load(t *testing.T) { + forest := forest{} + if err := forest.load("sample.txt"); err != nil { + t.Log(err) + t.FailNow() + } + + if forest.rows != 11 { + t.Logf("Expected 11 rows, Got %d rows", forest.rows) + t.FailNow() + } + + if forest.width != 11 { + t.Logf("Expected 11 columns, Got %d columns", forest.rows) + t.FailNow() + } +} + +func Test_forest_toboggan(t *testing.T) { + forest := forest{} + forest.load("sample.txt") + + impacts := forest.toboggan(3, 1) + + if impacts != 7 { + t.Logf("Expected 7 tree hits, actually hit %d trees", impacts) + t.FailNow() + } +} diff --git a/three/sample.txt b/three/sample.txt new file mode 100644 index 0000000..8f551de --- /dev/null +++ b/three/sample.txt @@ -0,0 +1,11 @@ +..##....... +#...#...#.. +.#....#..#. +..#.#...#.# +.#...##..#. +..#.##..... +.#.#.#....# +.#........# +#.##...#... +#...##....# +.#..#...#.# \ No newline at end of file diff --git a/three/trees.txt b/three/trees.txt new file mode 100644 index 0000000..a1b6437 --- /dev/null +++ b/three/trees.txt @@ -0,0 +1,323 @@ +.........#..##..#..#........#.. +#...#..#..#...##.....##.##.#... +....#..............#....#....#. +#.#..#.....#...#.##..#.#.#..... +........#..#.#..#.......#...... +.#........#.#..###.#....#.#.#.. +........#........#.......#..... +...##..#.#.#........##......... +#.#.##..###............#...#... +............#....#.......###.## +....##....##..#........#......# +............#.#..........#..... +#.#....#....##...#.....#.....#. +......#.#.#...#.....###....#..# +...........##..#.........#..#.# +..#..#.................#..#..#. +.#....###...#.......#.........# +#.#.#.#...#......#.......#...#. +.......#.#.#...#..............# +...##.......#..##.#.......##... +#.#.##....#..##..##..###...###. +.#......##.##.#....#.##........ +..###.............##..##..#.... +.....#.#...........#..##..##... +.###.#.#......#.....#........## +...#.......#...##..#..#..#..... +..............#.#..##.##..##..# +#..#.#......#............#..... +........#..#....#.............. +...#...#..............#.#####.. +...##......#........#.#...#.... +..##......#............#..#..#. +....#.........#.#.#.....###.#.. +#....#........#........#....#.# +.....#...#..##.....##...#.....# +#...#.#.#...##..##.###.#.#..... +......#.#..........#...#.##.... +..............##...#..#.......# +........##.....#.....#.#....#.. +..............#..#..#...#.....# +##......##.......##...#.#....#. +.....#.............#.#......... +#.........##..#..#.........##.. +..#..#.....#####.........##.#.. +.......##.#......#........#.... +#.................#.#...#....#. +...#........#.###.##.##.....#.. +#.....##..#...##.#.#......#.... +.....#..#.#..........##..#.##.. +..###.............#..#..#...#.. +...###..#...#.....##..........# +#.......#.#...#....#..##..#..#. +.#..#.........#..............#. +..######.....#....##......#.... +#..##...#......#..#.#....#..... +.#...................#.#.....#. +..#...#.#..#.#......#..#...#..# +..##..##.#.##.........#.#.#.... +...#...#...........#..##.##...# +#...#....#....#....#..#.##..#.. +..#.##....#....###..#.......... +#.#..##.#.#...##.#..#.##..#.#.. +#......##...#.#..........#..#.. +#.#...#..#...#.#.#..#........#. +#.#.##.#..#...#..#.#.##........ +.....#......#........#..#...... +...#....#.#....#............... +....#..###..#....#..#....#....# +.#........###..........##.##.#. +#.#......##....##...##.#......# +#..##.##...#...........##.#.#.. +.#.....#.#...#................. +##..........#..#....#.....#...# +....#.#..........##..#.....#.## +#.#..#..#..##..........#....... +..#.#.###...................... +......##..##.....#..##.##....#. +...#.......#.##....#......#.... +...#...#........#...#.#...#..## +##...#....#.#...#.#.##..##...#. +...#.....#...#...#....###.#..#. +..#.#..#........#......#..##..# +...#......#...#.#.##...##.#.#.# +....#.#....#....#.....#.....##. +.....#.#..##.#....##....##..... +.#...###..#.....#............#. +#..#.#.#..#..#...#....#...#.... +#.....#..#...#................# +..........#..#.......#......#.# +...#..#......#...#......#...... +.#.#.....#.#.#.#......#..#..#.. +.....#.........#.#.#.....##.#.. +.....#.#.....#..#..#..#.....### +##....#......##....##.#....#.#. +#####........#..........##..... +.#...##...#...#.......#....#... +#.#.##...##...##..##........#.. +#.#..............#.#...#...###. +...#.....##..#.........#....#.# +#.#....#....#..##.#..#...#..... +..#....#.#..#...#...##.....#... +....#...#...................... +..#...#.......#..#...##....#... +.#........#...#.....##.##...#.. +#......#..............#..#..#.. +...........#.#..#.#.#....#....# +.##..##.......#...#..#.....#..# +...#.........#.........###..#.. +...#.##....#....#.....#.....#.. +.#.#.#.........#.#.#....#....#. +...#..........##..#....#.#..... +...#....##................#.... +#....##..#..#........##...#.... +#...#...##.#............#....#. +##..#....#...#...............#. +..........#.#...#..##..#.#..... +..##...##..#....#.#......#..... +.......#......#.#.....#.....##. +#...###.....##..##....#.#....#. +.###......#.....#.#............ +#.....#.....####.##....#..#.... +......###.............#......## +.........##.......##..#..#..#.. +.#.......#....#...#...#.#...... +#...#..#...#........#...##..#.. +.#....#........#.........##..#. +..............##.#...##..#.##.# +.#....#...#....#......#..#..... +#....##.#...#.#.....###..#....# +#.......##.#..###.............. +#..#..#..#......#.#..#...#..#.# +.......#.#.#..#..#...#..#...... +.#..#......#.....#......##..##. +....#....#.......#.......#.#.## +.......#.#................#...# +#.#.....#.......#.#........#... +.....#....##...#......#.....##. +.#......#.#...#..#....#....#.## +##...#.###.#....#..#....#.#...# +....#.##..##.#.............#... +#..#.............##.......#.#.. +##.#..#..#.#...........###...## +.#.#.....#......###........#... +#.#...#.#....##......#.#....#.. +#.........#..........#......... +.......#....#...#..#.....#...## +.......................#...#..# +.###...........##...#........## +#.#....######.#........#..##.#. +..#.##.#...#.#.......#.##.##..# +#.............###..#.##.#...... +...#..##......#...#..###.....#. +..........#.....#..#...##..#... +..##..........#.#..#.....#...#. +...#.......#.....##.........#.. +#..#.#...#..#...###...#...#.#.. +#.##....#..#.#.......#..#..#... +..#.##.#......#.#......#....#.. +..........#...##.....###....... +...#...##..#......#...##....... +....#........#.#.......#..###.. +.....#.#..........##.#..#..#.#. +.............##.....#.#..##.... +...#...............##...#...... +....#......#..#....#...##..#... +.##.#....#.#.....#.#.........#. +.....#.###....#..###..#.#.....# +.#.........##.........##...#... +..#.....###....##..........#..# +........#..#.#.#..#.......#..## +..#.#..#.#............#.##.#..# +.#....#.....#..#...#.......##.. +.#...........#.#..#..###.###... +..#.....#..#........#.#........ +.#........##........#..#.##.... +......#.....##........##..#.... +.#..................##....#.#.. +.#..#.#..#.#...#........#...... +...#..##.#......#..#..........# +....#.##...#....##............. +#....#.##....##.###..#..#..#... +..........#..#...##.##....#..#. +.###.#.....#...#...#...#....... +............#...............#.# +#....#...#......#....#.#.#.#.## +...#..........#.#.#.....###.... +#.#...##...#..#.....###...#.... +......#...#..#..#..#.##...##... +...#..#.#....#...#.#.........## +##....#..###.#.##.....##....... +..#.#...#..##.......#.#.......# +##......#...........#......#... +.......#..###....###..##.#...## +.........#.....#..#.......##..# +.......#.##..#....#...#.#...#.. +#..#.#..................##.#..# +...#..#..#.....#..#........#... +...#.#..###..#.....##...#....#. +..#..#......#...........#...#.. +#...##.##..###.......##........ +.#.....#..#....#.....#.##....#. +#..#........#.#....#..#...#.### +..#...#.#.#.....#.....#..#..... +.##.............#.#......##...# +.#....#####............#.....## +#.###.......#.#...##.....#..... +......#.##..#...#..#..##.#..##. +......#.#...##.....#...#....##. +....#............#...#...#....# +.........##.#.#....#....#....## +.#...##.#...#.......#.##....#.# +#....#.#...#.#...#.#.#...#..... +.#.#.........##..#..#.......... +.#.........#.#.....#..#.#..###. +....##.#.#..........#..####.... +....#..#.#.#...#...#..#....#... +..#.#...#...##.......#.#.#..#.. +...##...#......#.....#.#...#..# +......#.###.#.......##...#...#. +.....#.#.#......##..........### +##.#.#.#..#....#............... +.#.#.##.......#....#.#.....#..# +.........#...#.#..#.......#.... +....#.####.#......#...#...##... +#..#..#..#..#....#...##.....##. +......####.#..##..#.....##..... +##.#.........#........#..#.#... +.#.#....#....#.......#.#....##. +....#....#.......##..#.....#... +.#......#..#....#.#............ +#..#.#.##.....#..#.#.#.#.#.##.. +.#.....#.....#...#..#.#...#.#.. +.#.#.##............#.#.#.#.#.#. +.##..........#.....#...###..... +#.#...#...#................#.#. +##...#.##.....#.....#.#.##..... +####.....##..........#......#.. +#.............#..............#. +.###....#.#...#..#..#..#....... +..#.#.....#...#..#..####....... +...#.#..#........#..##..#..#.## +.#........#..........#.#...##.. +.#.......#.#.#..#...#..#.#...## +.#.....##......##.............. +......#..#.#.##...##.#.....#... +.........#.#...##.....##....#.# +.....##...#........#..#.#..#.#. +.#.##..#.....##...#...###.#.#.. +...##...#...#..#.#..#.......... +##..............#...#...#.#..#. +......#..#......#..#.....#...#. +.......#...#..#....#.....#..... +..##.....##..#.#........#...... +.###.#...#..................... +..#...#.................#...#.. +#..#.##...####.............#... +.##....#..####.......#......... +#..#...###...#...#..#..##...... +....#.##.#.#.........#.....#..# +.....#...#.....#.#.#.##.#...##. +.............#........#.....#.. +...##.###.#....##.......#..#... +#..#....#....#.#............#.. +.........#.##........##.....#.. +.........#.#.#..#..#.......#... +.......#.#..#.......#.....#.#.. +##.#.....##...##.....#.#....... +.#.#.#......##.##.#.........#.. +..#.##..###.....###.........##. +.#......#..#..##...#.#...##.#.# +......#.#............#.....#... +###.#..#..#..#..#.##...#....... +.#.#.##..###....#......##..###. +#...#.#.#..#..#..##.#.##....#.. +..#...#...####...#......####.## +..##.#.####........#..#......#. +.#..#.......#...#.#.........#.. +........#.#....#..#####..#..... +.#...........#..#..#..#...#.... +....#....#...#................. +....##..#....##....#..#....#.## +....#.##.....###...#...##.##... +......##.#..##.#.#.#....#.#.#.. +##.#...###....#.#..#.#.###....# +......###..#..#..........##...# +..........#.##...##..#....##.#. +.#...#.#..#.#.#..#.....#....... +.#....#..#.#..#.#...##.#.#..... +.##.....#...#..##.#........#... +....#......#.........#....#..## +.#..#.#.#.#..#..#.#.........#.. +.........#.....#...#....#...... +#..#..#........#...#.#......... +...#.#.#...##.#.#...#..#......# +#.#.#.#........#...#..#.....#.. +.###..#..#..###..#..#.......... +.....#......#.#..#...#.......#. +##.##.........#.......##....... +#...##.......#..#.#.......#.... +#..#..#.....#...#......#....... +.#..#..#.##....#.#..#...#...#.. +.#...#.....#..#.........#..#... +...#.#.#.......#....#..##.....# +.........#..##.#..#..#.#....... +#.##.....##..###..#..#..#.##... +........#......#...##...###..## +.##....##..#..#..###......#.... +............##......#...#..##.. +...##.......#......#...##.##..# +...#..#..#.#...####.#.......#.. +..#.##..#....#......#.#.....#.. +..#.##..............#..##.....# +.....##....#......#....#......# +......#..#......#.........#..#. +...#.##.###...###..#.##........ +..........####.#.##.....#..#.## +#...##...........#...........## +#.#..#.#....#.#..#....##......# +.......#...#.....#......#.#.##. +....#.##..##..........#..#..... +#.#.#...#......#...#.....#.##.# +........#.......#..##.....##... +.....####.#....#.#............. \ No newline at end of file