Advent of Code 2023 Day 6: Next higher integer

December 6, 2023
aoc-2023 rust

This is part of a series on little things I learned while doing the problems from Advent of Code 2023. It won’t fully explain any problems or solutions from AOC. You can find all my solutions on Github.

A small programming technique tidbit I hadn’t encountered before: when finding solutions to strict inequalities (e.g. find the smallest integer x > y where y is floating point), if your program solves for a floating point x == y1, how do you find the next greater integer than x to satisfy the inequality?

My first instinct was that it’s simply ceil(x), but that’s actually wrong — if x is already an integer, then ceil(x) == x == y does not satisfy x > y. Instead, you do floor(x) + 1, to guarantee that the result is 1. greater than y, and 2. an integer.

Similarly when you need integral x < y, you can do ceil(x) - 1.

  1. to make it more concrete, the AoC problem involved solving for the roots of a quadratic equation, and finding the integral solutions strictly between them.