# 2022 Day 4: Camp Cleanup

**URL:** https://forum.ada-lang.io/t/2022-day-4-camp-cleanup/206
**Category:** Advent of Code
**Tags:** spoilers
**Created:** [December 4, 2022, 5:17am UTC](https://forum.ada-lang.io/t/2022-day-4-camp-cleanup/206 "2022-12-04T05:17:44Z")
**Posts on this page:** 17
**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 4, 2022, 5:17am UTC](https://forum.ada-lang.io/t/2022-day-4-camp-cleanup/206/1 "2022-12-04T05:17:44Z")

</div>

I definitely wasted some time overengineering this one. I was expecting part 2 to have more than two ranges per line and wrote a parser to handle that. I also made the parser do no dynamic allocation, because my I/O library supports that now.

[advent/day4\_2.adb at 3886b077597131c55f15bd23bf3f5dc14faeb4c8 · JeremyGrosser/advent · GitHub](https://github.com/JeremyGrosser/advent/blob/3886b077597131c55f15bd23bf3f5dc14faeb4c8/2022/src/day4_2.adb)

---

<div class="post-metadata">

### Author: ![zertovitch](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/zertovitch/32/90_2.png) [@zertovitch](https://forum.ada-lang.io/u/zertovitch)
#### Post date: [December 4, 2022, 6:06am UTC](https://forum.ada-lang.io/t/2022-day-4-camp-cleanup/206/2 "2022-12-04T06:06:50Z")

</div>

Gained lots of time skipping much of the explanation… and also lost lots of time doing so, because I thought the format was:

```auto
.234..... 2-4
.....678. 6-8

```

instead of:

```auto
2-4,6-8

```

🤬  
Anyway, the “final” code is here: [hac/aoc\_2022\_04.adb at 74dd26dbb513dec406561fa5e8cc439f6abe802c · zertovitch/hac · GitHub](https://github.com/zertovitch/hac/blob/74dd26dbb513dec406561fa5e8cc439f6abe802c/exm/aoc/2022/aoc_2022_04.adb)

---

<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 4, 2022, 6:07am UTC](https://forum.ada-lang.io/t/2022-day-4-camp-cleanup/206/3 "2022-12-04T06:07:17Z")

</div>

It’s been said that Advent of Code is really a reading comprehension test, not a programming test 🙂

---

<div class="post-metadata">

### Author: ![zertovitch](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/zertovitch/32/90_2.png) [@zertovitch](https://forum.ada-lang.io/u/zertovitch)
#### Post date: [December 4, 2022, 6:39am UTC](https://forum.ada-lang.io/t/2022-day-4-camp-cleanup/206/4 "2022-12-04T06:39:08Z")

</div>

A way of avoiding the format issue is to download input.txt (and have a glimpse on it) early in the process…

---

<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, 2022, 7:42am UTC](https://forum.ada-lang.io/t/2022-day-4-camp-cleanup/206/5 "2022-12-04T07:42:43Z")

</div>

> [@JeremyGrosser](#):
>
> I definitely wasted some time overengineering this one.

Really? You were still done before I finished part 1 – I checked the leaderboard out of curiosity – and I think I finished both parts quicker than I finished part 1 on either of the previous 3 days!

This year seems a bit unusual: the first day has been the hardest to me, and they’ve gotten easier since – so much so that on Days 3 and 4 I implemented alternate solutions. I’m waiting for the other shoe to drop.

---

<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, 2022, 7:44am UTC](https://forum.ada-lang.io/t/2022-day-4-camp-cleanup/206/6 "2022-12-04T07:44:06Z")

</div>

> [@zertovitch](#):
>
> A way of avoiding the format issue is to download input.txt (and have a glimpse on it) early in the process…

Yeah, I learned that during my first AoC back in 2020… 😁

---

<div class="post-metadata">

### Author: ![zertovitch](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/zertovitch/32/90_2.png) [@zertovitch](https://forum.ada-lang.io/u/zertovitch)
#### Post date: [December 4, 2022, 2:40pm UTC](https://forum.ada-lang.io/t/2022-day-4-camp-cleanup/206/7 "2022-12-04T14:40:11Z")

</div>

I think I also did, but forgot again.  
Now I have set up a [check list](https://github.com/zertovitch/hac/blob/621eb92e24466b15f0973803940692e593089154/exm/aoc/aoc_check_list.txt).  
Can be useful for those who are in a time zone where the puzzle pops up early in the morning…

---

<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, 2022, 4:33pm UTC](https://forum.ada-lang.io/t/2022-day-4-camp-cleanup/206/8 "2022-12-04T16:33:48Z")

</div>

If step 1 of a checklist **_must_** say “coffee” then you might be rising too early. 😉

---

<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 4, 2022, 5:02pm UTC](https://forum.ada-lang.io/t/2022-day-4-camp-cleanup/206/9 "2022-12-04T17:02:32Z")

</div>

I kind of wanted to create dynamically ranged arrays to check against, but actually have errands today so if statement it is!  
This is what I came up with [adventofcode/day4.adb at bee3c2dc4fd0bf7e6c1d8916f529dc30cf5beee1 · AJ-Ianozi/adventofcode · GitHub](https://github.com/AJ-Ianozi/adventofcode/blob/bee3c2dc4fd0bf7e6c1d8916f529dc30cf5beee1/2022/src/day4.adb)

---

<div class="post-metadata">

### Author: ![captain-haddock17](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/captain-haddock17/32/186_2.png) [@captain-haddock17](https://forum.ada-lang.io/u/captain-haddock17)
#### Post date: [December 4, 2022, 5:49pm UTC](https://forum.ada-lang.io/t/2022-day-4-camp-cleanup/206/10 "2022-12-04T17:49:08Z")

</div>

Curious …  
Anybody tried to read the file as a record of data ?  
using `Ada.Text_IO.Text_Streams` and `Ada.Streams` to read each record (line) of data, implementing `Data_Record'Read (Data_Stream, Data);`  
See [my puzzle\_04](https://github.com/captain-haddock17/Advent_of_Code_2022/tree/main/puzzle_04)

Seems you all used plain Text\_IO. 😲  
Why not ? Pro/Cons ?

(Jeremy did still used a specific generic Stream library)

Anybody ? 👋

---

<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 4, 2022, 5:56pm UTC](https://forum.ada-lang.io/t/2022-day-4-camp-cleanup/206/11 "2022-12-04T17:56:33Z")

</div>

I used streams for all of my solutions last year and started to this year. However, yesterday I rewrote my I/O library to use mmap for the input file. This is much faster, but means my `Read_Until` helper isn’t compatible with the Root\_Stream\_Type interface anymore.

---

<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 4, 2022, 8:24pm UTC](https://forum.ada-lang.io/t/2022-day-4-camp-cleanup/206/12 "2022-12-04T20:24:15Z")

</div>

But you ignore `Stream` and read from Ada.Integer\_Text\_IO.Get also, no?

---

<div class="post-metadata">

### Author: ![RREE](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/rree/32/34_2.png) [@RREE](https://forum.ada-lang.io/u/RREE)
#### Post date: [December 4, 2022, 8:46pm UTC](https://forum.ada-lang.io/t/2022-day-4-camp-cleanup/206/13 "2022-12-04T20:46:37Z")

</div>

I used `subtype`s to check for overlaps in the second part:

```ada
   function Overlap (Left, Right : Sect_Range) return Boolean is
      subtype Left_Range is Natural range Left.Low .. Left.High;
      subtype Right_Range is Natural range Right.Low .. Right.High;
   begin
      if Left.Low in Right_Range or else Left.High in Right_Range then
         return True;
      elsif Right.Low in Left_Range or else Right.High in Left_Range then
         return True;
      else
         return False;
      end if;
   end Overlap;

```

---

<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, 2022, 8:50pm UTC](https://forum.ada-lang.io/t/2022-day-4-camp-cleanup/206/14 "2022-12-04T20:50:09Z")

</div>

> [@captain-haddock17](#):
>
> Seems you all used plain Text\_IO. 😲  
> Why not ? Pro/Cons ?

As an Ada dilettante\*\*\*, I know `Text_IO`. I am not familiar with `Text_Streams`. I actually remembering thinking last night that it would be nice to have a library that parses a line of input according to a known structure. How convenient is it to use and implement `Text_Streams` and `Data_Record'Read`?

\*\*\*Calling me an amateur or hobbyist is an insult to amateurs and hobbyists.

---

<div class="post-metadata">

### Author: ![captain-haddock17](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/captain-haddock17/32/186_2.png) [@captain-haddock17](https://forum.ada-lang.io/u/captain-haddock17)
#### Post date: [December 5, 2022, 5:38pm UTC](https://forum.ada-lang.io/t/2022-day-4-camp-cleanup/206/15 "2022-12-05T17:38:31Z")

</div>

I thought I could still do those elementary types with `read (Stream, ...)`, but I didn’t succeed.  
Isn’t it (IMHO) the conventional way of reading elementary types? (with `get`) so as to cope with variable integer length?

I’ll retry …

---

<div class="post-metadata">

### Author: ![zertovitch](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/zertovitch/32/90_2.png) [@zertovitch](https://forum.ada-lang.io/u/zertovitch)
#### Post date: [December 5, 2022, 8:38pm UTC](https://forum.ada-lang.io/t/2022-day-4-camp-cleanup/206/16 "2022-12-05T20:38:14Z")

</div>

Well this game’s data are in plain text files. So why not using Text\_IO?

---

<div class="post-metadata">

### Author: ![captain-haddock17](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/captain-haddock17/32/186_2.png) [@captain-haddock17](https://forum.ada-lang.io/u/captain-haddock17)
#### Post date: [December 6, 2022, 5:39pm UTC](https://forum.ada-lang.io/t/2022-day-4-camp-cleanup/206/17 "2022-12-06T17:39:31Z")

</div>

I like to design software as the domain specification is described. (say “O.O.”)  
With Ada, one may easily implement this specification directly in code. unlike C.  
IMHO, you get super readability 🕶

Using `Text_IO.Streams` gives me the opportunity to get an Ada data record (filled-up) per line, just by being (almost) _declarative_ in the code.
