diff options
| author | Thomas Schmucker <ts@its1.de> | 2024-11-17 16:59:53 +0100 |
|---|---|---|
| committer | Thomas Schmucker <ts@its1.de> | 2024-11-17 16:59:53 +0100 |
| commit | 0413a04a422e5985ae91a02489cafcb85515ea9f (patch) | |
| tree | 669ea63a5186e48f6915a76317bacce9af490391 /2015 | |
| parent | ccf0e4ab6360b0038ed9a8c44dad840f5979230e (diff) | |
| download | advent-of-code-0413a04a422e5985ae91a02489cafcb85515ea9f.tar.gz advent-of-code-0413a04a422e5985ae91a02489cafcb85515ea9f.tar.bz2 advent-of-code-0413a04a422e5985ae91a02489cafcb85515ea9f.zip | |
aoc 2015, day 18
Diffstat (limited to '2015')
| -rw-r--r-- | 2015/src/day18.cpp | 143 |
1 files changed, 143 insertions, 0 deletions
diff --git a/2015/src/day18.cpp b/2015/src/day18.cpp new file mode 100644 index 0000000..fa465f4 --- /dev/null +++ b/2015/src/day18.cpp | |||
| @@ -0,0 +1,143 @@ | |||
| 1 | #include <algorithm> | ||
| 2 | #include <fstream> | ||
| 3 | #include <iostream> | ||
| 4 | #include <map> | ||
| 5 | #include <tuple> | ||
| 6 | using namespace std; | ||
| 7 | |||
| 8 | using puzzle_type = map<tuple<size_t, size_t>, bool>; | ||
| 9 | |||
| 10 | auto | ||
| 11 | read_file(string_view filename) | ||
| 12 | { | ||
| 13 | fstream input{ filename }; | ||
| 14 | puzzle_type puzzle; | ||
| 15 | |||
| 16 | size_t line = 0; | ||
| 17 | for ( string str; getline(input, str); ) { | ||
| 18 | for ( size_t column = 0; column != str.size(); ++column ) { | ||
| 19 | if ( str[column] == '#' ) { | ||
| 20 | puzzle[{ column, line }] = true; | ||
| 21 | } | ||
| 22 | } | ||
| 23 | |||
| 24 | ++line; | ||
| 25 | } | ||
| 26 | |||
| 27 | return puzzle; | ||
| 28 | } | ||
| 29 | |||
| 30 | void | ||
| 31 | iterate(puzzle_type& puzzle, size_t width) | ||
| 32 | { | ||
| 33 | puzzle_type next; | ||
| 34 | |||
| 35 | for ( size_t line = 0; line != width; ++line ) { | ||
| 36 | for ( size_t column = 0; column != width; ++column ) { | ||
| 37 | static const vector<tuple<size_t, size_t>> offsets = { | ||
| 38 | { -1, -1 }, | ||
| 39 | { 0, -1 }, | ||
| 40 | { 1, -1 }, | ||
| 41 | { -1, 0 }, | ||
| 42 | { 1, 0 }, | ||
| 43 | { -1, 1 }, | ||
| 44 | { 0, 1 }, | ||
| 45 | { 1, 1 } | ||
| 46 | }; | ||
| 47 | |||
| 48 | size_t count = 0; | ||
| 49 | for ( const auto& [delta_x, delta_y]: offsets ) { | ||
| 50 | if ( puzzle[{ column + delta_x, line + delta_y }] ) { | ||
| 51 | ++count; | ||
| 52 | } | ||
| 53 | } | ||
| 54 | |||
| 55 | if ( puzzle[{ column, line }] ) { | ||
| 56 | if ( count == 2 || count == 3 ) { | ||
| 57 | next[{ column, line }] = true; | ||
| 58 | } | ||
| 59 | } | ||
| 60 | else { | ||
| 61 | if ( count == 3 ) { | ||
| 62 | next[{ column, line }] = true; | ||
| 63 | } | ||
| 64 | } | ||
| 65 | } | ||
| 66 | } | ||
| 67 | |||
| 68 | puzzle.swap(next); | ||
| 69 | } | ||
| 70 | |||
| 71 | void | ||
| 72 | print(puzzle_type puzzle, size_t width) | ||
| 73 | { | ||
| 74 | for ( size_t line = 0; line != width; ++line ) { | ||
| 75 | for ( size_t column = 0; column != width; ++column ) { | ||
| 76 | auto value = puzzle[{ column, line }]; | ||
| 77 | cout << (value ? '#' : '.'); | ||
| 78 | } | ||
| 79 | cout << '\n'; | ||
| 80 | } | ||
| 81 | cout << endl; | ||
| 82 | } | ||
| 83 | |||
| 84 | void | ||
| 85 | part1(puzzle_type puzzle) | ||
| 86 | { | ||
| 87 | static const size_t width = 100; | ||
| 88 | |||
| 89 | for ( int i = 0; i != 100; ++i ) { | ||
| 90 | iterate(puzzle, width); | ||
| 91 | } | ||
| 92 | |||
| 93 | long sum = 0; | ||
| 94 | for ( size_t line = 0; line != width; ++line ) { | ||
| 95 | for ( size_t column = 0; column != width; ++column ) { | ||
| 96 | auto value = puzzle[{ column, line }]; | ||
| 97 | if ( value ) { | ||
| 98 | ++sum; | ||
| 99 | } | ||
| 100 | } | ||
| 101 | } | ||
| 102 | cout << sum << endl; | ||
| 103 | } | ||
| 104 | |||
| 105 | void | ||
| 106 | enable_corners(puzzle_type& puzzle, size_t width) | ||
| 107 | { | ||
| 108 | puzzle[{ 0, 0 }] = true; | ||
| 109 | puzzle[{ width - 1, 0 }] = true; | ||
| 110 | puzzle[{ 0, width - 1 }] = true; | ||
| 111 | puzzle[{ width - 1, width - 1 }] = true; | ||
| 112 | } | ||
| 113 | |||
| 114 | void | ||
| 115 | part2(puzzle_type puzzle) | ||
| 116 | { | ||
| 117 | static const size_t width = 100; | ||
| 118 | |||
| 119 | for ( int i = 0; i != 100; ++i ) { | ||
| 120 | enable_corners(puzzle, width); | ||
| 121 | iterate(puzzle, width); | ||
| 122 | } | ||
| 123 | enable_corners(puzzle, width); | ||
| 124 | |||
| 125 | long sum = 0; | ||
| 126 | for ( size_t line = 0; line != width; ++line ) { | ||
| 127 | for ( size_t column = 0; column != width; ++column ) { | ||
| 128 | auto value = puzzle[{ column, line }]; | ||
| 129 | if ( value ) { | ||
| 130 | ++sum; | ||
| 131 | } | ||
| 132 | } | ||
| 133 | } | ||
| 134 | cout << sum << endl; | ||
| 135 | } | ||
| 136 | |||
| 137 | int | ||
| 138 | main() | ||
| 139 | { | ||
| 140 | auto puzzle = read_file("data/day18.txt"); | ||
| 141 | part1(puzzle); | ||
| 142 | part2(puzzle); | ||
| 143 | } | ||
