Created
January 13, 2019 00:15
-
-
Save towc/33551f4c8ce9333a0e0d8de33f064dd0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <fstream> | |
#include <string> | |
#include <vector> | |
struct claim { | |
int id; | |
int x; | |
int y; | |
int w; | |
int h; | |
}; | |
struct cell { | |
int x; | |
int claims; | |
}; | |
claim parseLine(const std::string& line) { | |
// #ID @ X,Y: WxH | |
int pos = 1; | |
int posn = line.find(" "); | |
std::string id = line.substr(pos, posn - pos); | |
pos = posn + 3; | |
posn = line.find(","); | |
std::string x = line.substr(pos, posn - pos); | |
pos = posn + 1; | |
posn = line.find(":"); | |
std::string y = line.substr(pos, posn - pos); | |
pos = posn + 2; | |
posn = line.find("x"); | |
std::string w = line.substr(pos, posn - pos); | |
pos = posn + 1; | |
std::string h = line.substr(pos); | |
return claim{ | |
std::stoi(id), | |
std::stoi(x), | |
std::stoi(y) - 1, | |
std::stoi(w), | |
std::stoi(h) | |
}; | |
} | |
cell* findCell(const int& x, const int& y, std::vector<std::vector<cell*>>& cells) { | |
for(auto const& c: cells[y]) { | |
if (c->x == x) { | |
return c; | |
} | |
} | |
cell c = cell{x,0}; | |
cell* cp = &c; | |
cells[y].push_back(cp); | |
return cp; | |
} | |
int main() { | |
std::string line; | |
std::ifstream inFile; | |
inFile.open("aoc-input-3"); | |
std::vector<claim> claims; | |
int bx = 0; | |
int by = 0; | |
while (std::getline(inFile, line)) { | |
claim c = parseLine(line); | |
if (c.x + c.w > bx) | |
bx = c.x + c.w; | |
if (c.y + c.h > by) | |
by = c.y + c.h; | |
claims.push_back(c); | |
} | |
std::vector<std::vector<cell*>> cells(by); | |
int sum = 0; | |
for(auto const& c: claims) { | |
for(int x = 0; x < c.w; ++x) { | |
for(int y = 0; y < c.h; ++y) { | |
int nx = c.x + x; | |
int ny = c.y + y; | |
cell* c2 = findCell(nx, ny, cells); | |
c2->claims++; | |
if (c2->claims == 2) | |
++sum; | |
} | |
} | |
} | |
std::cout << sum; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const bounds = { | |
x: 0, | |
y: 0 | |
}; | |
const claim = (id, x, y, w, h) => ({ | |
id, | |
x, y, | |
w, h, | |
}); | |
const claims = require('fs').readFileSync('aoc-input-3', 'utf-8').trim() | |
.split('\n') | |
.map((line) => { | |
const [_, id, x, y, w, h] = line.match(/^#(\d+) @ (\d+),(\d+): (\d+)x(\d+)$/).map(Number); | |
bounds.x = Math.max(bounds.x, x + w); | |
bounds.y = Math.max(bounds.y, y + h); | |
return claim(id, x, y - 1, w, h); | |
}); | |
const cells = Array(bounds.y).fill().map(() => []); | |
const get = (x, y) => { | |
let c = cells[y].find(c => c.x === x); | |
if (!c) { | |
c = { x, claims: 0 }; | |
cells[y].push(c); | |
} | |
return c; | |
} | |
let sum = 0; | |
for(const claim of claims) { | |
for(let x = 0; x < claim.w; ++x) { | |
for(let y = 0; y < claim.h; ++y) { | |
const nx = claim.x + x; | |
const ny = claim.y + y; | |
const c = get(nx, ny); | |
c.claims++; | |
if (c.claims === 2) { | |
++sum; | |
} | |
} | |
} | |
}; | |
console.log(sum); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment