Day 1: Dusty Shed

Link to Day 1

Problem One

In a list of 1,944 three-digit numbers, what do you get if you multiply the three numbers that appear most often in the list? The list will be presented in an HTML <pre> element, with each number separated by a newline.

Solution
  1. The list is currently formatted as a string. Change the data type to an array or a list, depending on the language.
    • I will call this origList.
  2. Duplicate the array and delete the duplicate elements in the new array.
    • I will call this noDups.
  3. Initiate an empty array, if necessary.
    • I will call this counts.
  4. Iterate through noDups. For each item, count how many many of each of that item (number) is in origList.
    4a. Create a new object o. Give it two keys, and assign them the values as follows:
    k: assign the number that was filtered.
    c: assign the count.
    4b. Add the object to the counts array.
  5. Sort the counts array by the value of each key c, from largest to smallest.
  6. Assign a new variable mthree. Give it a value of 1.
  7. Iterate through the first 3 elements of counts, multiplying the value of each k by the value of mthree.
  8. Return the final value of mthree.
const arr = document.querySelector("pre").innerText.split("\n") 
const s = new Set(arr) // duplicate array without duplicate items

var m = []
for (let n of s) {
    var count = arr.filter(item => item === n) 
        var o = {
            k: Number(n)
            c: count
        }
        m.push(o)
} 

var srt = m.sort((a, b) => b.c - a.c); // make sure to sort largest to smallest

var mthree = 1 // assign mthree as 1, not 0 (or else the return value will be 0)

for (let i = 0; i < 3; i++) {
    mthree = mthree * srt[i].k
}

console.log(mthree)
numbers = input.split("/n")
nodups = list(set(numbers)) 

# because there was a final \n there was an empty 
# string that I had to delete from the nodups 
# object because it was throwing me an error as it 
# wouldn't convert to an integer
# I was able to see that it was the first element 
# in the nodups set

del nodups[0]

counts = []
for n in nodups:
    c = numbers.count(n)
    o = {
        "count": c,
        "num": int(n)
    }
    counts.append(o)

# I used a lambda function to sort the counts list

counts.sort(key=lambda u: u['count'], reverse=True) # reverse=True sorts in descending order, so largest to smallest

# Unlike with the JavaScript solution, if I were 
# to initialize a variable with a value of 1 then 
# call a loop to update the value, its value would 
# return to 1 with each iteration of the loop. 
# Instead, we can just multiple the first three 
# values by force. It's not pretty but it works.

counts[0]['num'] * counts[1]['num'] * counts[2]['num']

Problem Two

Now, using the same list of numbers, multiply the three numbers that occur the fewest times in the list.

Solution

The solution is the same as for problem one, except for step 5: instead of sorting the "counts" array from largest to smallest, sort from smallest to largest.

// keeping variables set from previous solution

srt = m.sort((a, b) => a.c - b.c); 

/* check this array -- the index 2 is the empty value we got from the last /n. We need to delete this. */

srt.splice(2,1) // delete the value at index 2, deleting only 1 item in the array

/* from here, the solution is exactly the same */

mthree = 1  // we must reset mthree as 1

for (let i = 0; i < 3; i++) {
    mthree = mthree * srt[i].k
}

console.log(mthree)
# we just need to re-sort our counts list, then the solution is the same

counts.sort(key=lambda u: u['count'])
counts[0]['num'] * counts[1]['num'] * counts[2]['num']