~deciduously/aoc2020

572c9f8f95a22e10d2a7ffd9bc5f11eaf6d8ba23 — Ben Lovy 1 year, 6 months ago b08a1d2
Day 3
4 files changed, 410 insertions(+), 0 deletions(-)

A 3/day3
A 3/day3.nim
A 3/puzzle
A 3/sample
A 3/day3 => 3/day3 +0 -0
A 3/day3.nim => 3/day3.nim +76 -0
@@ 0,0 1,76 @@
proc readLines(name: string): seq[string] =
    let f = open(name)
    defer: f.close()

    var ret: seq[string] = @[]

    for line in f.lines:
        ret.add(line)

    ret

type
    Row = seq[bool]

# Process one row into a sequence of bools
proc rowToSeq(str: string): Row =
    var ret = newSeq[bool]()
    for ch in str:
        ret.add(ch == '#')
    ret

proc numCols(row: Row): int =
    len(row)

type
    Grid = seq[Row]

# Read the whole map
proc inputToGrid(row_strs: openArray[string]): Grid =
    var ret = newSeq[Row]()
    for row in row_strs:
        ret.add(rowToSeq(row))
    ret

proc numRows(grid: Grid): int =
    len(grid)

type
    Map = ref object
        grid: Grid
        position: (int, int)

proc newMap(filename: string): Map =
    Map(
        grid: inputToGrid(readLines(filename)),
        position: (0, 0)
    )

proc mapSize(map: Map): (int, int) =
    (map.grid.numRows(), map.grid[0].numCols())

# Are we standing on a tree?
proc currentVal(map: Map): bool =
    map.grid[map.position[0]][map.position[1]]

proc reset(map: Map) =
    map.position = (0, 0)

# Count trees
proc traverse(map: Map, right: int, down: int): int =
    var ret = 0
    let (height, width) = map.mapSize()
    while map.position[0] < map.grid.numRows():
        if map.currentVal(): ret += 1
        if map.position[0] + down > height - 1: break
        map.position[0] = map.position[0] + down
        map.position[1] = (map.position[1] + right) mod width
    map.reset()
    ret

# Tries all prescribed slopes, multiplies results
proc tryAllSlopes(map: Map): int =
    map.traverse(1, 1) * map.traverse(3, 1) * map.traverse(5, 1) * map.traverse(7, 1) * map.traverse(1, 2)

var map = newMap("puzzle")
echo map.tryAllSlopes()
\ No newline at end of file

A 3/puzzle => 3/puzzle +323 -0
@@ 0,0 1,323 @@
.....#.........#...#..##....#..
.#........#...#........#.......
......#......#..#...#....#.#..#
...#.#####.#.......##.#........
...........#......#..#.....#...
#.#..#...#.#...#.##.....#.....#
....#..#....#...#.#...#.##.....
##...#..........##..######.....
.....#...#......#.............#
........##....#...##..#....#...
...#...#.........#.#..........#
..#.#.....##..........#........
##.......................#.....
#..#...##...##.#.........##....
.#....#.#####....#...#...#.....
#......#......###..#........#.#
.#....##..##.###.#.......#.....
.#..#.........##....#.#....#...
........#..................#...
.......#..#..#............#....
........#...................##.
.#......#......#.####......#...
..###.#..#..#.........#........
..#...........###..#.....#.##..
...#.##.#....#................#
#.....#.............#.#........
.#..............#.........#....
##.................#..........#
.#..#....#.###....##..#..#...#.
##........#......##.....#....##
#......#..#........#......#.#..
....#.##.#.............#...##..
.#...#...#..#............##...#
.#..#...#..#..#....##..#.#.#...
#....#...##.#.#......#........#
#..#..#...#.#.....#..##.#......
.....#..#.#..#.##.......#..###.
#......#......#...#............
.....#......#......#..#.##..#.#
......#..##..#.....#....#......
..#..#...#..#...#....###.#.#...
.................#..#..........
......#...##..#.....#...##.....
..#...............#...#.#.....#
.#....#.##.##..#.........##....
...###....##...#......#......##
....#...#.....#.........#..##..
..###.........#..#..#...#......
...##.....#.........#.......#..
.....#.................#.#.....
.#.###.#..#...#..##....#....##.
....#.....##.........#.#.......
.#.#....#..#................#..
..#.#......#......#........#...
#........#....#..#..#..#....#.#
#...........##..#....#..####...
.....#.......#.#...#.#....###..
.......#....#.......#..........
.............#.....#...........
#....#......#...#..##.#........
....#.......#.#.......#....###.
.####.#...........#.#.#...#.#..
#..##....##.#......#...........
...##...#.#.....#.....#........
...#.............#.....#...#...
...#.....#..#.....##...###..#.#
....##..#..##..#..#...#.....#..
........#...................##.
....#.......#.....#.......#....
....##.........#.#.............
......#..#........#.#...#......
.#..#...#...........#......#..#
.#....#.#........#............#
......#...................#...#
##...#.......................#.
........###.......#.......#..#.
...........##.............#....
..##...#.....#....#......#....#
................###...##...#.#.
..#.#.....#....##...#..##......
.....................#.#......#
.......#....##.#..#........##..
.##....#......#....#.........#.
#............#.........#..#.#..
....#...........#..#....#....##
.......#..#.....##.........#...
.##..........#.#.#....#..#.....
........#....##.##.#......#....
....##..##......##.....#.###...
......##.#....##.#.#....#......
..#..#..........#.....##.....##
#........#.##...#.#....#....###
........##............#........
##.##..##.#..#...##............
....#..#....#...........#....#.
..#.......#.#.......#...#......
.#..........##.....#..#...#...#
.................##.#...#...##.
##.............#......#....#...
..........#.#....#.............
...##..#.#.....#.....#.#.......
...##...##.#......#.#...#......
..#..#.....##..##..........##..
......##........##.......#....#
....#..####..#...##........#...
#.......#....#.......##.......#
........#..........#.........#.
.....#....#.........#.#.#.....#
..##.....#....#....#..#......#.
....#..#.##...#..#.....#......#
........###.........#..###...#.
.....#.......#.....#.#.#.......
...##.....#....##.....#.#.#...#
#.##....#.##.....#.#.#........#
.##..#.......#...#.#.......#...
.#..........#.............#....
.#...#...#......#..##..........
.......................#.#....#
............###....#..##.#..#..
...#.#......##....#..#.........
..#...#....#....#.#............
..#.#..###...............##....
.....##...#.....#........#..#.#
...........#......#..#...#.##.#
#...##......##...#..#...#..#...
..##....#............#......#.#
.#.#..#...#...#.#...#...##..##.
..#.#....#.......#.#.#.#.#.##..
....###.##..#...##....#........
.#...............#........#....
...#..#........##...#.##.......
........#..#..#......##........
##....#....#............#......
#....#...#.###.#.###.......#...
...#.###.##....#.........#...##
..#......##.#.....#..#.......#.
##.............#..#..##....#.#.
#...#...##........#.#.......#..
........#..#.....#.#..#..#.#...
#..##.........#.#.#.##...#....#
............#...#....#..#....#.
.....#.......#......##..#......
.#.....................#......#
...................#....#.#....
.....#....#.....##.............
#....##.#....##..#....##....#..
....#..........#..........#....
.....#.#...............#..##...
...#......###.......#..##......
#.#.#....##..#......#.##.#.....
.#...###..#.....##.........#.#.
..#...#.............#....#.....
#..#.............#.....#.....#.
.#.........#.#...#..#....#...#.
#....#......#....#.#..........#
.........................#.....
...................#...........
#.#...#......#....#............
.#..#........#...##....#....#..
..#......#..#..........##......
#.#....#....##....#.........#..
...#.#.#.#..#....##..#....#..#.
..#..............#.....##......
....#.........#...#.....#..#...
..#..................#.#.......
.....##.##........#.#....#..###
..#.#...#.....#..##..##.#.#.#..
.....#......#............#.....
.#.......#....##...............
...#.................#.....#...
...#.#..#.#...##........#....##
..........##...................
#........#..........#.#........
................#..##.##.#....#
....##..#.#.#...#...#....#.#.#.
..#.........#......##....#.....
.##.........#.....#.#..........
...##...###...........#......##
..#........#......#.....##.#...
###.....#.#.#...#.......#....#.
..##...#....###..##.#.#..##....
..###...##.......#.#..#....#..#
..#...............###....#..#..
...........#....#.##..#........
.#...#..#.#...##..#....#...#..#
..#............#......#.....#..
.#...#...#.#...#.#.............
...####.........#....##....#.#.
.....##...#........#.#......#..
...####...#.#..#.#.#.#.........
........#.##.#..#.......#......
......##......#.........#.#....
..#.#...#....#.....###.....##..
#.#.##..........#...##..#..#.#.
.....#................#.#..#..#
.........#........#.....#..#..#
......#...........#...........#
..#........#.#.........#...##..
.....####.....#....##.#........
....#...#........#.......#...#.
...#..#....#.....##....###.....
........#..#..#.#.#............
#..#......#..#....#....#.#.#..#
.........#...#......##.........
..#....#............#..#.....#.
#............#.#...#......#...#
..#..##...#........#.........##
.#...#....##...#.......#..##...
#..#.##......#........##...#...
...#..........#...#..#..#....#.
##..#........##..##...#..###.#.
............##...............#.
#......#...#....#.........#...#
................#..#.#.........
.....#...#...#...##.......#...#
..##.###...#...#.#..##.#.#...#.
#...##..........#....##.#.#.#..
.#.........#..........#........
.......#.#...............#.....
...#...#............#..........
.........#..#..........#.......
.........#..#...#....#.##....#.
..#............#......#....#.##
...#...#.#........#......#..#.#
........#......##...##...#..#.#
.......###......#............#.
#.....#...##.#.#...#.......#.#.
..#......#..............##....#
..#............##.......#.#.#.#
...#.#.....#.#.#........####...
...#................#..........
..#...#....#....#......#..#...#
.###......#..............#.#..#
......#......#..........##..#..
...##.#...........#.#.....##.#.
.#...#......#..........#.......
....#...#....#..........#.#....
..................##..#.....#.#
###.................#......##..
.....#.....#............#.#..#.
.....#........#...#....#.#.....
#.#...#........................
.#...#.......#..#.......#......
.......#.#.....###.#...#.#.....
#...#.#...........##...#.......
.#.......#.....#..#..#....#....
...#..##.....#..#..#.....#.....
...#................###......#.
#..#...##.###..#..##.......#...
.#.#.#........#.#.............#
#.......#..#.......#.....##...#
.#.#.#............#..#....#.#..
...#.#.##.#......##.....#....##
#............###...#....#......
.....#..#..#.#.........##.#....
.#.##........#.#.#...#.......#.
..###..#..#.#...#.##...###.....
#............#.............#...
.#.##.....#..#.......#...#...#.
.#...#........###...####.......
.#.#..##..#.....#.#..#.........
....#.#.#............##..#...#.
###.##......#.#.....#.....#....
.........#...##.....##....#....
..#................#.........#.
#.......###..##..##............
.....#...#.............#..#..#.
..........#...................#
....#....#...........#.........
.##.......##.##.........##.....
#......#.#....#....#...#.#.#...
..#.##..#.###.#.##....#..#.....
#....##.#...#..................
.......#...#...........#...#...
....###.#...#..#...............
##.#.#..#.#......#.#......#...#
.............#.....#.##....#...
#.............###....#...#.##.#
#..#.##.............#.##...#...
.#.#......#.........#...#......
.#.........#.#.#.....##.#.#....
.................#........#....
....##.#.#..#.........#........
#...##......##....#.#..#......#
..........##...##..#......##...
..........#..#.#..##..#..#.....
..#..#.....##........#...#.#...
#..........#.#.#..............#
#..........##.....#.#...##....#
.....#...#..#..#...##.#.......#
.##.#...............#.#...#....
..........#.....#......#.......
.....#.#......##...#.......#...
...........#.#...#.....#....#.#
.###.#........##....#.##...#...
#....#.##....#.###..##.#.......
##...........#..##.........#...
....#.##...#...#.....#.#..#....
........#.#.#..#.#...........##
..........#.##...#....#......#.
.##.....#.#.....##.#.......#.#.
.#..#....#.#.....#.##.#....#..#
#.......#..#..........##....#..
.#........#...#..#.#...#....#..
#......##...#...##..#.#.......#
.#......#.##.#............##.#.
.#....#.....##..##..........#..
..###..#..#...#...#.#.#..##....
.#.#.##...#..#...........#....#
....#......#.......##...#.#.#..
.......#..#...##..#.........#..
....#..#.#.......##........#..#
........#.#....#.##..#.......#.
.....#.......#.#...#.#.........
........#...#....#.#....###..#.
......#..#.##..##..#...#.#.....
.#.#.....#.....#....#...#...#..
...#..#...#..#......#..#.#.....
...##...#...........#..#......#
..#...#####..#.#.##....##......
...........#......#.#..#.......
..#....##..#.##.......#.#.#..#.
..#..#........#...#.......#....

A 3/sample => 3/sample +11 -0
@@ 0,0 1,11 @@
..##.......
#...#...#..
.#....#..#.
..#.#...#.#
.#...##..#.
..#.##.....
.#.#.#....#
.#........#
#.##...#...
#...##....#
.#..#...#.#
\ No newline at end of file