Day 15 Part 1

Summary

Determine the some of each string and then add them all together

To determine the value of a string, loop through each character and:

  1. Get the ASCII code for it, add to string total
  2. Multiply string total by 17
  3. Set the total to the remainder of dividing itself by 256
Full challenge: Day 14

Formatting

Read in the raw data, seperate by commas, return the array string[]

1
2
3
4
5
function parse_data(fd: string): string[]{
  fd = fd.trim(); // trim any newlines
  let fd_array: string[] = fd.split(',');
  return fd_array;
}

Calculating

It as easy as it sounds, just follow the steps provided

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
const raw_data: string; // fill from file of fetch
const data: string[] = parse_data(raw_data);

let total_sum: number = 0;

data.forEach((ele) => {
  let local_sum: number = 0;
  for (let i = 0; i < ele.length; i++){
    local_sum += ele.charCodeAt(i);
    local_sum = local_sum * 17;
    local_sum = local_sum % 256;
  };
  total_sum += local_sum;
});
console.log(`Sum => ${total_sum}`);

On to Part 2!

comments powered by Disqus