Browse Source

[2020-12-03] Add solutions for day03

master
Elis Hirwing 10 months ago
parent
commit
d83ed5dd0b
Signed by: etu GPG Key ID: D57EFA625C9A925F
  1. 2
      Makefile
  2. 11
      day03/example.txt
  3. 323
      day03/input.txt
  4. 20
      day03/lib.go
  5. 14
      day03/parse.go
  6. 9
      day03/solve1.go
  7. 13
      day03/solve2.go
  8. 4
      main.go

2
Makefile

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

11
day03/example.txt

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

323
day03/input.txt

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

20
day03/lib.go

@ -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
}

14
day03/parse.go

@ -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))
}
}

9
day03/solve1.go

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

13
day03/solve2.go

@ -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)
}

4
main.go

@ -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