Day 3: Part 1 and 2
This commit is contained in:
10
main.go
10
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())
|
||||
}
|
||||
|
71
three/day_three.go
Normal file
71
three/day_three.go
Normal file
@@ -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)
|
||||
}
|
33
three/day_three_test.go
Normal file
33
three/day_three_test.go
Normal file
@@ -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()
|
||||
}
|
||||
}
|
11
three/sample.txt
Normal file
11
three/sample.txt
Normal file
@@ -0,0 +1,11 @@
|
||||
..##.......
|
||||
#...#...#..
|
||||
.#....#..#.
|
||||
..#.#...#.#
|
||||
.#...##..#.
|
||||
..#.##.....
|
||||
.#.#.#....#
|
||||
.#........#
|
||||
#.##...#...
|
||||
#...##....#
|
||||
.#..#...#.#
|
323
three/trees.txt
Normal file
323
three/trees.txt
Normal file
@@ -0,0 +1,323 @@
|
||||
.........#..##..#..#........#..
|
||||
#...#..#..#...##.....##.##.#...
|
||||
....#..............#....#....#.
|
||||
#.#..#.....#...#.##..#.#.#.....
|
||||
........#..#.#..#.......#......
|
||||
.#........#.#..###.#....#.#.#..
|
||||
........#........#.......#.....
|
||||
...##..#.#.#........##.........
|
||||
#.#.##..###............#...#...
|
||||
............#....#.......###.##
|
||||
....##....##..#........#......#
|
||||
............#.#..........#.....
|
||||
#.#....#....##...#.....#.....#.
|
||||
......#.#.#...#.....###....#..#
|
||||
...........##..#.........#..#.#
|
||||
..#..#.................#..#..#.
|
||||
.#....###...#.......#.........#
|
||||
#.#.#.#...#......#.......#...#.
|
||||
.......#.#.#...#..............#
|
||||
...##.......#..##.#.......##...
|
||||
#.#.##....#..##..##..###...###.
|
||||
.#......##.##.#....#.##........
|
||||
..###.............##..##..#....
|
||||
.....#.#...........#..##..##...
|
||||
.###.#.#......#.....#........##
|
||||
...#.......#...##..#..#..#.....
|
||||
..............#.#..##.##..##..#
|
||||
#..#.#......#............#.....
|
||||
........#..#....#..............
|
||||
...#...#..............#.#####..
|
||||
...##......#........#.#...#....
|
||||
..##......#............#..#..#.
|
||||
....#.........#.#.#.....###.#..
|
||||
#....#........#........#....#.#
|
||||
.....#...#..##.....##...#.....#
|
||||
#...#.#.#...##..##.###.#.#.....
|
||||
......#.#..........#...#.##....
|
||||
..............##...#..#.......#
|
||||
........##.....#.....#.#....#..
|
||||
..............#..#..#...#.....#
|
||||
##......##.......##...#.#....#.
|
||||
.....#.............#.#.........
|
||||
#.........##..#..#.........##..
|
||||
..#..#.....#####.........##.#..
|
||||
.......##.#......#........#....
|
||||
#.................#.#...#....#.
|
||||
...#........#.###.##.##.....#..
|
||||
#.....##..#...##.#.#......#....
|
||||
.....#..#.#..........##..#.##..
|
||||
..###.............#..#..#...#..
|
||||
...###..#...#.....##..........#
|
||||
#.......#.#...#....#..##..#..#.
|
||||
.#..#.........#..............#.
|
||||
..######.....#....##......#....
|
||||
#..##...#......#..#.#....#.....
|
||||
.#...................#.#.....#.
|
||||
..#...#.#..#.#......#..#...#..#
|
||||
..##..##.#.##.........#.#.#....
|
||||
...#...#...........#..##.##...#
|
||||
#...#....#....#....#..#.##..#..
|
||||
..#.##....#....###..#..........
|
||||
#.#..##.#.#...##.#..#.##..#.#..
|
||||
#......##...#.#..........#..#..
|
||||
#.#...#..#...#.#.#..#........#.
|
||||
#.#.##.#..#...#..#.#.##........
|
||||
.....#......#........#..#......
|
||||
...#....#.#....#...............
|
||||
....#..###..#....#..#....#....#
|
||||
.#........###..........##.##.#.
|
||||
#.#......##....##...##.#......#
|
||||
#..##.##...#...........##.#.#..
|
||||
.#.....#.#...#.................
|
||||
##..........#..#....#.....#...#
|
||||
....#.#..........##..#.....#.##
|
||||
#.#..#..#..##..........#.......
|
||||
..#.#.###......................
|
||||
......##..##.....#..##.##....#.
|
||||
...#.......#.##....#......#....
|
||||
...#...#........#...#.#...#..##
|
||||
##...#....#.#...#.#.##..##...#.
|
||||
...#.....#...#...#....###.#..#.
|
||||
..#.#..#........#......#..##..#
|
||||
...#......#...#.#.##...##.#.#.#
|
||||
....#.#....#....#.....#.....##.
|
||||
.....#.#..##.#....##....##.....
|
||||
.#...###..#.....#............#.
|
||||
#..#.#.#..#..#...#....#...#....
|
||||
#.....#..#...#................#
|
||||
..........#..#.......#......#.#
|
||||
...#..#......#...#......#......
|
||||
.#.#.....#.#.#.#......#..#..#..
|
||||
.....#.........#.#.#.....##.#..
|
||||
.....#.#.....#..#..#..#.....###
|
||||
##....#......##....##.#....#.#.
|
||||
#####........#..........##.....
|
||||
.#...##...#...#.......#....#...
|
||||
#.#.##...##...##..##........#..
|
||||
#.#..............#.#...#...###.
|
||||
...#.....##..#.........#....#.#
|
||||
#.#....#....#..##.#..#...#.....
|
||||
..#....#.#..#...#...##.....#...
|
||||
....#...#......................
|
||||
..#...#.......#..#...##....#...
|
||||
.#........#...#.....##.##...#..
|
||||
#......#..............#..#..#..
|
||||
...........#.#..#.#.#....#....#
|
||||
.##..##.......#...#..#.....#..#
|
||||
...#.........#.........###..#..
|
||||
...#.##....#....#.....#.....#..
|
||||
.#.#.#.........#.#.#....#....#.
|
||||
...#..........##..#....#.#.....
|
||||
...#....##................#....
|
||||
#....##..#..#........##...#....
|
||||
#...#...##.#............#....#.
|
||||
##..#....#...#...............#.
|
||||
..........#.#...#..##..#.#.....
|
||||
..##...##..#....#.#......#.....
|
||||
.......#......#.#.....#.....##.
|
||||
#...###.....##..##....#.#....#.
|
||||
.###......#.....#.#............
|
||||
#.....#.....####.##....#..#....
|
||||
......###.............#......##
|
||||
.........##.......##..#..#..#..
|
||||
.#.......#....#...#...#.#......
|
||||
#...#..#...#........#...##..#..
|
||||
.#....#........#.........##..#.
|
||||
..............##.#...##..#.##.#
|
||||
.#....#...#....#......#..#.....
|
||||
#....##.#...#.#.....###..#....#
|
||||
#.......##.#..###..............
|
||||
#..#..#..#......#.#..#...#..#.#
|
||||
.......#.#.#..#..#...#..#......
|
||||
.#..#......#.....#......##..##.
|
||||
....#....#.......#.......#.#.##
|
||||
.......#.#................#...#
|
||||
#.#.....#.......#.#........#...
|
||||
.....#....##...#......#.....##.
|
||||
.#......#.#...#..#....#....#.##
|
||||
##...#.###.#....#..#....#.#...#
|
||||
....#.##..##.#.............#...
|
||||
#..#.............##.......#.#..
|
||||
##.#..#..#.#...........###...##
|
||||
.#.#.....#......###........#...
|
||||
#.#...#.#....##......#.#....#..
|
||||
#.........#..........#.........
|
||||
.......#....#...#..#.....#...##
|
||||
.......................#...#..#
|
||||
.###...........##...#........##
|
||||
#.#....######.#........#..##.#.
|
||||
..#.##.#...#.#.......#.##.##..#
|
||||
#.............###..#.##.#......
|
||||
...#..##......#...#..###.....#.
|
||||
..........#.....#..#...##..#...
|
||||
..##..........#.#..#.....#...#.
|
||||
...#.......#.....##.........#..
|
||||
#..#.#...#..#...###...#...#.#..
|
||||
#.##....#..#.#.......#..#..#...
|
||||
..#.##.#......#.#......#....#..
|
||||
..........#...##.....###.......
|
||||
...#...##..#......#...##.......
|
||||
....#........#.#.......#..###..
|
||||
.....#.#..........##.#..#..#.#.
|
||||
.............##.....#.#..##....
|
||||
...#...............##...#......
|
||||
....#......#..#....#...##..#...
|
||||
.##.#....#.#.....#.#.........#.
|
||||
.....#.###....#..###..#.#.....#
|
||||
.#.........##.........##...#...
|
||||
..#.....###....##..........#..#
|
||||
........#..#.#.#..#.......#..##
|
||||
..#.#..#.#............#.##.#..#
|
||||
.#....#.....#..#...#.......##..
|
||||
.#...........#.#..#..###.###...
|
||||
..#.....#..#........#.#........
|
||||
.#........##........#..#.##....
|
||||
......#.....##........##..#....
|
||||
.#..................##....#.#..
|
||||
.#..#.#..#.#...#........#......
|
||||
...#..##.#......#..#..........#
|
||||
....#.##...#....##.............
|
||||
#....#.##....##.###..#..#..#...
|
||||
..........#..#...##.##....#..#.
|
||||
.###.#.....#...#...#...#.......
|
||||
............#...............#.#
|
||||
#....#...#......#....#.#.#.#.##
|
||||
...#..........#.#.#.....###....
|
||||
#.#...##...#..#.....###...#....
|
||||
......#...#..#..#..#.##...##...
|
||||
...#..#.#....#...#.#.........##
|
||||
##....#..###.#.##.....##.......
|
||||
..#.#...#..##.......#.#.......#
|
||||
##......#...........#......#...
|
||||
.......#..###....###..##.#...##
|
||||
.........#.....#..#.......##..#
|
||||
.......#.##..#....#...#.#...#..
|
||||
#..#.#..................##.#..#
|
||||
...#..#..#.....#..#........#...
|
||||
...#.#..###..#.....##...#....#.
|
||||
..#..#......#...........#...#..
|
||||
#...##.##..###.......##........
|
||||
.#.....#..#....#.....#.##....#.
|
||||
#..#........#.#....#..#...#.###
|
||||
..#...#.#.#.....#.....#..#.....
|
||||
.##.............#.#......##...#
|
||||
.#....#####............#.....##
|
||||
#.###.......#.#...##.....#.....
|
||||
......#.##..#...#..#..##.#..##.
|
||||
......#.#...##.....#...#....##.
|
||||
....#............#...#...#....#
|
||||
.........##.#.#....#....#....##
|
||||
.#...##.#...#.......#.##....#.#
|
||||
#....#.#...#.#...#.#.#...#.....
|
||||
.#.#.........##..#..#..........
|
||||
.#.........#.#.....#..#.#..###.
|
||||
....##.#.#..........#..####....
|
||||
....#..#.#.#...#...#..#....#...
|
||||
..#.#...#...##.......#.#.#..#..
|
||||
...##...#......#.....#.#...#..#
|
||||
......#.###.#.......##...#...#.
|
||||
.....#.#.#......##..........###
|
||||
##.#.#.#..#....#...............
|
||||
.#.#.##.......#....#.#.....#..#
|
||||
.........#...#.#..#.......#....
|
||||
....#.####.#......#...#...##...
|
||||
#..#..#..#..#....#...##.....##.
|
||||
......####.#..##..#.....##.....
|
||||
##.#.........#........#..#.#...
|
||||
.#.#....#....#.......#.#....##.
|
||||
....#....#.......##..#.....#...
|
||||
.#......#..#....#.#............
|
||||
#..#.#.##.....#..#.#.#.#.#.##..
|
||||
.#.....#.....#...#..#.#...#.#..
|
||||
.#.#.##............#.#.#.#.#.#.
|
||||
.##..........#.....#...###.....
|
||||
#.#...#...#................#.#.
|
||||
##...#.##.....#.....#.#.##.....
|
||||
####.....##..........#......#..
|
||||
#.............#..............#.
|
||||
.###....#.#...#..#..#..#.......
|
||||
..#.#.....#...#..#..####.......
|
||||
...#.#..#........#..##..#..#.##
|
||||
.#........#..........#.#...##..
|
||||
.#.......#.#.#..#...#..#.#...##
|
||||
.#.....##......##..............
|
||||
......#..#.#.##...##.#.....#...
|
||||
.........#.#...##.....##....#.#
|
||||
.....##...#........#..#.#..#.#.
|
||||
.#.##..#.....##...#...###.#.#..
|
||||
...##...#...#..#.#..#..........
|
||||
##..............#...#...#.#..#.
|
||||
......#..#......#..#.....#...#.
|
||||
.......#...#..#....#.....#.....
|
||||
..##.....##..#.#........#......
|
||||
.###.#...#.....................
|
||||
..#...#.................#...#..
|
||||
#..#.##...####.............#...
|
||||
.##....#..####.......#.........
|
||||
#..#...###...#...#..#..##......
|
||||
....#.##.#.#.........#.....#..#
|
||||
.....#...#.....#.#.#.##.#...##.
|
||||
.............#........#.....#..
|
||||
...##.###.#....##.......#..#...
|
||||
#..#....#....#.#............#..
|
||||
.........#.##........##.....#..
|
||||
.........#.#.#..#..#.......#...
|
||||
.......#.#..#.......#.....#.#..
|
||||
##.#.....##...##.....#.#.......
|
||||
.#.#.#......##.##.#.........#..
|
||||
..#.##..###.....###.........##.
|
||||
.#......#..#..##...#.#...##.#.#
|
||||
......#.#............#.....#...
|
||||
###.#..#..#..#..#.##...#.......
|
||||
.#.#.##..###....#......##..###.
|
||||
#...#.#.#..#..#..##.#.##....#..
|
||||
..#...#...####...#......####.##
|
||||
..##.#.####........#..#......#.
|
||||
.#..#.......#...#.#.........#..
|
||||
........#.#....#..#####..#.....
|
||||
.#...........#..#..#..#...#....
|
||||
....#....#...#.................
|
||||
....##..#....##....#..#....#.##
|
||||
....#.##.....###...#...##.##...
|
||||
......##.#..##.#.#.#....#.#.#..
|
||||
##.#...###....#.#..#.#.###....#
|
||||
......###..#..#..........##...#
|
||||
..........#.##...##..#....##.#.
|
||||
.#...#.#..#.#.#..#.....#.......
|
||||
.#....#..#.#..#.#...##.#.#.....
|
||||
.##.....#...#..##.#........#...
|
||||
....#......#.........#....#..##
|
||||
.#..#.#.#.#..#..#.#.........#..
|
||||
.........#.....#...#....#......
|
||||
#..#..#........#...#.#.........
|
||||
...#.#.#...##.#.#...#..#......#
|
||||
#.#.#.#........#...#..#.....#..
|
||||
.###..#..#..###..#..#..........
|
||||
.....#......#.#..#...#.......#.
|
||||
##.##.........#.......##.......
|
||||
#...##.......#..#.#.......#....
|
||||
#..#..#.....#...#......#.......
|
||||
.#..#..#.##....#.#..#...#...#..
|
||||
.#...#.....#..#.........#..#...
|
||||
...#.#.#.......#....#..##.....#
|
||||
.........#..##.#..#..#.#.......
|
||||
#.##.....##..###..#..#..#.##...
|
||||
........#......#...##...###..##
|
||||
.##....##..#..#..###......#....
|
||||
............##......#...#..##..
|
||||
...##.......#......#...##.##..#
|
||||
...#..#..#.#...####.#.......#..
|
||||
..#.##..#....#......#.#.....#..
|
||||
..#.##..............#..##.....#
|
||||
.....##....#......#....#......#
|
||||
......#..#......#.........#..#.
|
||||
...#.##.###...###..#.##........
|
||||
..........####.#.##.....#..#.##
|
||||
#...##...........#...........##
|
||||
#.#..#.#....#.#..#....##......#
|
||||
.......#...#.....#......#.#.##.
|
||||
....#.##..##..........#..#.....
|
||||
#.#.#...#......#...#.....#.##.#
|
||||
........#.......#..##.....##...
|
||||
.....####.#....#.#.............
|
Reference in New Issue
Block a user