Golfcoder FAQ LOGIN
Error

Advent of Code Leaderboard 2024 / Day 12

View puzzle on adventofcode.com

Submit solution



Leaderboard

Name Language Tokens Sum Tokens Part 1 Tokens Part 2 Last change
1 Profile imageMichael Böiers Kotlin 10215 215 - 9 days ago
2 Profile imagedbasden C 10344 344 - 9 days ago
3 Profile imageMechazawa Rust 11183 1183 - 9 days ago

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, Bash), 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.
  • Your code must be able to process all valid Advent of Code inputs. Golfcoder might reevaluate correctness of your solution with different inputs after your submission.

344 tokens in C for part 1 by dbasden

Download solution

#include<stdio.h>

chargrid[40960];
intreplanted_grid[40960];
inti,j,width;
longinttotal;

intarea[1024],perimeter[1024];

staticvoidreplant(intpos,intfrom,intto){
if(grid[pos]!=from)return;
replanted_grid[pos]=to;
grid[pos]+=32;
replant(pos-144,from,to);replant(pos-1,from,to);
replant(pos+144,from,to);replant(pos+1,from,to);
}
staticintreplant_everything(){
intnext_colour=1;
for(i=145;i<40815;++i){
intch=grid[i];
if(ch>='A'&&ch<='Z')replant(i,ch,next_colour++);
}
returnnext_colour;
}

intmain(){
char*p=&(grid[1]);
while(scanf("%s%n",(char*)(p+=144),&width)==1);
intmax_ch=replant_everything();
for(i=144+1;i<40960-144-1;++i){
intch=replanted_grid[i];
if(replanted_grid[i]){
area[ch]++;
perimeter[ch]+=(replanted_grid[i-144]!=ch)+(replanted_grid[i-1]!=ch)+(replanted_grid[i+144]!=ch)+(replanted_grid[i+1]!=ch);
}
}
for(intch=1;ch<max_ch;ch++)if(area[ch])total+=area[ch]*perimeter[ch];
printf("%ld\n",total);
}