#include #define MAX_LINES 2048 #define MAX_CHARS 2048 #define SLOPES_COUNT 5 long solve(char input[MAX_LINES][MAX_CHARS], int y, int x) { size_t i=0, j=0; unsigned int width=0; long trees_count=0; /* Count lines width */ while (input[0][width] != '\0') width++; while (input[i][0] != '\0') { if (input[i][j] == '#') trees_count++; i += x; j += y; if (j >= width) j -= width; } return trees_count; } int main() { size_t i; long result = 1; struct { int y; int x; } slopes[SLOPES_COUNT] = { { 1, 1 }, { 3, 1 }, { 5, 1 }, { 7, 1 }, { 1, 2 }, }; char test_input[MAX_LINES][MAX_CHARS] = { "..##.......", "#...#...#..", ".#....#..#.", "..#.#...#.#", ".#...##..#.", "..#.##.....", ".#.#.#....#", ".#........#", "#.##...#...", "#...##....#", ".#..#...#.#", "", }; char input[MAX_LINES][MAX_CHARS] = { ".....##.#.....#........#....##.", "....#...#...#.#.......#........", ".....##.#......#.......#.......", "...##.........#...#............", "........#...#.......#.........#", "..........#......#..#....#....#", "..................#..#..#....##", ".....##...#..#..#..#..#.##.....", "..##.###....#.#.........#......", "#.......#......#......#....##..", ".....#..#.#.......#......#.....", "............#............#.....", "...#.#........#........#.#.##.#", ".#..#...#.....#....##..........", "##..........#...#...#..........", "...........#...###...#.......##", ".#..#............#........#....", "##.#..#.....#.......#.#.#......", ".##.....#....#.#.......#.##....", "..##...........#.......#..##.#.", "##...#.#........#..#...#...#..#", ".#..#........#.#.......#..#...#", ".##.##.##...#.#............##..", "..#.#..###......#..#......#....", ".#..#..#.##.#.##.#.#...........", "...#....#..#.#.#.........#..#..", "......#.#....##.##......#......", "#....#.##.##....#..#...........", "...#.#.#.#..#.#..#.#..#.##.....", "#.....#######.###.##.#.#.#.....", "..#.##.....##......#...#.......", "..#....#..#...##.#..#..#..#..#.", ".............#.##....#.........", ".#....#.##.....#...............", ".#............#....#...#.##....", ".#.....#.##.###.......#..#.....", ".#...#.........#.......#..#....", "..#.#..#.##.......##...........", ".....##..#..#..#..#.##..#.....#", "..##............##...#..#......", "...#..#....#..##.....##..#.#...", "#.....##....#.#.#...#...#..##.#", "#.#..#.........#.##.#...#.#.#..", ".....#.#....##....#............", "#.......#..#.....##..#...#...#.", ".....#.#...#...#..#......#.....", "..##....#.#.#.#.#..#...........", "##..#...#.........#......#...#.", "..#...#.#.#.#..#.#.##..##......", "#............###.....###.......", "..........#...#........###.....", ".......##...#...#...#........#.", ".#..#.##.#.....................", ".#..##........##.##...#.......#", ".......##......#.....#......#..", ".##.#.....#......#......#......", "#...##.#.#...#.#...............", "........#..#...#.##.......#....", "...................#...#...##..", "...#...#.........#.....#..#.#..", ".###..#........#..##.##..#.##..", "#...#.....#.....#.....#..#..#..", "###..#.....#.#.#.#......#....#.", "#........#....##.#...##........", ".#.#..##........##....##.#.#...", "#...#....#.###.#.#.........#...", "...#...##..###.......#.........", "......#....#..##..#.....#.#....", "........#...##...###......##...", "..........##.#.......##........", "...#....#......#...##.....#....", "###.#.....#.#..#..#....#...#..#", ".#.....#.#....#...............#", "..#....#....####....###....#.#.", "....##........#..#.##.#....#...", ".......##...#...#..#....####...", "#...##.#......##...#..#........", "..##..#.##....#.......##.#.#...", "..#.#...............#...#.#....", "....#.....#.#.....#.##.......#.", "...#.#..##.#.#..............##.", "..#.....#...#.............#.##.", "##..#.#...#........#..#.....##.", "...........##...#.#.###...#....", "...#.#.#..#..................#.", ".#...##.............#...#......", "..#..#...#.#.......#...#.....#.", "..##.......#.#.................", ".##..#........###.....#....#.##", "......#..###.......#....##....#", "....#.....#.................#..", "........#...#...#..............", "...#..#.###.......#..#.#.#.##..", "..#...#.....#....#.........#...", "...#.............#........###..", "......#..............#......#..", "#..#...........#...#..........#", "...##...#.###..#...#.....#.#...", "....#..##......#.......##......", "....#....##.#...#.#..#....#...#", ".#...........#..#....##...#..##", "..#.#.................###.#...#", "..#.#.#...##...........#.......", "..........#..##...#.#..##....##", "........#........#.##..#.#...#.", ".....#...##.......##......#...#", "....#...#..#..#.....#..........", ".#..#......#..#..#..###.......#", ".##..........#...#...#.#.....##", "..#..........#.#.#...###.......", "....#................#...##....", ".##..#....#..........#.#.#.....", "..##...#.#........#.....#.##...", "....####.....#..#.........##..#", "......#.........#...#..........", "....#...................#..##..", ".##....#.#.........#....#...#..", "....##...##.....#..####........", "..##.#....#.#.......##...#.....", "#...#.#.#...#..#..##.....#.....", "#..................###.....#...", "#.#.....#.......#.#...###.#....", ".#..#....#............#........", "#.#....#..#.#...............#..", "..#..#..#.............#......#.", "..#.......##...................", ".#....#.........#....#.#.#..#..", "....#....#..#...............#..", "......#..#..##......#.........#", "..#.##........##......#..#..#.#", "#.....#.#....#.........##...#..", "###..............#....###...##.", "....#..##......#.......##......", "......#...#.##......##....#..#.", "..........#....#..##.......#..#", ".#..#...##..#...........#..#..#", ".....#....#...#..###...###....#", ".#####..#...#.#.#..#.#.###...##", "..##............##.#...#.##...#", ".##..#...#...#....##.#..#..##..", ".#....#...#............##..#...", ".#.#......#....#....#..##..##..", ".........#...#.......#.##..#...", "#.........#.....##.....#..#..#.", "...##.#...#...#..#..#....##..##", ".#............#...#....##......", "..#...#.##.........#.#......#.#", "....#.##........#.........#..##", "#.........#......#.#......#..#.", "........#.#.......#.#........#.", "..#..........##.#...#..#.#.....", "..#...#....#...#...#..#.#..#.#.", ".#.........#....#..#####..#....", "#.#....#.#.###...#.............", "..##...........##......##......", "#.....#..#....#...............#", "...#.#..#....##......#...##....", "...#........#.....#...#..#.....", ".#......##.........#......#....", "..#..###.##...#.#.....#........", ".............#......#..#.......", "..#...............#.#...#..#..#", ".......#..#...#.#####......#..#", ".........#.....#...............", "##........#............#.#.....", ".#...#.....#..#..#...#....#...#", "..#....#....##......##.....#.#.", "#...##..##......#...#....#.....", "....#.#.#.....###....##.##....#", "..........##...##.......#......", "..#.......#...##.#....##.##....", "....#........................#.", "...#...#.#.##...#.....#...#..#.", ".#....##..#..#..........##..##.", ".#.....#..#...#.##.....#.......", ".#.##...#.#..#.....##....#...#.", ".##...#........##....#..#......", ".....#........#..........#.#..#", "....#..##.......#..#.....#.....", "...........#...#........#.##..#", ".....#..#....#..#.#.....#....##", ".....#....#.##.#..##...........", "...##.......##.........#.......", "...............##..#....#.#....", ".......###..#........#..####.##", ".......#.##...#.#....#.####....", "....#...............#..........", "##.#.......#.....#......#...#..", "......##.....#....#.....#..#..#", ".....#...##.............#......", "#.#.##.#.....#..#........#.....", "......##....#..#........#......", "............#........#..#.#....", "##.......#......#...####..#.##.", "..##..#...#.............#.##...", ".....#..##......#.##......###..", "............#........#........#", "#.#.#.#...#.#.....#.........#..", ".........#...............#.....", ".............###.#.......#....#", "###.##..#..#..........#....#...", "#......#...#..#..#.....#.##....", "............#....#....#..#.....", "..#.#....#...#......#.#..#..##.", "...#........................#..", "#.#...#..........#......#.#....", ".........#................#...#", "##.....#....#........##.......#", "#...##........#...#...........#", "...#...#..........##.......#.#.", "..#.#.#....#......##...........", "...#.#...#.##.#..#.#.##........", "#....##.....###..#.......#.....", "###.....#.#.#...#..#.........##", "..#......#..###...#.#.#.....#.#", ".#....#.....#............#..##.", "....#....##..........#.....##..", "#...........#....#...#..#...##.", "..#.......#.....#..........#...", ".#..#................#......#..", "..#......#.#...#..#.#....#....#", "...#..#...###..#..##....#.#....", "..#..............#.....#.......", "...#.#...#.........#.#.........", "##......##...........##.#.##..#", "..#..##..#....#.#......#.#...##", "...#.###....###...#.....#......", "#.#................#......#....", "..#.....#.....#....##.......#..", ".#.#...............##..#.......", "...#....#.......#.#.....##..#..", ".........#....#.......#.#...##.", "#....#......##.#.........##...#", "#.............#..##.#.#..##....", "...#....#..#...#....#.#.#.#...#", ".#....#....#..##.....#.#...###.", "##............#.#...##.#..#.#..", "##.#....##.....#..#..###....#..", "##....#................##......", "...##..#...#..###....#.....##..", ".#...##......#..#.#.....#...#..", "..##......##...#.##.......#....", "......#.....#.....##........#.#", "##....#...........#............", "#.......#....#..#.##..##.#..#..", ".#....##.#.....#..#..#.........", ".#....#.#.#...#.....##.....#.#.", ".......##.#.#........#......##.", "##........#.##.......#...#..#..", "...###..##....#.#....#.#.......", "......#.......#...##.....#...#.", "..#......##.#......#.....#.....", ".....#.....###...#.............", "#...#.#...#...#..#......#......", "#.....#.......###.#....###.#...", "...#.......#....####....##..#..", "#.#.....#....#........#.......#", ".........#.......#......#.#...#", "..##....#.....##...............", "..........#..#.#..#......#.....", "..................##...##.#....", "........#.......#...#..#.#.#...", ".....#.#..##..#..#.#..#.......#", ".....#........#..#..#....#....#", "##............#..#..#...#....#.", ".....#....................##..#", "........##.#....###............", "##.......#.##................#.", ".....###.#..#..#...#....###.##.", ".#......#.#....#.....##.#......", "...##......##.........#...#....", "....####..............#........", "#...#.#..##..##.........##.....", "......#......#....#..#.........", "#.....#.....#.##...............", "..#.##..#...##.#.####..#....###", "#..#......#....#.##..##...#.#..", "#....#.......#.....#.....#.#...", "##.......#.....##...#.....#....", "...#...##..........#..##..##..#", ".###..#..##...#....#...#..#....", "......##..###.......###...#....", "....#...#.#.......#.##...##..##", "#.#......#..##.#.#..#..#..#....", "......#........#.......#.......", "..........#.#.....##...........", "......#..#........#..#.#..###..", "##..#.............##..#........", ".........#....#.....#.........#", ".....#..##...#..#..##.##......#", "###..#...........#.......#....#", "...............#....#.#........", ".##.#...#.#........##....#.....", ".##.###...##..###....#...#...#.", ".##..#....#.#.#...#.#.#.#...#..", ".###.#...#.......#....#..#.....", "..#..#.#.#.#........#.....##...", ".#.......#.#...#.#...........##", "...#.....##....#.....##...#....", "................#.....####...#.", ".#.#......#.......##...#.##....", ".###.........#.#......#..#.#...", "#......#...#....#..##.......#..", ".##..#....#..#...........#...#.", ".#...#.......##........#.##....", "..#...........#...##...........", ".....##....##......#....#..#...", "#......#.#...#.##.#...##....#..", "#....................#...##...#", "..#............#........#......", ".............#.........##.....#", "...#...#......##.#...#...#.#...", "..#...#.#.................#....", "....##...#....#...###.##......#", "...#....#...#..#...#....#.....#", "...##.#........#..#.........#..", "..##.....#..##...#.....##...#..", "#.........#.#.#...#......#...#.", "#.#...........#...#..#..#..##..", "..#..#..##....#..........#.###.", ".....#..#....#.#...#...#..#..#.", "###.....#..#.................#.", ".#..##.##.#......#....##..#....", "", }; for (i=0; i