Day 1: Trebuchet?!
Gabo sent over this Advent of Code thing and I instantly got pulled in and enjoyed solving this. It seems like a great way to practise and learn more which is fun.
I haven't been doing much of this kind of coding recently so I think I was slower to get into this than I am now (writing this in retrospect on day 6).
Part two was more confusing because I originally thought about comparing written numbers with actual numbers whilst analysing their character positions, but soon realised I could just replace the written numbers with actual numbers. Had some issues with that because I missed one of the combined word combinations and didn't find it for ages.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
const allStrings = input.split(/\s+/);
const allNums = [];
allStrings.forEach(function(string){
var convertedString = string;
convertedString = convertedString.replace(/twone/g,'twoone');
convertedString = convertedString.replace(/oneight/g,'oneeight');
convertedString = convertedString.replace(/threeight/g,'threeeight');
convertedString = convertedString.replace(/fiveight/g,'fiveeight');
convertedString = convertedString.replace(/nineight/g,'nineeight');
convertedString = convertedString.replace(/eightwo/g,'eighttwo');
convertedString = convertedString.replace(/eighthree/g,'eightthree');
convertedString = convertedString.replace(/sevenine/g,'sevennine');
convertedString = convertedString.replace(/one/g, 1);
convertedString = convertedString.replace(/two/g, 2);
convertedString = convertedString.replace(/three/g, 3);
convertedString = convertedString.replace(/four/g, 4);
convertedString = convertedString.replace(/five/g, 5);
convertedString = convertedString.replace(/six/g, 6);
convertedString = convertedString.replace(/seven/g, 7);
convertedString = convertedString.replace(/eight/g, 8);
convertedString = convertedString.replace(/nine/g, 9);
// console.log(convertedString);
const chars = convertedString.split('');
var firstNum = null;
var lastNum = null;
chars.forEach(function(char){
// console.log(char);
const isNotNum = isNaN(char);
// console.log(isNotNum);
if( !isNotNum ) {
if( firstNum === null ) {
firstNum = char;
}
lastNum = char;
}
});
var newNum = `${firstNum}${lastNum}`;
newNum = +newNum
// console.log(newNum)
// console.log(typeof newNum)
allNums.push(newNum);
});
// console.log(allNums);
var total = 0;
allNums.forEach(function(num) {
total = total + +num
});
console.log('total',total);