Golfcoder FAQ LOGIN
Error

Advent of Code Leaderboard 2023 / Day 3

View puzzle on adventofcode.com

Submit solution



Rules
  • You're welcome to participate alone or in a team.
  • You may submit multiple solutions and explore different programming languages.
  • Stick to the standard library of your language, no further dependencies/libraries, except the ones which OneCompiler provides (e.g. NumPy for Python).
  • Ensure your code aligns to the template (Python, Rust, Go, Kotlin, JavaScript, C#, TypeScript, C++, Java, C, Swift, Scala, Ruby), reading the puzzle input from stdin (terminated with end-of-file), and printing the solution to stdout.
  • Please refrain from making network requests, reading data from files, or storing data in variable/function/class names for reflection.

Leaderboard

Name Language Tokens Sum Tokens Part 1 Tokens Part 2 Last change
1 Profile imageLiquidFun Python 555 275 280 4 months ago
2 Profile imageValentin Slawicek Kotlin 1198 576 622 5 months ago
3 Profile imageVisible-Bag4062 C++ 10473 473 - 5 months ago

473 tokens in C++ for part 1 by Visible-Bag4062

Download solution

#include<iostream>
#include<map>
#include<numeric>
#include<string>
#include<vector>

usingnamespacestd;

#defineA(i,j)adjs.push_back({i,j});
#defineNif(number.empty())

chardot='.';
typedefarray<uint32_t,2>Pos;

intmain(){
stringline;
vector<string>grid;
while(getline(cin,line))
grid.push_back(line);
Posdim{(uint32_t)grid.size(),(uint32_t)grid[0].size()};
grid.insert(begin(grid),string(dim[1],dot));
for(autoit=begin(grid);it!=end(grid);++it)
*it=dot+*it+dot;

map<Pos,char>symbols;
map<Pos,vector<uint32_t>>numbers;
for(uint32_ti=1;i<dim[0]+1;++i){
vector<char>number;
vector<Pos>adjs;
for(uint32_tj=1;j<dim[1]+2;++j){
autog=grid[i][j];
if(g>47&&g<58){
N{
A(i-1,j-1);
A(i,j-1);
A(i+1,j-1);
}
number.push_back(g);
A(i-1,j);
A(i+1,j);
continue;
}
if(dot!=g)
symbols[{i,j}]=g;
N
continue;
A(i-1,j);
A(i,j);
A(i+1,j);
uint32_tn=0;
for(autoc:number)
n=10*n+c-'0';
for(autoadj:adjs){
autoit=numbers.find(adj);
if(it!=end(numbers))
it->second.push_back(n);
else
numbers[adj]=vector<uint32_t>{n};
}
number.clear();
adjs.clear();
}
}

uint64_tsum=0;
for(autosym:symbols){
autoit=numbers.find(sym.first);
if(it!=end(numbers))
sum+=accumulate(begin(it->second),end(it->second),0);
}
cout<<sum<<endl;
}