[2020-12-03] Add solutions for day03

master
Elis Hirwing 3 years ago
parent e0674428d0
commit d83ed5dd0b
Signed by: etu
GPG Key ID: D57EFA625C9A925F

@ -9,6 +9,8 @@ day02:
go run main.go day02 input
day03:
go run main.go day03 input
day04:
day05:
day06:

@ -0,0 +1,11 @@
..##.......
#...#...#..
.#....#..#.
..#.#...#.#
.#...##..#.
..#.##.....
.#.#.#....#
.#........#
#.##...#...
#...##....#
.#..#...#.#

@ -0,0 +1,323 @@
..#.#...#.#.#.##.....###.#....#
...........##.#...#.#..........
....#.....#..#.............#...
.#....###..##...#...##...#.#..#
#.......#.........#..#.......#.
...#.##..##...#.#......#.##.#..
#.#..##.....#.....#..##........
...#.####...#.##...#...........
.#...#..#..#....#.#.#.#.##.....
##.#..#.##..#......#..##.#.#..#
.#.##.....#.#...............#.#
..##.#.....#.....##..##.#....#.
#..#..........#...##........#..
#..##.#.#...............#..#...
..#....#...#.......#.......#...
.........#.#.##.#........#.....
#...##....#..#.........#.#...##
...#.#...#...........#..#...#..
...#..#........#...#...........
.#....##.#...#.#....#....##....
...#...#......#.#.......#...##.
####..........##....#..........
#..#...........................
#....#...####..##.#......#.#...
..#..#.....##.....#...#....#..#
#.##......#..##........#.......
..........#.....#...#.#.#....##
....##...##..#........#...#..#.
#..#..#...##..............##...
###.##..##.###...#....##.#..#..
.#......#.................#.#..
#.#..#.##.#.#.#.....#.........#
..##......#.......##........#..
#..............#.##.#.....#....
............................##.
..#.##......#..........#....#..
..##.....#..##.#....#.......##.
..#.#.##.#.........#...........
...........##.#.#...#......###.
#....#...#........#.#...#.#.###
..............#...#.....##....#
#...#...#..............#..#...#
.##..#.........#.##.#..#...##..
.....#.........#..#..#.......#.
.#......#.#.#....##..#...#..##.
#....................#.#....#..
......#.....##............#....
.#.....#......####.....#....##.
##.####.#..#..........#......#.
##....................#..##....
.....#...#.#.##.#.###.....#....
.#..#...####.#.#...#.#.....#...
#.....##.........##.##.##.....#
....#....##.###.........#...###
.......#........#.##.....#####.
...#.##..#...#...####.....##...
..#....#....#......#......#.#..
...#.#.#.........#.......#..#..
.....#...........#.#........##.
..##...#.#.##.#.#.#...###.#....
..##.............###....#.#....
#.......#....#..#...#..##..#...
....##..#.......####....#..#.##
##....#...#.#.#...#...#........
....#.#................#...#...
...#.....#.#.......##....#.#..#
#....##.#...#.#..#.#.........#.
#..##.........##.....#...#.....
....#.....#.#..#..##..##.##...#
#.....#...#.#.#.##....#.#.##...
.#.#........#..##.......#...#.#
..###.....#..#.##....#...#....#
...#..###...#...#.......#..#...
.#....##.......#.#..........##.
...#.#.............##.....##...
..#..#...#.....#...#...........
.#.#......#.##....#.....#......
........#.#.....#.#...#..#.#..#
#.....#.#.....#.##..#.#....#.#.
..#..###.#.#........#.....##..#
#.#....#......#.#....###..#...#
...#.#....#..#.##.....#...#....
....##....#.#...#.........#..##
.#......#...#.............#..#.
#........#........#.#.....##...
..##..#.##..#........#.........
.....#...#...#..#.....#.#.##.#.
..#..#..#.........#...#.......#
....#.....#.......#.##.#.##..##
......#.......##...#......#....
....#....##.......###.#......#.
.....#..#.#........#....#.....#
#...#...#....#...###........#..
#...........####.......#.#..#.#
..###....#..........#...#.###..
....#.#.....#....#..#.....#.##.
...##.#..#..#.......#......#.#.
....#......###..#.....#.....#..
.....#.#.#.....#.##.#....####..
.##....#.....#.#....##..#......
#..#.....#..#...#....#.#.......
.##.#..####..#.##.#......#.....
......#....#.......##.##....#..
...#....#....#..##.......##.###
..##..........##.............#.
.#...#.#...##..##.....#..#.....
....#.#.##...................#.
.......#.#..#....#.....#.......
.#.#..#....####...#.#.##....#..
.#.##...#..#..#...#.#.......#..
##.#.....##.........#.......#..
.##...#.....#.........##.#....#
.............#..#............##
...##.......#.....#.......#.##.
##..##.........................
.##.#........#........#........
.....#................#.#......
.............#....#....##....#.
#..##...##...#..#.#............
.......#...####.#..#..#.....##.
..#.#..#......#.....#.#.#.....#
...#..##........#..#.#....#.#..
.#.....#..###..#....#.##.#...#.
#.#..#.##.#..#......#.###...#..
##..#.#..###....##.#...#...##.#
##..#.........#...##......#....
#.#...#.#..#..........#.......#
.......#.#.......#.....##..#...
........#..##............##.#..
........##.....#........#..#...
#..##.#..###......##...........
..#.....#.#.#....#...#.#..#..##
#...............#.......#.#.##.
#..#.....#....#............#.#.
...#....#...#....#..#..###.....
..#....#.#.....#..#......##.#.#
.#.#....#..#...#....#........#.
..##....##....#.....#.#........
.#...#....##..##.....##.....##.
.#...........#....##...##.#....
...#.....#......###.##.#.......
......#.#..##.#.#....#...#...##
....#...###.##....#.#.....#....
.......#.....#......#.....##..#
.####.#...##..#....#...........
................###...#....#..#
...#...#.....###.#.##.......#..
..#....#...##...#.###......#.#.
#...#......#............#.....#
#.........#...............#..#.
...#.##.....#............#.....
........#......##..#..#..#.#..#
....#....#.....#.#.....##..#...
.....#....#..##.....#..........
.##....#..#...........##.......
#......##.....#...#.....#......
...#.....#......#.#....#.......
...#................##...#..#..
........#..........#....#......
......#....#.#.#...........#.#.
.#............#....##.......##.
#.......#.....#...##.#..##.....
.#.....#.##..#..#....#.#..#.#.#
....#...............###........
#####...........#..#.......#..#
...#.......#...#.#............#
#...#..#.#...#.#...#.##.....##.
.#..#..#..#.....#....#...#.....
.#...#......#.......#.........#
.#....#.....#...#...#..#....#..
#....#....#.......#.....##.....
.#...#.#.##.#....#..##........#
..##...#............#..........
..........#..#..#...#....#.....
..#.......#....#.....##..##....
.#...#......#...#..###...#...#.
..##...#......#...#.#.#...#....
.....#..#.#.#.#.#...#....##..#.
##..#..##....#.#........##.#...
.##..#.#...##..#....#..#.......
.....#...#...#..#.#..#......#..
.#.....##.##..#....####..#....#
......##.................#....#
....##.......###...#.##...##.#.
...#...#.................##.#..
.#.....##...#...#.....#.....##.
##.........####..#...#...#....#
...##.....#......#.###..#......
.....###..##.#.......###..##...
#....#...#.#...#...#.#....#..#.
#...#.........##.#.........###.
#....#..###..........##........
.###.....#.#.....#........##..#
....#.........##..#..#.#.#..#..
..#......#...........#..##...#.
...#.#..#..#...#.##..#..#.....#
.#...#...#....................#
..#..##..#.............#.....#.
.....###.#.#.#...##..#.##....#.
..#...####..##.#....#...#...#..
.....#..#........#.#.#..#.##...
#.#.........####..#...#.#......
..............#..#........#....
....#........#......#.........#
#..#.##......#.#.......#....#..
....#..............#.#.#..#....
#.#......#.....##.......#..##..
.#.#..........#....#......#....
.....#.......#.##.....#......##
...#...#.##.............####...
..#....##...#...##..#.#..##.#..
..#.........##.......###.#.....
..#.........#####..##...#......
..#.#...#.......#.####......##.
......#.#.#....#......####....#
.###...........#...#..#..#..##.
..#...#..##.##...#.#.##.....#..
.....#..#....##.......#...#....
......#.....#.........#..#..#..
...#..#.........##.....##.#...#
....##...#......#..#.....#.....
....#..#....#....#........##...
##.....#.......#.....#.#.#..#..
.....#..##.....##.##.#.........
.#.#..##.............#.#.......
......#.##.#.....#.#......#..#.
..........#.#..#....#.#.#.#..##
...##.....#..#...#...#...##....
........#.#......#..###..#.....
..#.##......#.......#.......#..
...#....##.##.........#.#......
......#....#.#.........#......#
.....#...#....#...#......#..#..
.##...#......#.........#...#.#.
..#.#.#......#....#............
..#.....##.............#.##.##.
#......#......#...##.......#.#.
##........#.....#..............
.#.###.................#.#....#
........##.#..##........#.#....
.......###...#...##.#..#....#..
.#..#....#..#......##......#...
.#...#....#..........##..##.#..
.#..###.......#............#...
...#.....###.#..#........#.#.#.
...#....#..#.##..........#.#.#.
.#..##..#.....#...........#....
#...#...##....#..#....##.......
#..#......#................#...
#..##....#.#..#......#.#.#.....
##.#..#...#.....#.#...#......##
#....#.#.#....#.....##.....##..
....#...##.#...####.#.#.#.#..#.
.....#.#....#..#.....#..#......
.........#.#...................
........#.....####......#..#..#
.#.#.##.#...#.#......#...##.##.
.#......#.#.#...#..#.......#...
..#......#.##.##.#.#....#......
..........#.#...###............
.##..#..#.#.#..#.....#..#.#....
......#.......#.#..#.#....#...#
.#.......###......#...#.#.#....
.............##..#..#...#....#.
....#......#.#...#.#...#...#...
..#....#.......#.#..#..#.#..#.#
.#..#.#...#.....#.#...#####...#
.##............#....#..........
#.......####...#.#.#...........
...#.......##.#..........#....#
..#.#......#.......##.....#..##
#......#.###..#......#......#.#
##....#..#....#.##....#..#.....
...##...#.#....#.#.......#.....
#...####....#..#.#..#.##....###
.....#..#..........###..#......
.#..#..#...#....#.##..#..#.....
#..#.....#....#..#.##...##.....
.....###.#..#.......#...###.##.
#..#........#.#..#.#.........#.
....##........................#
.#....#.#.#.#.#...#......#....#
#....#...#.##.......#.#.###....
..........###..##....#..##.#...
...##..###...#.#.#.......##...#
##.#...#..#.....###....#.......
..#..##....###........##....###
.....##..#...#..#.....#..#....#
#................#....#...#..##
#....#.#....#..###.#.#...#..#.#
........##.#...#.#.#.#...#.....
..#..###....#......##.#...##...
..#..##....#.##..#.....#.....#.
.#.#...#.....#..#..#......##.#.
........#.#...#..##....#..#....
...##...#...#...#...##...##..#.
.......#..#..#....#.#..#...##..
.#.....#.##........#...#.#.....
##.#..#....#.#....#.#....#...#.
..#.#......#.......##...#....#.
#.#..####..#........#.......###
....#.......#.......##.#...#.#.
..#..#.#.............#..#......
........###.....##....#.......#
...#.....#...#...#....#.###....
#...##.#........#..#...##..#..#
...##..#....#....#.#.#...#.#...
#......#.....#....###......##..
.....#.........####...##..#....
.......#...##...#..#..#.#......
.#.#....#.....#.......#........
...##...#....##..#.....###.....
.#....#........##......#....#.#
.........#.#.#.#...........#.#.
....#.#..##......#.#.#..##.....
.........#.....##....#.........
....#.............#...........#
...#..##........#.....###......
#....#....#......#..#..#..#.#..
#......##.....#..#....#..#.#...
#..............#....#.#....###.
..##..#..#...#...##........##..
..#.##....#..#......###..#.....

@ -0,0 +1,20 @@
package day03
func checkSlope(rows [][]byte, ySpeed int, xSpeed int) int {
rowLen := len(rows[0])
key := 0
counter := 0
for i := 0; i < len(rows); i += ySpeed {
// Check if this column on this row is a tree ASCII Char 35 (#)
// Use modulo to "wrap" the map.
if rows[i][key%rowLen] == 35 {
counter++
}
// Step right
key += xSpeed
}
return counter
}

@ -0,0 +1,14 @@
package day03
import (
"git.elis.nu/etu/aoc2020/utils"
)
var rows [][]byte
func ParseFile(input string) {
// Parse file
for _, line := range utils.GetLinesFromFile("day03/" + input + ".txt") {
rows = append(rows, []byte(line))
}
}

@ -0,0 +1,9 @@
package day03
import "log"
func Solve1() {
trees := checkSlope(rows, 1, 3)
log.Println("2020-12-03.01: Answer:", trees)
}

@ -0,0 +1,13 @@
package day03
import "log"
func Solve2() {
trees := checkSlope(rows, 1, 1) *
checkSlope(rows, 1, 3) *
checkSlope(rows, 1, 5) *
checkSlope(rows, 1, 7) *
checkSlope(rows, 2, 1)
log.Println("2020-12-03.02: Answer:", trees)
}

@ -6,6 +6,7 @@ import (
"git.elis.nu/etu/aoc2020/day01"
"git.elis.nu/etu/aoc2020/day02"
"git.elis.nu/etu/aoc2020/day03"
"git.elis.nu/etu/aoc2020/utils"
)
@ -20,5 +21,8 @@ func main() {
case "day02":
utils.Perf("2020-12-02", day02.ParseFile, day02.Solve1, day02.Solve2)
case "day03":
utils.Perf("2020-12-03", day03.ParseFile, day03.Solve1, day03.Solve2)
}
}

Loading…
Cancel
Save