# 2023 Day 3: Gear Ratios

**URL:** <https://forum.ada-lang.io/t/2023-day-3-gear-ratios/567>\
**Category:** Advent of Code\
**Created:** [December 3, 2023, 6:37am UTC](https://forum.ada-lang.io/t/2023-day-3-gear-ratios/567 "2023-12-03T06:37:29Z")\
**Posts on this page:** 5\
**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, 2023, 6:37am UTC](https://forum.ada-lang.io/t/2023-day-3-gear-ratios/567/1 "2023-12-03T06:37:29Z")

</div>

Got stuck on part 1 because I had my unit test script using yesterday’s input!  
Part 2 went okay, just required some careful reading.

Nothing about my solution feels optimal in terms of CPU cycles. Looking forward to seeing how everyone else decides to model this one.

---

<div class="post-metadata">

**Author:** ![cantanima](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/cantanima/32/121_2.png) [@cantanima](https://forum.ada-lang.io/u/cantanima)\
**Post date:** [December 3, 2023, 8:38am UTC](https://forum.ada-lang.io/t/2023-day-3-gear-ratios/567/2 "2023-12-03T08:38:53Z")

</div>

I started to use a map, too, but then it occurred to me that an array of booleans would serve the same job, and probably be more efficient. So I took that route. Also, in part 2 I didn’t keep track of all the “spans”; I just summed up values as I went along. So between saving some on the first difference and possibly using more on the second, … who knows how the CPU cycles come out. 😁

---

<div class="post-metadata">

**Author:** ![Max](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/max/32/21_2.png) [@Max](https://forum.ada-lang.io/u/Max)\
**Post date:** [December 3, 2023, 11:12am UTC](https://forum.ada-lang.io/t/2023-day-3-gear-ratios/567/3 "2023-12-03T11:12:57Z")

</div>

My solution is [there](https://github.com/reznikmm/advent-of-ada/blob/df748c9dea702324586ac819a0e1038ca579262f/source/day_03.adb) 😊

---

<div class="post-metadata">

**Author:** ![cantanima](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/cantanima/32/121_2.png) [@cantanima](https://forum.ada-lang.io/u/cantanima)\
**Post date:** [December 3, 2023, 11:09pm UTC](https://forum.ada-lang.io/t/2023-day-3-gear-ratios/567/4 "2023-12-03T23:09:44Z")

</div>

Now includes a [Rust version.](https://github.com/johnperry-math/AoC2023/blob/a34ab78a2adc3a080aed98e47ecc08785f8bb21f/day3/src/main.rs) Eventually I’ll get to a Modula-2 version, but a few remarks on my frustrations with Rust – which **really** surprised me:

- 💔 I **really** missed having access to custom arrays, and it’s also a lot harder in Rust to check indices. In Ada, I can do this:

- 😱 There’s also the annoyance (which made me waste quite a bit of time debugging) where `(-1..1)` indicates the numbers -1 and 0, **not** 1. This is why I use `(-1..2)` above.

- ❤ I do like the use of functional programming / “combinators” in Rust.

All in all, I wasn’t expecting the Rust to be harder to write and more annoying to look at and debug than the Ada. _Maybe_ it’s due in part to my starting from Ada, and _probably_ it’s due in large part to my not thinking things through as well as I should (I’ll just translate the Ada to Rust today; how hard can that be? )

---

<div class="post-metadata">

**Author:** ![cantanima](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/cantanima/32/121_2.png) [@cantanima](https://forum.ada-lang.io/u/cantanima)\
**Post date:** [December 4, 2023, 5:37am UTC](https://forum.ada-lang.io/t/2023-day-3-gear-ratios/567/5 "2023-12-04T05:37:13Z")

</div>

Updated with [a Modula-2 implementation](https://github.com/johnperry-math/AoC2023/blob/37197433846fcd85e5d8a68d6766561d7195bae8/day3/src/day3.mod). (I haven’t yet implemented the solution to day 2 in M2.) I’m definitely appreciating some of Ada’s safety features, since the lack of them in Modula-2 hammered me a few times. For example:

- ⚠ You can define a Modula-2 variable to be of a certain range type, but Modula-2 won’t choke if it goes outside that range the way Ada will – unless this is a bug in gm2. In particular, this sort of thing passes muster in gm2 and works the way I wanted, even though it understandably raised a run-time error in Ada: the loop can only end if `Left > MAX(Constraints)`, but if that happens then `Left` by definition no longer has a valid value for its type, and a run-time error _should_ be raised, even in Modula-2 (?). I haven’t yet found a clear answer in t[he reference I’m using](https://www.modula2.org/reference/index.php).

- ❓ Can’t seem to define a constant type of a variant record. I’m not sure if that’s a misunderstanding on my part, or a bug in gm2.

- 😱 I’m pretty sure this is a bug: if you neglect the parentheses on a function procedure that takes no parameters, gm2 treats it as if you want the address(?). Hence, the following line of the module repeatedly gave the wrong answer:
