Day 3
This commit is contained in:
parent
896bebbc97
commit
a61af3799d
3 changed files with 257 additions and 1 deletions
|
|
@ -12,7 +12,7 @@ if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
|
|||
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3")
|
||||
endif()
|
||||
|
||||
set(executables d1 d2)
|
||||
set(executables d1 d2 d3)
|
||||
|
||||
foreach(executable ${executables})
|
||||
add_executable(${executable} "${executable}/main.cxx")
|
||||
|
|
|
|||
140
d3/input.txt
Normal file
140
d3/input.txt
Normal file
|
|
@ -0,0 +1,140 @@
|
|||
...766.......821.547.....577......................................387.....................56..........446.793..........292..................
|
||||
...........................%...../.....981..........627..../..........-.....623......610..-..............*..................16......891.....
|
||||
...$...........716..&336.......470.325.................*.84........$..34....*.....+.....#.....*76....#.........303.433........-........&....
|
||||
.117../359.#...............595............129..963#..722..........128........192.313........31........887...............234.......-.........
|
||||
............298.....922...*.......482.......*..................*......./.........................395................264..../.......166......
|
||||
.732..................*..815..920*......113.827.........453.571.356..902......693...147............*.....128................................
|
||||
...*..........451-.442..................*...................................+....*....*.......918...680...........................529+......
|
||||
....844.587.....................347...425.....974......348.........$615....174.330..............*..................556.......972*...........
|
||||
..........&...676.........947..%.................*976.*.................45..........192........272.131..............-..977*......85.........
|
||||
.588..........*........$.$.......515...493.............73.....%...........*.....428.*....................*968..............964.......153*274
|
||||
....=..860...157....347............*..*......954.930.......472...618....899......%..726.330..44.......687........$..........................
|
||||
..........@..................+....465..47.......*......*............*........554............@...................485..320....................
|
||||
.....................%831.267.......................305.844.........413........*........741....-...692.948..........*..........650.......510
|
||||
....................................*212....419..............848............710.............670.....*......932...281......-.........398.....
|
||||
.......%....782.......187#..-....890...........@................*../...*531.....745................645......*........#..&..835......*.......
|
||||
.......180..=....153*......487........@322.........693.........805.687..............350.......259........626...849.221.833.......573....%674
|
||||
.....................821...........................*...988.............90..273...................*............*.............................
|
||||
.....65*845..346%..................*412...@.....901.............79.....*.....*..........169.764.14...3.....839...........559..=.............
|
||||
.....................553..798.............318.%..............29......772...................*......................511.........749...697.....
|
||||
.........@............*....*.........414.......13.517.......*...............597.....................28*366..........*......%.........*......
|
||||
......412...........724....973...&....*.............*..#......................%...910...........702......../......473.651...375.*347........
|
||||
.............*559................906..111........449....510......+342............*....797.262.............650............................175
|
||||
...........40.........793...................156......./.....643................577..........+.....................*304...711....437*368.....
|
||||
.........................*.............................843..*...........47..........406............=..../......761.......*...............851
|
||||
......309@......283...296......#.....322............./.....72.....729.....+...515......*..78......735..970.130..........16...../.&...426*...
|
||||
................................720.@......952....958.........672*..................239....*...............+................816..1..........
|
||||
............=..............259........494.+...........................+...447...........310............*...............%553.................
|
||||
....650....999..837*.......+.........*......%359..604.....577.........591.+.......................832.383........@252.........292...........
|
||||
....*...............677..........581.528...........*..=.....%.....................583.........................&........*354.................
|
||||
.................=.........470...*...........=...467...117........$736..3.........*...........................127...945.......808...&....785
|
||||
.747...658.....223...../........563.......466..........................#...652.841......417...........253.................906.*...984.......
|
||||
..........*106.......173....396.....................*106....645...426........*.......*......310.........%.....238*546.....*...442......$145.
|
||||
...618.........&203............*........988......833.................*......594.....837.....*...........................460.......671.......
|
||||
........................*...548.....704*......................274..985.575*...............122.....574......=391.............433%.-..........
|
||||
......871...747.......468................466*367..482....455....*..........53...................&.*.................507@....................
|
||||
........*..*..............597......................*...........855............................502.243.$484......446............-............
|
||||
.......51.............841...+...........7.565.....281.873*305........603................$..................#781...*.819.....591..104....675.
|
||||
............+..........*.......391-......*...........................-....658....515...861.......195/.......................................
|
||||
.............130.903...235..........757................&...765*237.........&....*...........215.......389%............*...269*812...........
|
||||
.526.919...........+.......-.......*........508*202...109............53.......899...........*....@..................55.94..............26...
|
||||
...*....................329........73...........................60=..+.....................10..152........674*993..........74..343..........
|
||||
......305......................902........349...542/.......................783..352...38.....................................=..*....507....
|
||||
.797.....+...............@.......=.......$.............303.348..408............*......-...729......=867.683..+.425*137.........698....&.....
|
||||
....*...........988.964.145.785.....916............*.....#.*...*.....535%.806.532.........*....@.........*..11..............................
|
||||
.684..834...473*.....*.......-.....*........892.372.448.....77.220........*............326..723.........836...........#503...994....*786....
|
||||
.........*..........863..483....773...........*.........101................121..535..................46......706..............*..334........
|
||||
.938*..304..../854......*...........267...462..367........#.......................*..%..............*........*...&...*......631.......384...
|
||||
....................................@.....*......................485..836...197..594..393....433..169.209..13....812..849..........=..../...
|
||||
.....................#.....132........172....419..380..245*860...........*...$.................=......@............................264......
|
||||
....52.555.........602...*........780*..........*...*............................398*245................................61.196.+............
|
||||
......*........@........624...823...............823..770.@....518......663.........................@972.....396*975.......*.....986.....*968
|
||||
.............694.308........................373...........578...#.........................959........................=250............461....
|
||||
..601...............#....719.-.......&.......................................757.............*.........344................660............699
|
||||
.....-.358..480*989...........261.....749......@......-689...209-.......826$...-..194........457..........*...473..........*.........893*...
|
||||
.339......%...........221..................643..564................................*.............443.....798.=.........668..69....@.........
|
||||
....*702..........&........145.734...........@..........$951...............325......439...........&.......................*....151.......436
|
||||
.........#......554.661....%......-....243.....................735.......-.....457.........769*........474*769............547........331....
|
||||
136.361..907.........*........../..........600.....-..739.769..#.........306.....*.............245..............*218..296......683...*......
|
||||
...*..................682...746.765..............681.....*...........511......704.........231................892..................*.715..728
|
||||
.......*415...................*........295..468.......................*.............35...*......./................665.985..&148.........&...
|
||||
....831.......510....444.......565.................3.....334......558..........626........342..532..426......709...#....*.......815.........
|
||||
..............*......*.............................*....*....*...*...............@.....................................418......*......%527.
|
||||
.280#...515..706..305........................344.623...957.803..2..............=...871.............243..989....+..............218...........
|
||||
.........*...............773...../...........@.......$................519/.88..249.+...........449*........#....86....454...2........$437...
|
||||
..........822...197.........*..717......./.......8....562.58*102.936@......*............187...........................@.........212.........
|
||||
..349.719.............728..623...........58.....*........................610....873........@...................................*............
|
||||
...*..*..................%.......423*214......259....426...101.....346............*.+.........359.....813.=...686.....$...639............140
|
||||
..839.768....464.........................../........*.....&...........@....367.951...741..........*....=..283...+..195.....%........632.*...
|
||||
...........#.......................821.893..539..287....................../.....................942...........................149.....*.925.
|
||||
......%....155..........*48.............*............143....../901.............844*368...=...................48....777.........*....246.....
|
||||
....79...............282.............437...............+...........561.....736............133.%.........+......*....*...257*....71..........
|
||||
.............655.131................................................*..........%..............154.......897..791.....29.....358.............
|
||||
...$...966.....*....*....864......892...........661..857=........=..259........645.................................$....630...........134...
|
||||
.806...=.....=....949...............*....#.......*..............531.................410.............................428..*..................
|
||||
..............225............941....570...705....705..36.86...........................%...972/...735....448/............528.................
|
||||
.....253................................................*...506.918.......390.478............................862............................
|
||||
.....*......137......................300..........607.....#....*......663................190................*.....869&..117$................
|
||||
.831.252.......+...%483..110.........*......299.........606..........&..................=................148...24..............147*748...851
|
||||
...........................-.......431.....%....820.........713.219....366.......704...........................*....%...&...............*...
|
||||
.......669.295.......*.................................155..*......#......*......=.....727........494...27...110...588..676..904...345...382
|
||||
......@...........987.829......732/......=.517*519.405.......897........115........716*...................*....................*......*.....
|
||||
..........................740..........713.........*.............978........*................+387.......=..885....-...+968......951....669..
|
||||
....105..............907.-.......105.......807..741.........*....*........627...882...=...........695..779.......248.......421..............
|
||||
...$................$.............*..907*.....*.....*....111.215.89...723........&....546.....849*....................119.....*......644*...
|
||||
.......62.....723......155.923...26......678.341.998.107...................=.........................=.........934.9....*...................
|
||||
....20*.................*...........251..............................470.46../........................31............*...406...441...........
|
||||
........$61......#...628......942......&......255/..926.......................520......47&...............240.......791.......*....875...424.
|
||||
.851.27.......700............*.....234..............-.......91.......=..729......................706...........458.....752...722.*......*...
|
||||
....*...............997....635...........920...*...........*.......266............2................*.............*......=................656
|
||||
.........=...................................57.115.773.944..107.........368...../..205.......971..829.728........566..........75....269....
|
||||
.......538...........646.......706#..762..............*........*.814=...................291..*..............30................*......&......
|
||||
..605*..............*...............*.......226.....709.8...671............159..974#...$.....427.......................645..587.............
|
||||
..........973.866..799......913...741...600*....705...............+........*.............................565..606.........*........654.778..
|
||||
......872....*.....................................#.873.....-225.499..546.847.......763..743@......113.....*.....=....380............*.....
|
||||
.........+......891.53.......206..........760..................................822.....-........136..%.....610.....20......85...............
|
||||
............200..+....*......*....236*511......849+.....672-.&................@..........591*.....*....292......%......176*.......571.......
|
||||
....#15......*......783....512..+....................*........914.86...-216........-574......381.658......*..201....17......758..*......$...
|
||||
..............560................725...628...+...646.432..867.....*.....................................734........*..........*.570....948..
|
||||
....417.....................................992....*............638......696..362...=.......40......................843.....722.............
|
||||
....*......*.................855.169...623......816..816...97.........*....*.....-..998..............................................3......
|
||||
...646..646.389..........................-..............*../.....990...972.92......................333....=..........185..261.400...*...#...
|
||||
.................966.&.............$.........#.....413.700........*...............................&....465...........*......=...+.621....452
|
||||
..873..247........*...212....290....429...551.....=............305....#...719.776....573#....657...............972..477.....................
|
||||
.....*...........725............*.........................405.........952..*.....$..........*.....856.......................295.............
|
||||
.....661.............919*92.....373.917*..794....#............12/..&.......468.......$....841.680.../..303..$..............*....653.197.=...
|
||||
..........730...........................9.#...#..166...206..........794.............891........*.............192.....300...329..+...*....884
|
||||
............/...........*189.....*..........677........+...%.............961.................819....294*............................242.....
|
||||
.....................139......607.41.923....................610..528......*...&455.....70.-.............215.......827&...256....124.........
|
||||
...............342..........................454.................*.......885.&...........*..396....69&....................*........%.........
|
||||
................&...........842...581..335.&.......672.......144.............852.......801...............&60..*............&..........802...
|
||||
..73.133.40....................+...*....%...........+...............248..........769.......511......707*......710..........228..........*...
|
||||
.....*...*.........984.....532....415..................................=.........*...151....*...........467........170..........#....913....
|
||||
..206.....147.....%....338....@..........394.........143%./......958..........749...*....569...................848...#.........268..........
|
||||
..............493......*.............*....................844....*................824...........=...553.......%...........894...............
|
||||
......................38..........796.852...=....955............674.974...................964..551.=............*259..-..*..................
|
||||
...596............857......941...............497...&.564.890.........*..311.905......382...../..........25...329.....241..174......873......
|
||||
...........907.......#........*741.....................*......180.808..*......*........*..........764..........................269*.........
|
||||
...308.......&.569......898........524...452...426..430....34.*.......937...231........752..........&.......111.......................93....
|
||||
....*..........&..............754...*.....*......................409............619................................610...............*......
|
||||
...20...886............788.......&..261.943.............#...#.....................*.../........@......+..137........*..460.205......760.71..
|
||||
........*.........+........837........................184..892...................811..161....648...643.....*........10.......*..............
|
||||
........309.....290..........*...../227.......331...........................659............................876..440.......891.........893...
|
||||
............*..........198...................*.............711*629.............*....837....538..-................*....335.....+.........=...
|
||||
..........923.............*281.330..423...726....530..*280....................990..*.............651..661......121......=.....236...344.....
|
||||
.....419...........$.............*...*..........*.............866..904............96..422.............@......%.....744.....$.......*........
|
||||
.......*..........770......../..843.794..42......201...........-..*.....................*........%.........905........&.....335..865...255..
|
||||
....164....95.814...........300.............*332...................180.....739...=..464.619....617......-.............................#.....
|
||||
.............*.........*.............304..81................../...........*....662...*................658...............368..%...........996
|
||||
.........755........738.329.&866...../.....................614...$.......851.......254......917.152............297..860*.....466...774......
|
||||
..........*................................467.....173.........$.684...........................*.......535.......$....................*.....
|
||||
..960..569.....888..=......................+........*........744.................................882........................975........841..
|
||||
...*..............*.700..........................458....*817......668..........882.710.............#.413%......@.............*..............
|
||||
..648............63...............803.237...341......229.............*..632....*......*910...405................625...........805...&...$...
|
||||
....................../...=............*.........984..........417...78.....*..141.+............*....46..839............786*88......454.289..
|
||||
.....145..=........502..63............111...826....*.../.........*......740.......153.432.....74......*....&.430..............594...........
|
||||
.......*...739..............599.&.............#...454.611........291........196......................172.......*.%434............*..........
|
||||
.........*..............671..&...266............/.......................928.-................................434......387/......16.699......
|
||||
......538.581........&....*............%......10.....168....537&....296..*......177...192................-.......470........................
|
||||
..................661......496.346*.....870............*................958....-......*......-....@......101.....+..........................
|
||||
..808..............................365..................195.........................90......482.837............................404.214......
|
||||
116
d3/main.cxx
Normal file
116
d3/main.cxx
Normal file
|
|
@ -0,0 +1,116 @@
|
|||
#include <iostream>
|
||||
#include <algorithm>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <regex>
|
||||
#include <unordered_map>
|
||||
|
||||
struct Number
|
||||
{
|
||||
int value;
|
||||
int row;
|
||||
int start;
|
||||
int end;
|
||||
};
|
||||
|
||||
struct PairHash
|
||||
{
|
||||
template <class T1, class T2>
|
||||
std::size_t operator()(const std::pair<T1, T2> &p) const
|
||||
{
|
||||
auto h1 = std::hash<T1>{}(p.first);
|
||||
auto h2 = std::hash<T2>{}(p.second);
|
||||
|
||||
return h1 ^ h2;
|
||||
}
|
||||
};
|
||||
|
||||
bool isGear(const char &c)
|
||||
{
|
||||
return c == '*';
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
std::ifstream file{"../d3/input.txt"};
|
||||
std::vector<Number> ns{};
|
||||
std::vector<std::string> inputs{};
|
||||
std::unordered_map<std::pair<int, int>, std::vector<int>, PairHash> gears;
|
||||
std::string line;
|
||||
std::regex pattern("\\d+");
|
||||
int sum{0}, row{0};
|
||||
|
||||
while (std::getline(file, line))
|
||||
{
|
||||
inputs.emplace_back(line);
|
||||
|
||||
auto begin = std::sregex_iterator(line.begin(), line.end(), pattern);
|
||||
auto end = std::sregex_iterator();
|
||||
|
||||
for (std::sregex_iterator i = begin; i != end; i++)
|
||||
{
|
||||
Number n;
|
||||
std::smatch match = *i;
|
||||
n.value = std::stoi(match.str());
|
||||
n.start = match.position();
|
||||
n.end = match.position() + match.str().size() - 1;
|
||||
n.row = row;
|
||||
|
||||
ns.emplace_back(n);
|
||||
}
|
||||
row++;
|
||||
}
|
||||
|
||||
int maxIndex = inputs[0].size() - 1;
|
||||
int inputsSize = inputs.size() - 1;
|
||||
|
||||
for (const auto &n : ns)
|
||||
{
|
||||
int left{std::max(0, n.start - 1)}, right{std::min(n.end + 1, maxIndex)},
|
||||
before{std::max(n.row - 1, 0)}, after{std::min(n.row + 1, inputsSize)};
|
||||
|
||||
if (isGear(inputs[n.row][left]))
|
||||
{
|
||||
gears[std::make_pair(n.row, left)].emplace_back(n.value);
|
||||
continue;
|
||||
}
|
||||
if (isGear(inputs[n.row][right]))
|
||||
{
|
||||
gears[std::make_pair(n.row, right)].emplace_back(n.value);
|
||||
continue;
|
||||
}
|
||||
|
||||
for (auto i{left}; i <= right; i++)
|
||||
{
|
||||
if (isGear(inputs[before][i]))
|
||||
{
|
||||
gears[std::make_pair(before, i)].emplace_back(n.value);
|
||||
continue;
|
||||
}
|
||||
if (isGear(inputs[after][i]))
|
||||
{
|
||||
gears[std::make_pair(after, i)].emplace_back(n.value);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (const auto &gear : gears)
|
||||
{
|
||||
if (gear.second.size() == 2)
|
||||
{
|
||||
int product{1};
|
||||
for (const auto &num : gear.second)
|
||||
{
|
||||
product *= num;
|
||||
}
|
||||
sum += product;
|
||||
}
|
||||
}
|
||||
|
||||
std::cout << sum << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue