advent-of-code

Perserverance, or the lack thereof

git clone git://git.shimmy1996.com/advent-of-code.git
commit 475e486872d1f25f7e44d4a3cd7fd5d312d2db30
parent 14dff7dffa7a4c3dcdb229fee3713ae201754352
Author: Shimmy Xu <shimmy.xu@shimmy1996.com>
Date:   Thu,  3 Dec 2020 19:20:27 -0600

Add 2020 day 03

Diffstat:
A2020/day03/input.txt | 323+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A2020/day03/main.go | 51+++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 374 insertions(+), 0 deletions(-)
diff --git a/2020/day03/input.txt b/2020/day03/input.txt
@@ -0,0 +1,323 @@
+.........#....#.###.........##.
+..###.#......#......#.......##.
+##....#.#.......#.....#........
+#........#..........#.#...#...#
+#....###...##.....#........#...
+###..............##..#.....#...
+.................##.#..........
+.........##......#..###.....#.#
+..#..#...#.#.#...#.#.#.##...#..
+..............#.#.#..#..#..#...
+.#.#.#....#.........#.........#
+..#.#....##..#...#.....#..##..#
+............#.....#.........##.
+...#.#..#..........#.##.....#..
+#......#...##.......###........
+.....#....#.#..............#...
+.....#.......#..##.###.....#.#.
+.#.....#....##.#......##......#
+..##...##.........#..#.#..#....
+............#.......#.....#....
+.......................#...####
+.#.#.......#..#....#....##..#.#
+..#.##.#.#...............#.....
+#..##..#...#.....##..#...##.#..
+##...#....#####.....##...#...##
+.#..##..#..#.#..##.#.#........#
+....#..#........##......#.#....
+..#......#...##.#..##.......#..
+.#.#....#.#..#.....#..#...#....
+.....#.#.................#.....
+##.#........#.....#...........#
+#............#.....#..#.#...##.
+..#.#..#......#.......#......##
+....#.#....#...#....#..........
+.........#...#.##..#...#...#...
+....#...#...#..................
+..##...#.#....#...#......#.....
+#....#.......##..#...#..#......
+.#....##..#.#....#...##...#....
+#.................#...#.#...#..
+.#.....#..........#.......#....
+.#..........#.##....#.##......#
+#.#.....##.##..#.......#..#....
+.....#...#............#..##....
+...#.#.##.#..........#.#....#..
+.......#...#............#.....#
+..........#...##..#....###....#
+............#....#......###....
+...................####..#.##..
+...#.#.##.........#..#.#.......
+...........#.........#..###....
+.........#.........#...#...###.
+.#.#.##....#.#...........#.#...
+..###.....#...#.#.......##..#..
+.....#.#........#.#....#....##.
+......#...#........#.........#.
+...............#.........#.#...
+..#...#...#...#.###..###....#..
+#..#.......#..###.##.......#...
+#.#.........##..#.....###..#..#
+...#....#....#.#..#............
+..###..##....................#.
+..#.......#..........#.##......
+..##........#...###..##.#......
+......#.#...#.....#..##..#.....
+#........#......#..............
+........#........#.......#....#
+.....#.......#......#........##
+#.#......#.#...##.#.......#....
+#...................##...#.....
+..#.#...#..#...#..#.....#..#...
+.......##..................##..
+.............#..##.#......##..#
+###...........##.#....###..#...
+.#..........##...#..#......#...
+..#.###.#....#........#........
+....#....###.....#.......#.....
+.....##....#..##...#..#........
+.##...#..#....#..#.........#...
+#.........##....#..##..##......
+.#.#.............#.....##......
+..#.#............#.......##.#..
+..#.##..#.........#......#.#.#.
+.#...#...#..#....#...#....#.#.#
+....#...#..#.##..##.......##...
+.#.....#.....#............#....
+..........#....#..#..#......#..
+.............#....#..#.........
+....#.#.#.......#....#.#.......
+..##....#.#....#...#........###
+#...#..........#..........###..
+...#..#...#...#..##......#...#.
+.....................#...#..#.#
+#..#............#.........#.##.
+..#...#...........#.......#....
+.....##..........#...##.....##.
+.#.....#.#........##...........
+..#....#..#.#..##...#.........#
+.........#.###.##....#..###....
+.........#...##...##.#.#....##.
+...#..##.#...........#....#....
+..........#.#...........##.....
+...........#..........#...##...
+.........#..........#...#.#...#
+......#..#.................#...
+.....#...........#..#...#..###.
+.....#..#....#.#.##...##....##.
+...##.###.#.#..#.#....##.#.#...
+###.....#.....#........#...#...
+.#....#........#.#....#..#...##
+##.....#.....#......#.#..#...##
+.##....#...............#.......
+#.#.....#.#....#.........#.....
+..#...............#.......##...
+#...#.###..#....#.#.#..#.#.....
+##.###....##...#....#.....#....
+.......#................##.....
+....#....##..#....#..#......#..
+...#.........#...#........##.##
+.#..............#..............
+..##.......#.###..##.#.........
+..#...#...#...#...#...#.....##.
+.....#..##...#.....##..#.#.....
+..#.............#...#.........#
+#.........................#..##
+.............#..###........#...
+......#..#....#.##.......#..#..
+...#..#..#...#....#..#...##...#
+.##............#.......###.#...
+.#........#..#.................
+#.#.#.....##....##...#.....#.#.
+...##.......#.#......#...#.#...
+#.##....#.........#.....##...#.
+#...#..#....#.......#.##...##..
+.................##.#..##.#.#.#
+..#.............#.......#.#.##.
+#....##..................#...##
+..........#.......#..##........
+......#.#..#......#.#.........#
+#.#........#####......#.#.#....
+#..#........#.#..#.....#.......
+...#.............#.............
+.....#.......#......#..#.##..#.
+..#.........#..........#.##...#
+#.....#.#####......#.......###.
+.......#.....#...#.....#.#..#..
+#...#.#........#.#..#..#...#..#
+...#....#....##.....#..........
+.#.......##.......##...........
+...#.##.#.#..#....#...##....##.
+.#...#...#.........#...........
+.#.#.##..#.......#.#...#..#....
+.#....##.#.#...#......#......#.
+##..#..#..#..#.......#......#..
+.........#.#...........#....#.#
+........#....#.#...#.#..#......
+#.......#.#.................##.
+.....#..#..#....#.#........#.##
+.#..###..#....#..#........#.#..
+#...........#...#........#.....
+........#..#.#.#.#.......#....#
+....#..#..........#.#..#.....#.
+..####..........#..............
+....###.#..#........##..#......
+.#..#......##..........#...#...
+.#.....#....#......##.##...#.##
+..##.#.#......#.......##.......
+....##.......#..............#..
+........#.....##..............#
+.#...#....##.....#....#.......#
+....#.......###.......#.#.#....
+##.....##........#.....##......
+..........#.....#...##.#..#.#..
+..........#.#......#..........#
+..........#...#..#...#...#.....
+.#.......#..##.................
+.#........##..............#..#.
+.##...................#...#....
+.##....#.##.##........#........
+...##.....####.....#..#.......#
+...##.#...##...#.##............
+##.......#.....###..#..#...##..
+#.####...#...#...##..#..#....#.
+...#........##........#........
+#....#.#....##..#..#.##...#....
+...##....##....#.......#..###..
+..........#..#..........#..#.#.
+#..#....#.......#.......#....#.
+......#......#.....##..##.#..#.
+##.#.....#....#.......#...#...#
+..##..#.#...#...#.....###..##..
+....#..#.......#............##.
+#..##.#.#.....#####....#....#..
+.#........#...#.#..##.#.####.#.
+#...#...#.............#.#......
+.........#.....##..........#...
+.##....#....#........#......#.#
+#..###...#....#..........#.....
+.#...##.........#..#..#.#...#..
+#.#.#.......................#..
+#.....#..#.#............###....
+#...#.....#.....###..#..#.#.##.
+............#.........#.#.##...
+...#.......###......##......#..
+.#....#.#....##......##.#...#..
+.........#.......#....#...##..#
+................#.#.....#....#.
+.##......#....#..#..###..#...#.
+....##....#..#....#.##..#......
+.......#.#.....#..#............
+..........#....#....#..#..#....
+..#....#.....#.......#.........
+......#.........#.##..#....##.#
+..#..#.#.......##..#...##......
+...##..#.#.#...............##..
+..#.#.#......#....#.....#.##...
+..#.....#.#...........#....##..
+###.....##.....................
+.......#..#.................#..
+.#.#..#..#.........#......#...#
+##.......#.##.......#..........
+#..#.....#.....#.....#.......#.
+#..#.....#.....#..........#.#..
+.#....##....#.....#.......#.##.
+.....#.#......##..##.#.........
+#....##......#..#....#..##..#..
+#.##..#..#..............#...#..
+.#......#......#..#...........#
+..#.......#........#....#..#...
+.....##.#.......##....#.#....#.
+........#....................#.
+........#..#..........#........
+......#.#.....#.#.....#......#.
+#......##......###.##......#...
+...........###..#...#.......#..
+..#...###...#.....#....#...#..#
+.....##......#.#......##..#.#..
+#.#......##...#.....##...#...#.
+.#.#........#.......#.........#
+....#....#...##..........#.#...
+.#..##.#...#.#.....##......##..
+.#.....#..##....#....#....#..#.
+..#.......#.#.#..........#..#..
+#.#..#....#.##....#.......#....
+........##.........#..#.#......
+.......#.....#.##..##......#.#.
+.........................#.#.#.
+..#..............##.........#..
+.......###.#.#.......#.........
+#........#.....#.......#..#...#
+##....#..#....#...........#....
+..#..#.#.#.....#.#..#....#.....
+.#..##....#.##..#..............
+...#....##..#..#.##....#.#.....
+...##....#......##..#........#.
+....#......#....#....#........#
+...#..#...#.#...........#..#..#
+....#.#.#.........#...#...#....
+..........#....#......##....#.#
+..##..#...##.#...###.#.##......
+#........#.##......##.#........
+..#...#.##...#..........#.#...#
+...........#...........#.......
+......###....#..###..##........
+...#........#..#.#.............
+....#.#.....#.#............#..#
+##.#.....#........#....##.....#
+.......#.#...#..##.......#.#.#.
+#......##..#..#...#.....#..#.#.
+.#......###.....#..#.....#...#.
+....#.#.............#.##.......
+......#....#.....#.......#..#..
+#..#.#.#..#......#...#..#.#....
+#..............#.#....#...#....
+..#......#...##.#......#..#..#.
+.......##..#.##..#.#...#.....##
+.....#...........#....#.......#
+.#.........#..#..........##....
+#..##..#.#......##.......#..#..
+...#....#...........##.#.#.#..#
+#..#..#..#...........#....#.#.#
+.....##......#......#.#...#....
+.....#..##....###.....#....##..
+........#...##......#.....#..#.
+..#.#..#.#....#...#.......#....
+#.....#...#.....#.#.......#....
+......#...#.......#..##....#...
+#............#.....#....#......
+..##...#.....#..#......#...#...
+...#..#...#..#.......#........#
+...##.#.#........#........#....
+#.#..#......##.#..#..#......#..
+#.......#..#...................
+#.....#....#......#...........#
+.##.#...#.#...#..............##
+...###........#........#..##.#.
+..##....#.#.#.##..#.#......#...
+..#.#........#..............#..
+.......#.................##....
+..................#............
+....#.....#.#..............#.##
+......#.....#..#......#...#....
+..#....##......#...####....#.#.
+..........#...##...........##.#
+...#.#..#....#......#..#....#..
+#.........###...#.....#..#....#
+.#.#......##.#.....#...........
+...............#.#....##..#..#.
+..........#.#.#.#...#....##...#
+...#....##.................##..
+#..##..#...##.##.#......#.#.###
+#..#...#..#.....#...#.#..##...#
+..#................#...##....#.
+...#.....#.##.......##....#.#..
+....#.....#..#....##...........
+...............#..........#....
+....#...#........##...#........
+...#....#...#.###..............
+#.#....#.......#..#.##.##......
+.#.....#..#..#......#....#.#...
+...#..........................#
+............#.#..#.##......#...
+.....#..........#.#........#.#.
diff --git a/2020/day03/main.go b/2020/day03/main.go
@@ -0,0 +1,51 @@
+package main
+
+import (
+	"bufio"
+	"fmt"
+	"os"
+)
+
+func readInput(filename string) (res []string) {
+	file, _ := os.Open(filename)
+	defer file.Close()
+	scanner := bufio.NewScanner(file)
+	for scanner.Scan() {
+		res = append(res, scanner.Text())
+	}
+	return
+}
+
+func main() {
+	input := readInput("./input.txt")
+	fmt.Println(part1(input))
+	fmt.Println(part2(input))
+}
+
+func countTrees(input []string, dx int, dy int) (tree_count int) {
+	for x, y := dx, dy; y < len(input); x, y = (x+dx)%len(input[0]), y+dy {
+		if input[y][x] == '#' {
+			tree_count += 1
+		}
+	}
+	return
+}
+
+func part1(input []string) int {
+	return countTrees(input, 3, 1)
+}
+
+func part2(input []string) int {
+	// Right 1, down 1.
+	// Right 3, down 1. (This is the slope you already checked.)
+	// Right 5, down 1.
+	// Right 7, down 1.
+	// Right 1, down 2.
+	dxs := []int{1, 3, 5, 7, 1}
+	dys := []int{1, 1, 1, 1, 2}
+	tree_prod := 1
+	for i, dx := range dxs {
+		tree_prod *= countTrees(input, dx, dys[i])
+	}
+	return tree_prod
+}