14
submitted 10 months ago* (last edited 10 months ago) by CameronDev@programming.dev to c/advent_of_code@programming.dev

Day 21: Step

Megathread guidelines

  • Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
  • You can send code in code blocks by using three backticks, the code, and then three backticks or use something such as https://topaz.github.io/paste/ if you prefer sending it through a URL

FAQ

you are viewing a single comment's thread
view the rest of the comments
[-] sjmulder 1 points 10 months ago* (last edited 10 months ago)

C

That did not go accordingly to plan! Took hours and a bunch of notepad paper 😅

Major mistakes:

  • Off by one reading input so I was missing a row.
  • Messing up the area calculations for certain cases, like double-counting some diagonals or not counting them. Suppose those are off by ones too.
  • Not realizing the odd grid size meant the tick/tock states would alternate between repetitions of the grid within the covered area.

Some code:

static char map[GSZ][GSZ];	/* all reachable replaced by \0 */
static int nobs[2][2];		/* [in/out diamond][even/odd t] */
static int sz;

/*
 * We call the big diamond in the input (covered by part 1) the 'inside 
 * diamond' and the diamond found by combining the corner pieces on the
 * 'outside diamond'. For both we found the number of unreachable tiles
 * (obstacles, 'nobs') for the even and odd time steps.
 *
 * The total number of tiles covered in t is (t+1)^2 - accounting for
 * the checkerboard reachability pattern. With the part 1 and 2
 * questions, this exactly covers a certain square amount of diamonds
 * (ndia*ndia).
 *
 * Finding out which types of diamonds and in what state (odd/even) is
 * the tricky bit, e.g. the state alternates between the repeating grids
 * because the grid size is uneven.
 */
static uint64_t find_reach(uint64_t t)
{
	int odd = t&1;
	uint64_t ndia = t/sz*2 +1;

	return (t+1)*(t+1) -
	    (ndia/2 +!odd) * (ndia/2 +!odd) * nobs[0][0] -
	    (ndia/2 + odd) * (ndia/2 + odd) * nobs[0][1] -
	    ndia*ndia/4 * nobs[1][0] -
	    ndia*ndia/4 * nobs[1][1];
}

https://github.com/sjmulder/aoc/blob/master/2023/c/day21.c

this post was submitted on 21 Dec 2023
14 points (93.8% liked)

Advent Of Code

736 readers
2 users here now

An unofficial home for the advent of code community on programming.dev!

Advent of Code is an annual Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like.

AoC 2023

Solution Threads

M T W T F S S
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25

Rules/Guidelines

Relevant Communities

Relevant Links

Credits

Icon base by Lorc under CC BY 3.0 with modifications to add a gradient

console.log('Hello World')

founded 1 year ago
MODERATORS