midorishibukawa / advent-of-code_ml

moved to gitlab: https://gitlab.com/midorishibukawa/advent-of-code_ml
https://adventofcode.midori.shibukawa.io
GNU General Public License v3.0
0 stars 0 forks source link

solve day 03a (2023) #10

Closed midorishibukawa closed 10 months ago

midorishibukawa commented 10 months ago

https://adventofcode.com/2023/day/3

--- Day 3: Gear Ratios ---

You and the Elf eventually reach a gondola lift station; he says the gondola lift will take you up to the water source, but this is as far as he can bring you. You go inside.

It doesn't take long to find the gondolas, but there seems to be a problem: they're not moving.

"Aaah!"

You turn around to see a slightly-greasy Elf with a wrench and a look of surprise. "Sorry, I wasn't expecting anyone! The gondola lift isn't working right now; it'll still be a while before I can fix it." You offer to help.

The engineer explains that an engine part seems to be missing from the engine, but nobody can figure out which one. If you can add up all the part numbers in the engine schematic, it should be easy to work out which part is missing.

The engine schematic (your puzzle input) consists of a visual representation of the engine. There are lots of numbers and symbols you don't really understand, but apparently any number adjacent to a symbol, even diagonally, is a "part number" and should be included in your sum. (Periods (.) do not count as a symbol.)

Here is an example engine schematic:

467..114..
...*......
..35..633.
......#...
617*......
.....+.58.
..592.....
......755.
...$.*....
.664.598..

In this schematic, two numbers are not part numbers because they are not adjacent to a symbol: 114 (top right) and 58 (middle right). Every other number is adjacent to a symbol and so is a part number; their sum is 4361.

Of course, the actual engine schematic is much larger. What is the sum of all of the part numbers in the engine schematic?

midorishibukawa commented 10 months ago

this one was much harder in thought than in practice, as I spent a long time thinking about it and not actually writing any code (to be honest, I was also pretty distracted with setting up github actions workflows for this project. I also swapped from jane street's core library to ocaml batteries included during the process, which I've been enjoying a lot.

as the input for this one couldn't really be parsed in the same way as I did for the previous challenges, I decided to first split the string on newlines, and then parse it as a list of lists of chars. this way, it's much easier to look all around a digit to check if it's adjacent to any symbols.

the logic goes as follows:

  1. find a digit, and then look all around it for a symbol
  2. if you find a symbol, set the is_valid argument to true
  3. multiply the partial number (default = 0) by 10 and add the digit to it
  4. once you reach the end of the number (any character that isn't a digit, or the end of the file), add the number to the accumulator if the is_valid flag is set to true