I used the same approach. One small simplification you could consider for part B is removing the checkSafe() on line 61 and subsequent if(!safe) block. You can just jump straight into removing the first element and checking if that is safe.
Hi. Thank is sound; if it works without a number, it is okay. So, the first case is not required. Thank you for watching my videos and for being a part of the event. Best regards Daniel
instead of hit and trail method in the main function, I would try another approach to return the index of the item which caused the list to be unsafe from your check function, and then only remove that item from the list and check it again. I think that can also work.
Hi I had a really hard time figuring out which one was the bad number. It might be because I was only comparing two numbers. I did an approach where I tried to have a large window where I checked, and in that case, you might be able to do that. Thank you for watching my videos and being a part of the event. Best regards Daniel
I had trouble getting my brain in gear this morning .... my daughter was sick this weekend (stomach bug), on top of a whole bunch of other "life stuff" that has happened in the last couple of weeks (I took a mental health day off work on Friday), and I think I also had what she had but to a lesser degree. I had no trouble with Part 1, but was having a lot of trouble figuring out Part 2. Your comment about creating an "isValid" function was the tip I needed to go back and solve Part 2. I feel like there should be a cleaner way to do it rather than brute-force remove each element, but it does work in a fast enough time with the test input, so good enough for AoC :) To check for "all increasing or all decreasing", I am checking whether the sorted list is the same as the original list: report.sorted() == report || report.sortedDescending() == report ... which, of course, is only practical because the lists are very short... Otherwise I think my solution ended up being pretty similar to yours (especially after the hint)
Pretty sure I can't share links in YT comments, but I have my code linked in today's stream ☺ I was especially pleased with part 2 as I figured out the minimal checks that needs to be done 😁
I always enjoy your content. This is more fun that an Advent calendar, and lower calories too!
I used the same approach. One small simplification you could consider for part B is removing the checkSafe() on line 61 and subsequent if(!safe) block. You can just jump straight into removing the first element and checking if that is safe.
Hi.
Thank is sound; if it works without a number, it is okay. So, the first case is not required.
Thank you for watching my videos and for being a part of the event.
Best regards
Daniel
instead of hit and trail method in the main function, I would try another approach to return the index of the item which caused the list to be unsafe from your check function, and then only remove that item from the list and check it again. I think that can also work.
Hi
I had a really hard time figuring out which one was the bad number. It might be because I was only comparing two numbers. I did an approach where I tried to have a large window where I checked, and in that case, you might be able to do that.
Thank you for watching my videos and being a part of the event.
Best regards
Daniel
In Kotlin:
Part 1:
fun calc(input: List): Int = input.map { it.split(" ").map { it.toInt() } }.count {
it.windowed(2) { abs(it[0] - it[1]) }.all { it index != r } }
.any { it.windowed(2) { abs(it[0] - it[1]) }.all { it
I didn't really write it like this, I nicely broke it down into functions but above I've just compressed it into one liners. 😉
@@WayneBagguley IMO, Scala one liner looks much better
I had trouble getting my brain in gear this morning .... my daughter was sick this weekend (stomach bug), on top of a whole bunch of other "life stuff" that has happened in the last couple of weeks (I took a mental health day off work on Friday), and I think I also had what she had but to a lesser degree. I had no trouble with Part 1, but was having a lot of trouble figuring out Part 2. Your comment about creating an "isValid" function was the tip I needed to go back and solve Part 2. I feel like there should be a cleaner way to do it rather than brute-force remove each element, but it does work in a fast enough time with the test input, so good enough for AoC :)
To check for "all increasing or all decreasing", I am checking whether the sorted list is the same as the original list:
report.sorted() == report || report.sortedDescending() == report
... which, of course, is only practical because the lists are very short...
Otherwise I think my solution ended up being pretty similar to yours (especially after the hint)
Pretty sure I can't share links in YT comments, but I have my code linked in today's stream ☺ I was especially pleased with part 2 as I figured out the minimal checks that needs to be done 😁
It all depends on the channel. I allow links but I hold them for review. Don't want to spread spam to my viewers.
@@DanielPersson Ah, ok, I just assumed TH-cam filters them all out anyways 😅 I'll post the link in a bit, let me grab it