DEV Community

Cover image for What I've learned from the Advent of Code so far (days 1-5)

What I've learned from the Advent of Code so far (days 1-5)

Minna N. on December 06, 2020

I consider myself an advanced beginner in programming. I lack a lot of knowledge in best practices, gotchas, elegance... let alone algorithms, opti...
Collapse
 
matrixx profile image
Saija Saarenpää

Great post! I haven't had time to participate in the Advent of Code, but I still occasionally enjoy reading from others doing the tasks. I have one suggestion. Not necessarily an improvement, but an alternative way for the for loop for checking the hex code validity. This is also using the includes Ilê Caian already mention about:

function hexValidity(hexValue) {
  let validChars = Array.from('0123456789abcdef');
  let colourArray = Array.from(hexValue);

  if (colourArray[0] != '#' || colourArray.length != 7) {
    return false;
  }

  return colourArray.slice(1).every(character => {
    return validChars.includes(character);
  });
}
Enter fullscreen mode Exit fullscreen mode

The slice function gives a sub array starting from the index 1, so it will omit the # sign. every function for the array iterates over every character of the array and returns true if all of the iterations return true. In case any of the iterations return false, the whole function returns false.

Collapse
 
minna_xd profile image
Minna N.

Ah, clever! Thanks for the code example!

Collapse
 
caiangums profile image
Ilê Caian

Awesome post! I'm also taking adventures at the Advent of Code this year and I think I can give some thoughts too:

  • use of Array.includes(): returns true or false instead of the index
let validColors = ['amb', 'blu', 'brn', 'gry', 'grn', 'hzl', 'oth'];

if (validColors.includes(value)) { ... }
Enter fullscreen mode Exit fullscreen mode
  • you can "empty-split" strings
const validChars = '0123456789abcdef'.split();
Enter fullscreen mode Exit fullscreen mode
  • make use of const on variables that do not change (consider capitalizing some of them too)
const VALID_CHARS = '0123456789abcdef'.split();

if (VALID_CHARS.includes(letter)) { ... }
Enter fullscreen mode Exit fullscreen mode

I would also suggest using some of RegExp here and there. You can make good use of .match() and .replace() where you need to find patterns in repetitive strings. As an example, you can check the usage of .replace() at this solution

Keep coding!! 😄

Collapse
 
minna_xd profile image
Minna N.

Very good tips, thank you! I should've noticed includes and when I can use const. Actually, in my day 7 code (bags...) I use includes!

Thank you also for that code sample! I didn't know you can assign the groups into variables like that. 👍

A question which is either "hey, I'm able to give you a suggestion too!" OR there's some optimization magic going on that I don't know about. About the regex, you have:

/(\d*)-(\d*) (\w): (\w*)/g,

Is there a reason why you use * instead of +? Why not:

/(\d)-(\d) (\w): (\w+)/g,

(or \d+ in case the numbers are double or more digits)

Collapse
 
caiangums profile image
Ilê Caian

Thanks for noticing it! It's true that I could've used + instead of *!

About maintaining \d, I didn't see if the file has values with more than 1 digit, but with * or + will cover it! 😄

It's just a habit using * instead of + and maybe it could be optimized as you said!

Collapse
 
annisalli profile image
Anniina Sallinen

Very nice post, sounds like you have learned a lot! The destructuring is available also in Python and Clojure, but I tend to forget it all the time 🤦‍♀️ For example when solving the Advent of Code puzzles 😄

Collapse
 
syentix profile image
syentix

I don't know why but my first thought with the validation on day 4 was RegEx

Collapse
 
minna_xd profile image
Minna N.

Oh yes, I would've done regex but I didn't know how. 🙈 I'd come across regex in a search and replace usage only. I've improved a lot of my code "retroactively" so I'll probably look into regex solution as well because I like regex. 😄

Collapse
 
minna_xd profile image
Minna N.

Such luck: there is a post titled "How to use Regular Expressions in JavaScript" under Trending on DEV. Gotta check that out.

Thread Thread
 
syentix profile image
syentix

Regex is something yeah :D this site is great for testing out statements, also has useful tips :)

Thread Thread
 
minna_xd profile image
Minna N.

Thanks for the link!

Thread Thread
 
lucasqueiroz profile image
Lucas Queiroz

RegexEr is the best!
I highly recommend learning Regex, it will be very handy one day or another! And I don't even mean the more advanced features, just the basics would be a huge step forward!
You did an awesome job on days 1-5. Hoping to hear again on 6-[...]!

Collapse
 
calvin087 profile image
Calvin Torra

Destructuring........ facepalm for me there too. Would have saved time on that challenge.

Nice one, thanks.