Skip to content

Latest commit

 

History

History
57 lines (46 loc) · 2.97 KB

File metadata and controls

57 lines (46 loc) · 2.97 KB

3a

Given the list of ..##...... elements, count how many # you come across when you go down the list and access a character at index I with a specific pattern.

Thoughts

This seems to be rather trivial as well. I think we can fold the list, checking the elements at some index, and increment some counter accordingly. Then also keep track of the current offset and last offset.

In js, fully written out something like (not tested).

arr.shift() // 3 right, 1 down. So
const { trees } = arr.reduce((acc, item) => {
  const hit = item[acc.offset] == "#";
  return {
  ...acc,
    trees: hit ? acc.trees + 1 : acc.trees,
    runningOffset: acc.runningOffset + offset
  }
}, {
  trees: 0
  offset: 3
  runningOffset
})

Lets see if we can do this in Haskell 😅

--

So, I misread the problem. Instead of running out of list towards the right, it's more like an infinite set of repeating items. Not really a problem, doing a modulus on the list lenght will get you the correct index to take.

My solution is slightly hacky though. Because they stipulate a jump to the right by 3, and a jump down by 1, then taking that element. I have not encoded that behaviour at all, but simply go off the fact that in my dataset the element at my initial offset is 0.

3b

As I expected, now it's time to do the actual work, we're going the have to jump in the list. Right now, I think it might be easier to encode all these in some sort of a coordinate system where I would have access to it by x,y, so I can iterate over the numbers. incrementing them as I go, instead of folding the list. Let's have a brew ☕ and a think...

-- So we need to go from this:
-- ...#....#.#...##......#.#...##.
-- .#..#...##..#....##........##..
-- ..##.##...##.#.#....#..#......#
-- ....#....#..#..#.#....#..###...
-- ####.....##.#.##...##..#....#..
-- To something like 
-- 0,1: '.'
-- 0,2: '.'
-- .... 
-- 4,0: '#' 
-- Question

I think, list!!x!!y would give me the same thing. Given the list of lists, I think I should be able to go for that.

Ok. So this went really well. I don't think I've had one of these in the first go, but I did this time around. While not super elegantly written (there is quite a bit of noise), I think it's even a quite performant option. It will only step through the items I give it.

Concluding

Today was a good day 😎 . While my first solution only worked because of some luck, I did manage to write a 'proper' function for 3b. I also for the first time used some custom types for control logic. I use that in ReasonML all the time and it makes the code really nicely self documented. I feel the Match | NoMatch | OutOfBounds is a great example. It allows me to take an unsafe function (!!) and encode all of it's possibly behaviour in my own context (either we match a number, we don't match it, or the index we were accessing was out of bounds / to large for the array). Also annotating some (Int, Int) as Coordinate was nice, as it made it a bit shorter