# 2024 Day 3: Mull It Over

**URL:** <https://forum.ada-lang.io/t/2024-day-3-mull-it-over/1539>\
**Category:** Advent of Code\
**Created:** [December 3, 2024, 5:51am UTC](https://forum.ada-lang.io/t/2024-day-3-mull-it-over/1539 "2024-12-03T05:51:00Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![JeremyGrosser](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/jeremygrosser/32/3_2.png) [@JeremyGrosser](https://forum.ada-lang.io/u/JeremyGrosser)\
**Post date:** [December 3, 2024, 5:51am UTC](https://forum.ada-lang.io/t/2024-day-3-mull-it-over/1539/1 "2024-12-03T05:51:00Z")

</div>

Maybe I didn’t need to write a whole lexer here, but I did, and I had fun. Reading compiler books over the last year has really made every problem look like a compiler problem.

[advent/2024/src/day3\_2.adb at c50aa046df3ffebd26cf525fd53d6ebb8d89bc25 · JeremyGrosser/advent · GitHub](https://github.com/JeremyGrosser/advent/blob/c50aa046df3ffebd26cf525fd53d6ebb8d89bc25/2024/src/day3_2.adb)

---

<div class="post-metadata">

**Author:** ![cunger](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/cunger/32/322_2.png) [@cunger](https://forum.ada-lang.io/u/cunger)\
**Post date:** [December 3, 2024, 7:29am UTC](https://forum.ada-lang.io/t/2024-day-3-mull-it-over/1539/2 "2024-12-03T07:29:38Z")

</div>

I did the exact opposite and [simply used GNAT.Regpat](https://github.com/cunger/100hoursofada/blob/fc23ee7c9aab6bdf490a8a961eb50796c6423023/aoc2024/src/03/aoc2024_03.adb) 😄

---

<div class="post-metadata">

**Author:** ![jcmoyer](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/jcmoyer/32/373_2.png) [@jcmoyer](https://forum.ada-lang.io/u/jcmoyer)\
**Post date:** [December 3, 2024, 1:33pm UTC](https://forum.ada-lang.io/t/2024-day-3-mull-it-over/1539/3 "2024-12-03T13:33:42Z")

</div>

I also ended up writing a lexer because I was expecting part 2 to add a constraint like “expressions are actually nested.”

[puzzles/AdventOfCode2024/src/day03.adb at 28e953807e27b67d9a5dca2f1901d9d0ed09cdb8 · jcmoyer/puzzles · GitHub](https://github.com/jcmoyer/puzzles/blob/28e953807e27b67d9a5dca2f1901d9d0ed09cdb8/AdventOfCode2024/src/day03.adb)

---

<div class="post-metadata">

**Author:** ![alexispaez](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/alexispaez/32/920_2.png) [@alexispaez](https://forum.ada-lang.io/u/alexispaez)\
**Post date:** [December 3, 2024, 1:54pm UTC](https://forum.ada-lang.io/t/2024-day-3-mull-it-over/1539/4 "2024-12-03T13:54:49Z")

</div>

Completed part one using a sort of automaton like thing, jeez, it seemed like a lot of work! There’s a lot I need to learn to make my life easier, like lexers or Regpat!

[advent-of-code/2024/day\_3\_1/src/day\_3\_1.adb at 4e254b80eb1c4db029385ddb11a89f25f853ba0a · alexispaez/advent-of-code · GitHub](https://github.com/alexispaez/advent-of-code/blob/4e254b80eb1c4db029385ddb11a89f25f853ba0a/2024/day_3_1/src/day_3_1.adb)

Completed part 2, wasn’t so bad. Much better solutions by others… 😅:

[advent-of-code/2024/day\_3\_2/src/day\_3\_2.adb at master · alexispaez/advent-of-code · GitHub](https://github.com/alexispaez/advent-of-code/blob/master/2024/day_3_2/src/day_3_2.adb)

---

<div class="post-metadata">

**Author:** ![alexispaez](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/alexispaez/32/920_2.png) [@alexispaez](https://forum.ada-lang.io/u/alexispaez)\
**Post date:** [December 3, 2024, 2:18pm UTC](https://forum.ada-lang.io/t/2024-day-3-mull-it-over/1539/5 "2024-12-03T14:18:00Z")

</div>

And my approach for part one turns out to be very bad for part two 😆

---

<div class="post-metadata">

**Author:** ![rassoc](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/rassoc/32/519_2.png) [@rassoc](https://forum.ada-lang.io/u/rassoc)\
**Post date:** [December 3, 2024, 3:48pm UTC](https://forum.ada-lang.io/t/2024-day-3-mull-it-over/1539/6 "2024-12-03T15:48:09Z")

</div>

Fiddled around with Ada for far too long on this one, gave up and just wrote it in Ruby. 😫

```ruby
def solve(str) = str
  .scan(/mul\((\d{1,3}),(\d{1,3})\)/)
  .sum { |a, b| a.to_i * b.to_i }

input = File.read("day03.txt")
puts solve input
puts solve input.gsub(/don't\(\)(.*?do\(\)|.*$)/m, "")

```

Respect to those who finished it in Ada.

---

<div class="post-metadata">

**Author:** ![tgv](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/tgv/32/548_2.png) [@tgv](https://forum.ada-lang.io/u/tgv)\
**Post date:** [December 3, 2024, 4:20pm UTC](https://forum.ada-lang.io/t/2024-day-3-mull-it-over/1539/7 "2024-12-03T16:20:19Z")

</div>

I didn’t know about Gnat Regpat (nor about 'Reduce, which was nice for Day 1). Every day a new trick.

So, I also did it with a state machine. I’ve done those quite a few times, so I can sort of write them in my head. Since there were no large overlapping prefixes, it was quite straight-forward ([aoc2024/aoc202403b/src/parse\_mul.adb at 966ee3ba6711a26e15131c2fca18cf8720f6ff2c · theovosse/aoc2024 · GitHub](https://github.com/theovosse/aoc2024/blob/966ee3ba6711a26e15131c2fca18cf8720f6ff2c/aoc202403b/src/parse_mul.adb)). When doing the second problem, I almost gave up, because it becomes quite repetitive. Anyway, that’s also when I discovered I had a potential bug in the state machine of the first solution. Still there, BTW, but it doesn’t affect the solution unless there’s something like “mumul(10,10)” in the input.

---

<div class="post-metadata">

**Author:** ![AJ-Ianozi](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/aj-ianozi/32/39_2.png) [@AJ-Ianozi](https://forum.ada-lang.io/u/AJ-Ianozi)\
**Post date:** [December 3, 2024, 7:17pm UTC](https://forum.ada-lang.io/t/2024-day-3-mull-it-over/1539/8 "2024-12-03T19:17:25Z")

</div>

Great minds think alike, [I solved it almost the exact same way](https://github.com/AJ-Ianozi/adventofcode/blob/548ebe88ec5d2874238534018788598772010ebf/2024/day_03/src/day_03.adb).

---

<div class="post-metadata">

**Author:** ![FredPraca](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/fredpraca/32/192_2.png) [@FredPraca](https://forum.ada-lang.io/u/FredPraca)\
**Post date:** [December 3, 2024, 7:42pm UTC](https://forum.ada-lang.io/t/2024-day-3-mull-it-over/1539/9 "2024-12-03T19:42:26Z")

</div>

[fredpraca][3][Ada][RegPat solution](https://gogs.chickenkiller.fr/fred/advent-of-code-2024/raw/main/day_3/mull_it_over.adb)

This one was easy when you figured out how to make RegPat working

---

<div class="post-metadata">

**Author:** ![jere](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/jere/32/87_2.png) [@jere](https://forum.ada-lang.io/u/jere)\
**Post date:** [December 3, 2024, 8:03pm UTC](https://forum.ada-lang.io/t/2024-day-3-mull-it-over/1539/10 "2024-12-03T20:03:30Z")

</div>

I kind of approached it in a way I know is not scalable, but was easier for me to implement for the exercise. I just used Ada.Strings.Fixed.Index and searched for `"do()"`, `"don't()"`, and `"mul("` (followed by some simple parsing for the rest of `mul`), save their starting locations, sorted the results based on the index I found them at, then ran through the logic with the sorted array (enabling for do, disabling for don’t, multiplying for mul when enabled).

EDIT: Though I kinda guessed at what part 2 required based on a few of the solutions linked here. I don’t have an AoC account setup to see it myself.

---

<div class="post-metadata">

**Author:** ![Irvise](https://forum.ada-lang.io/letter_avatar_proxy/v4/letter/i/7ba0ec/32.png) [@Irvise](https://forum.ada-lang.io/u/Irvise)\
**Post date:** [December 3, 2024, 10:22pm UTC](https://forum.ada-lang.io/t/2024-day-3-mull-it-over/1539/11 "2024-12-03T22:22:27Z")

</div>

I still have part 2 to do, but already solved part one in a way that makes me extremely proud and most people probably think it is quite peculiar but… I used `GNAT.Spitbol.Patterns` and boy, I hate parsing, but once I made it work, my my, it is great!

The code can be summarized to:

```ada

   A_VStr, B_Vstr : VString;
   Digs : constant Pattern := Span("0123456789");
   Multiplicator_Pattern : constant Pattern := "mul(" & Digs **A_VStr & ',' & Digs** B_VStr & ')'; -- Please, read the documentation about GNAT.Spitbol.Patterns as found in g-spipat.ads

      while Match (Input_Line, Multiplicator_Pattern, "") loop
         pragma Debug(Put_Line("Matched Values: A = " & A_VStr & " B = " & B_VStr));
         A := Integer'Value(To_String(A_VStr));
         B := Integer'Value(To_String(B_VStr));
         Accumulator := Accumulator + A * B;
      end loop;

```

---

<div class="post-metadata">

**Author:** ![OneWingedShark](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/onewingedshark/32/305_2.png) [@OneWingedShark](https://forum.ada-lang.io/u/OneWingedShark)\
**Post date:** [December 4, 2024, 2:04am UTC](https://forum.ada-lang.io/t/2024-day-3-mull-it-over/1539/12 "2024-12-04T02:04:43Z")

</div>

`SPITBOL` is vastly unknown/underutilized/underrated.

---

<div class="post-metadata">

**Author:** ![Irvise](https://forum.ada-lang.io/letter_avatar_proxy/v4/letter/i/7ba0ec/32.png) [@Irvise](https://forum.ada-lang.io/u/Irvise)\
**Post date:** [December 6, 2024, 10:25am UTC](https://forum.ada-lang.io/t/2024-day-3-mull-it-over/1539/13 "2024-12-06T10:25:08Z")

</div>

Welp… AoC is telling me that my second solution is wrong… But I visually inspected the first two input lines and the code worked at cleaning the `don't()..do()` pairs… So I do not know why AoC is telling me that my result is wrong… So I give up 😕

If someone wants to take a look at why my code is wrong, feel free. I think my Spitbol solution is the cooler one though 😛

> <https://github.com/Irvise/advent-of-code-2024/blob/6d6157406d9a8a22524f2c5067baaa4e62f26620/day_3/src/main.adb>

Best regards,  
Fer

---

<div class="post-metadata">

**Author:** ![cunger](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/cunger/32/322_2.png) [@cunger](https://forum.ada-lang.io/u/cunger)\
**Post date:** [December 6, 2024, 10:45am UTC](https://forum.ada-lang.io/t/2024-day-3-mull-it-over/1539/14 "2024-12-06T10:45:29Z")

</div>

The `do()` and `don't()` logic applies to the input as a whole, not for each line separately. So if one line ends with

```ada
don't() xxx

```

and the next line starts with

```ada
yyy do() zzz

```

then `yyy` needs to be ignored as well. Seems like you would get this case wrong. (I made the same mistake at first…)

P.S. I agree, the Spitbol pattern solution looks pretty cool. 😄

---

<div class="post-metadata">

**Author:** ![Irvise](https://forum.ada-lang.io/letter_avatar_proxy/v4/letter/i/7ba0ec/32.png) [@Irvise](https://forum.ada-lang.io/u/Irvise)\
**Post date:** [December 6, 2024, 11:00am UTC](https://forum.ada-lang.io/t/2024-day-3-mull-it-over/1539/15 "2024-12-06T11:00:14Z")

</div>

AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

An append on the input (to create a single string) fixed this…

AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

Its done now. Thanks ^^
