Skip to content

Commit

Permalink
Solution day 1
Browse files Browse the repository at this point in the history
  • Loading branch information
MinThaMie committed Dec 1, 2023
1 parent 133915e commit 05c4676
Show file tree
Hide file tree
Showing 5 changed files with 2,063 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@ insert_final_newline = false

[*.{diff,md}]
trim_trailing_whitespace = false

[*.txt]
insert_final_newline = false
55 changes: 53 additions & 2 deletions app/controllers/puzzles/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,64 @@ import PuzzlesBaseController from './base';
export default class Puzzles1Controller extends PuzzlesBaseController {
// BEGIN-SNIPPET day1-solution1
solve1(input) {
return 'Solution 1';
const solution = input.reduce(
(accumulator, currentValue) =>
accumulator +
parseInt(
`${currentValue.replace(/\D/g, '').slice(0, 1)}${currentValue
.replace(/\D/g, '')
.slice(-1)}`,
),
0,
);
return solution;
}
// END-SNIPPET

// BEGIN-SNIPPET day1-solution2
solve2(input) {
return 'Solution 1';
const digits = [
'one',
'two',
'three',
'four',
'five',
'six',
'seven',
'eight',
'nine',
];
let solution = 0;
input.map((e) => {
let numbers = {};
let positions = [];
let realDigits = e.replace(/\D/g, '').split('');
realDigits.forEach((d) => {
let pos = e.indexOf(d);
if (positions.includes(pos)) {
pos = e.lastIndexOf(d);
}
positions.push(pos);
numbers[pos] = d;
});
digits.forEach((v, i) => {
if (e.includes(v)) {
let firstPos = e.indexOf(v);
let lastPos = e.lastIndexOf(v);
if (firstPos !== lastPos) {
numbers[lastPos] = i + 1;
positions.push(lastPos);
}
positions.push(firstPos);
numbers[firstPos] = i + 1;
}
});
positions.sort((a, b) => a > b);
solution += parseInt(
`${numbers[positions.slice(0, 1)]}${numbers[positions.slice(-1)]}`,
);
});
return solution;
}
// END-SNIPPET
}
Loading

0 comments on commit 05c4676

Please sign in to comment.