# Help with Advent of Code exercise

**URL:** <https://discourse.julialang.org/t/help-with-advent-of-code-exercise/93641>\
**Category:** New to Julia\
**Tags:** question, advent-of-code\
**Created:** [January 27, 2023, 3:38pm UTC](https://discourse.julialang.org/t/help-with-advent-of-code-exercise/93641 "2023-01-27T15:38:50Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![thetathetatheta](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thetathetatheta/32/45885_2.png) [@thetathetatheta](https://discourse.julialang.org/u/thetathetatheta)\
**Post date:** [January 27, 2023, 3:38pm UTC](https://discourse.julialang.org/t/help-with-advent-of-code-exercise/93641/1 "2023-01-27T15:38:51Z")

</div>

Hi!  
I started to learn Julia by solving Advent of Code exercises from previous years. My background is R but I’m a hobbyist dealing mostly with text mining for fun.  
So far everything is fun and games, I go through AoC 2020 and I’m at [day 6](https://adventofcode.com/2020/day/6). Now, I have following task (paraphrased, hopefully correctly):

1. My input contains chunks of strings in lines.
2. The chunks are separated by empty lines.
3. I’m supposed to count all unique occurrences of lowercase letters for each chunk.
4. And sum up the occurrences for each chunk as the result.

(two first chunks in my input file:)

fekdcbayqxnwvh  
fycwqktvxandeb  
kqbafvcxyewrdn  
akwqcvenxfydbs  
ewbaxdcvnkyfq

timjneyhbvxkfagdpzrous  
gsumijvxoheptbafnkyzrd  
yxtbnupramvdezhkfojsig  
soaruhxnpiemjvytzbfdkg  
vfanlgjoiskzmubtxhceyprd

Guided by the manual and Stack Overflow I ended up with following code. My plan:

1. Create a vector **vec** of vectors (chunks). Nested vectors (chunks) contain strings (lines) of my input.
2. Loop through the elements of nested vectors (strings/lines) and find all occurrences of each letter with _countmap_ function _using StatsBase_. I’ve got a dictionary as a result.
3. For each chunk _merge_ the dictionaries _mapcounted_ for separate line. That will give me occurrences of each letter for a chunk as a merged dictionary.
4. Count elements of merged dictionaries, ignoring number of occurrences. I’m going for number of unique letters.
5. Sum up counted elements of merged dictionaries for each chunk.

My problem - the number I’m getting is too high and I don’t know what’s wrong with my reasoning and/or my code.

The code:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/f/1/f13b58752fdcf05f66ff574aed768d80b032f100.png)

I’d be very grateful if someone could advise me on what I’m doing wrong and push me in the right direction.

---

<div class="post-metadata">

**Author:** ![HanD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hand/32/213908_2.png) [@HanD](https://discourse.julialang.org/u/HanD)\
**Post date:** [January 27, 2023, 4:02pm UTC](https://discourse.julialang.org/t/help-with-advent-of-code-exercise/93641/2 "2023-01-27T16:02:04Z")

</div>

Hello and welcome to the Julia community!

I think you might have overcomplicated the solution slightly. I’m not 100% sure I understand what is going on, but I have a feeling that the bug in this particular solution is that you keep adding unique counts of earlier chunks into the mix by merging `cnt` with `mrg`. Instead, you should simply take the `length` of `cnt` when accumulating the sub-results into `cnt2`.

That being said, I suggest you follow these rules of thumb:

1. avoid using global variables – they are error-prone and bad for the performance (a consequence of how the Julia compiler works);
2. prefer declarative over imperative – the less side effects you have, the less error-prone your code is (and it is also easier to test);
3. prefer built-in and stdlib functions over external libraries.

This particular problem can be solved with the following code:

```plaintext
sum(split(read("aoc2020d06input.txt", String), "\n\n")) do group
    chars_in_group = collect(replace(group, '\n' => ""))
    return length(unique(chars_in_group))
end

```

Taking it apart:

1. `read()` reads the entire contents of a file into a string.
2. `split()` breaks this up into chunks along double line breaks.
3. `replace()` joins the lines of the chunk, `collect()` takes all the characters in the string.
4. `unique()` drops repetitions, and `length()` returns the total (unique) count.
5. The external `sum` sums up the sub-sums for every chunk.

The `do` syntax is a Julia specialty, I suggest you look it up in the manual, it is a real treat.

On a related node, I happen to have solved all the tasks of AoC2020 in Julia. You can find [my solutions on GitHub](https://github.com/dhanak/competitive-coding/tree/master/aoc2020), for reference.

HTH!

---

<div class="post-metadata">

**Author:** ![cjdoris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cjdoris/32/213133_2.png) [@cjdoris](https://discourse.julialang.org/u/cjdoris)\
**Post date:** [January 27, 2023, 4:35pm UTC](https://discourse.julialang.org/t/help-with-advent-of-code-exercise/93641/3 "2023-01-27T16:35:04Z")

</div>

@HanD Can I suggest you put the solution and explanation into a “details” block, since it is a spoiler for the OP.

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [January 28, 2023, 9:03am UTC](https://discourse.julialang.org/t/help-with-advent-of-code-exercise/93641/4 "2023-01-28T09:03:10Z")

</div>

To see what happens to your code, try putting printlns in “appropriate” places.  
In my opinion the problem is the failure to reset the chunk vector, after each subcycle.  
See the difference by leaving the println with and without the statement commented out.

PS  
Next time don’t attach a screenshot, but copy and paste the instructions so that I (or whoever else) doesn’t have to rewrite them from scratch.

```julia
file=open(".\\v1.8.3\\aoc1 2020.txt")
v=Vector{String}[]
chunk=String[]
    for line in eachline(file)
        if line == ""
            push!(v,chunk)
            # chunk=String[]
            println("vector ", v)
        else
            push!(chunk,line)
            println("chunk ", chunk)
        end
    end

push!(v,chunk)
close(file)

```

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [January 28, 2023, 9:07am UTC](https://discourse.julialang.org/t/help-with-advent-of-code-exercise/93641/5 "2023-01-28T09:07:44Z")

</div>

in this way?

> **Summary**
>
> ```julia
> ss=split(read(".\\v1.8.3\\aoc1 2020.txt", String),"\n\n")
> mapreduce(uss->count(islowercase,uss),+, unique.(ss))
> 
> ```

---

<div class="post-metadata">

**Author:** ![thetathetatheta](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thetathetatheta/32/45885_2.png) [@thetathetatheta](https://discourse.julialang.org/u/thetathetatheta)\
**Post date:** [January 30, 2023, 8:28am UTC](https://discourse.julialang.org/t/help-with-advent-of-code-exercise/93641/6 "2023-01-30T08:28:00Z")

</div>

Thank you for the tips, they were very helpful. It seems I have to think a little bit more on handling the problem before I actually start to solve it. Seeing that it can be done in a very simple way is always very inspiring to me.
