Skip to content

Commit

Permalink
moving to let instead of const
Browse files Browse the repository at this point in the history
  • Loading branch information
shahata committed May 5, 2024
1 parent 1979353 commit 705e5f2
Show file tree
Hide file tree
Showing 63 changed files with 398 additions and 224 deletions.
13 changes: 9 additions & 4 deletions src/2015/day01.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
const parse = input => input.split('').map(x => (x === '(' ? 1 : -1));
function parse(input) {
return input.split('').map(x => (x === '(' ? 1 : -1));
}

export const part1 = input => parse(input).reduce((sum, x) => sum + x);
export function part1(input) {
return parse(input).reduce((sum, x) => sum + x);
}

export const part2 = input =>
parse(input).reduce(
export function part2(input) {
return parse(input).reduce(
(state, x, index) => ({
sum: state.sum + x,
marker: state.marker || (state.sum + x === -1 ? index + 1 : undefined),
}),
{ sum: 0, marker: undefined },
).marker;
}
14 changes: 9 additions & 5 deletions src/2015/day02.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
const parse = input => input.split('\n').map(x => x.split('x').map(Number));
function parse(input) {
return input.split('\n').map(x => x.split('x').map(Number));
}

export const part1 = input =>
parse(input)
export function part1(input) {
return parse(input)
.map(x => [x[0] * x[1], x[1] * x[2], x[0] * x[2]])
.map(x => 2 * (x[0] + x[1] + x[2]) + Math.min(...x))
.reduce((prev, item) => prev + item);
}

export const part2 = input =>
parse(input)
export function part2(input) {
return parse(input)
.map(x => 2 * (x[0] + x[1] + x[2] - Math.max(...x)) + x[0] * x[1] * x[2])
.reduce((prev, item) => prev + item);
}
6 changes: 3 additions & 3 deletions src/2015/day03.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function parse(input) {
export function part1(input) {
return parse(input).reduce(
(state, next) => {
const pos = (state.pos = next(state.pos));
let pos = (state.pos = next(state.pos));
state.visited.add(`${pos.x}-${pos.y}`);
return state;
},
Expand All @@ -27,8 +27,8 @@ export function part1(input) {
export function part2(input) {
return parse(input).reduce(
(state, next, index) => {
const turn = index % 2 === 0 ? 'santa' : 'robot';
const pos = (state.pos[turn] = next(state.pos[turn]));
let turn = index % 2 === 0 ? 'santa' : 'robot';
let pos = (state.pos[turn] = next(state.pos[turn]));
state.visited.add(`${pos.x}-${pos.y}`);
return state;
},
Expand Down
9 changes: 7 additions & 2 deletions src/2015/day04.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,10 @@ function solve(input, prefix) {
return result;
}

export const part1 = input => solve(input, '00000');
export const part2 = input => solve(input, '000000');
export function part1(input) {
return solve(input, '00000');
}

export function part2(input) {
return solve(input, '000000');
}
10 changes: 6 additions & 4 deletions src/2015/day05.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
export const part1 = input =>
input
export function part1(input) {
return input
.split('\n')
.filter(x => (x.match(/[aeiou]/g) || []).length >= 3)
.filter(x => x.match(/([a-z])\1/))
.filter(x => !x.match(/ab|cd|pq|xy/)).length;
}

export const part2 = input =>
input
export function part2(input) {
return input
.split('\n')
.filter(x => x.match(/([a-z][a-z]).*\1/))
.filter(x => x.match(/([a-z]).\1/)).length;
}
10 changes: 6 additions & 4 deletions src/2015/day06.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,18 @@ function countLights(input, operations) {
.reduce((sum, row) => sum + row.reduce((sum, x) => sum + x), 0);
}

export const part1 = input =>
countLights(input, {
export function part1(input) {
return countLights(input, {
'turn on': () => 1,
'turn off': () => 0,
'toggle': val => (val === 0 ? 1 : 0),
});
}

export const part2 = input =>
countLights(input, {
export function part2(input) {
return countLights(input, {
'turn on': val => val + 1,
'turn off': val => Math.max(val - 1, 0),
'toggle': val => val + 2,
});
}
24 changes: 14 additions & 10 deletions src/2015/day07.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const ops = {
AND: (p1, p2) => (Math.pow(2, 16) + (p1 & p2)) % Math.pow(2, 16),
OR: (p1, p2) => (Math.pow(2, 16) + (p1 | p2)) % Math.pow(2, 16),
NOT: (p1, p2) => (Math.pow(2, 16) + ~p2) % Math.pow(2, 16),
LSHIFT: (p1, p2) => (Math.pow(2, 16) + (p1 << p2)) % Math.pow(2, 16),
RSHIFT: (p1, p2) => (Math.pow(2, 16) + (p1 >> p2)) % Math.pow(2, 16),
undefined: p1 => (Math.pow(2, 16) + p1) % Math.pow(2, 16),
AND: (p1, p2) => (2 ** 16 + (p1 & p2)) % 2 ** 16,
OR: (p1, p2) => (2 ** 16 + (p1 | p2)) % 2 ** 16,
NOT: (p1, p2) => (2 ** 16 + ~p2) % 2 ** 16,
LSHIFT: (p1, p2) => (2 ** 16 + (p1 << p2)) % 2 ** 16,
RSHIFT: (p1, p2) => (2 ** 16 + (p1 >> p2)) % 2 ** 16,
undefined: p1 => (2 ** 16 + p1) % 2 ** 16,
};

function getter(id) {
Expand All @@ -24,14 +24,18 @@ function makeCircuit(input) {
}))
.reduce((circuit, gate) => {
circuit[gate.result] = () => {
const memo = gate.op(gate.p1(circuit), gate.p2(circuit));
let memo = gate.op(gate.p1(circuit), gate.p2(circuit));
circuit[gate.result] = () => memo;
return memo;
};
return circuit;
}, {});
}

export const part1 = input => makeCircuit(input.split('\n')).a();
export const part2 = input =>
makeCircuit(input.split('\n').concat(`${part1(input)} -> b`)).a();
export function part1(input) {
return makeCircuit(input.split('\n')).a();
}

export function part2(input) {
return makeCircuit(input.split('\n').concat(`${part1(input)} -> b`)).a();
}
10 changes: 6 additions & 4 deletions src/2015/day08.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
export const part1 = input =>
input
export function part1(input) {
return input
.split('\n')
.map(x => x.length - eval(x).length)
.reduce((sum, x) => sum + x);
}

export const part2 = input =>
input
export function part2(input) {
return input
.split('\n')
.map(x => JSON.stringify(x).length - x.length)
.reduce((sum, x) => sum + x);
}
15 changes: 10 additions & 5 deletions src/2015/day09.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function shortest(graph, curr, visited) {
const paths = Object.keys(graph[curr])
let paths = Object.keys(graph[curr])
.filter(node => visited.indexOf(node) === -1)
.map(
node => graph[curr][node] + shortest(graph, node, visited.concat(curr)),
Expand All @@ -8,7 +8,7 @@ function shortest(graph, curr, visited) {
}

function longest(graph, curr, visited) {
const paths = Object.keys(graph[curr])
let paths = Object.keys(graph[curr])
.filter(node => visited.indexOf(node) === -1)
.map(
node => graph[curr][node] + longest(graph, node, visited.concat(curr)),
Expand All @@ -17,7 +17,7 @@ function longest(graph, curr, visited) {
}

function parse(input) {
const graph = input
let graph = input
.split('\n')
.map(x => x.match(/^(.*) to (.*) = (\d+)$/))
.map(([, p1, p2, d]) => ({ p1, p2, d: +d }))
Expand All @@ -33,5 +33,10 @@ function parse(input) {
return graph;
}

export const part1 = input => shortest(parse(input), '$$start', []);
export const part2 = input => longest(parse(input), '$$start', []);
export function part1(input) {
return shortest(parse(input), '$$start', []);
}

export function part2(input) {
return longest(parse(input), '$$start', []);
}
11 changes: 7 additions & 4 deletions src/2015/day10.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ function transform(s) {
.join('');
}

export const part1 = (input, times = 40) =>
new Array(times).fill().reduce(prev => transform(prev), input).length;
export const part2 = (input, times = 50) =>
new Array(times).fill().reduce(prev => transform(prev), input).length;
export function part1(input, times = 40) {
return new Array(times).fill().reduce(prev => transform(prev), input).length;
}

export function part2(input, times = 50) {
return new Array(times).fill().reduce(prev => transform(prev), input).length;
}
9 changes: 7 additions & 2 deletions src/2015/day11.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,10 @@ function next(password) {
return s.toString(36);
}

export const part1 = input => next(input);
export const part2 = input => next(next(input));
export function part1(input) {
return next(input);
}

export function part2(input) {
return next(next(input));
}
11 changes: 8 additions & 3 deletions src/2015/day12.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@ function deepSum(obj, ignore) {
if (Array.isArray(obj)) {
return obj.reduce((sum, x) => sum + deepSum(x, ignore), 0);
} else if (typeof obj === 'object') {
const values = Object.keys(obj).map(x => obj[x]);
let values = Object.keys(obj).map(x => obj[x]);
return ignore && values.indexOf(ignore) > -1 ? 0 : deepSum(values, ignore);
} else {
return typeof obj === 'number' ? obj : 0;
}
}

export const part1 = input => deepSum(JSON.parse(input));
export const part2 = input => deepSum(JSON.parse(input), 'red');
export function part1(input) {
return deepSum(JSON.parse(input));
}

export function part2(input) {
return deepSum(JSON.parse(input), 'red');
}
12 changes: 6 additions & 6 deletions src/2015/day13.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function longest(graph, curr, visited) {
const paths = Object.keys(graph[curr])
let paths = Object.keys(graph[curr])
.filter(node => visited.indexOf(node) === -1)
.map(
node => graph[curr][node] + longest(graph, node, visited.concat(curr)),
Expand All @@ -8,15 +8,15 @@ function longest(graph, curr, visited) {
}

function parse(input) {
const signs = { gain: +1, lose: -1 };
const graph = input
let signs = { gain: +1, lose: -1 };
let graph = input
.split('\n')
.map(x =>
x.match(/^(.*) would (gain|lose) (\d+) happiness .* next to (.*)\.$/),
)
.map(x => ({ p1: x[1], p2: x[4], d: signs[x[2]] * +x[3] }))
.reduce((graph, edge) => {
const prev = (graph[edge.p1] && graph[edge.p1][edge.p2]) || 0;
let prev = (graph[edge.p1] && graph[edge.p1][edge.p2]) || 0;
graph[edge.p1] = { ...graph[edge.p1], [edge.p2]: edge.d + prev };
graph[edge.p2] = { ...graph[edge.p2], [edge.p1]: edge.d + prev };
return graph;
Expand All @@ -29,11 +29,11 @@ function parse(input) {
}

export function part1(input) {
const graph = parse(input);
let graph = parse(input);
return longest(graph, Object.keys(graph).shift(), []);
}

export function part2(input) {
const graph = parse(input);
let graph = parse(input);
return longest(graph, '$$me', []);
}
8 changes: 4 additions & 4 deletions src/2015/day14.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,23 @@ function parse(input) {
function run(race, seconds) {
for (let i = 0; i < seconds; i++) {
race = race.map(x => {
const ran = i % (x.fly + x.rest) < x.fly;
let ran = i % (x.fly + x.rest) < x.fly;
x.distance += ran ? x.speed : 0;
return x;
});

const lead = race.reduce((prev, x) => Math.max(prev, x.distance), 0);
let lead = race.reduce((prev, x) => Math.max(prev, x.distance), 0);
race.filter(x => x.distance === lead).forEach(x => x.points++);
}
return race;
}

export function part1(input, seconds = 2503) {
const race = run(parse(input), seconds);
let race = run(parse(input), seconds);
return race.reduce((prev, x) => Math.max(prev, x.distance), 0);
}

export function part2(input, seconds = 2503) {
const race = run(parse(input), seconds);
let race = run(parse(input), seconds);
return race.reduce((prev, x) => Math.max(prev, x.points), 0);
}
16 changes: 8 additions & 8 deletions src/2015/day15.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ export function day(input) {
function add(digits, param, i) {
i = i || 0;
if (i < digits.length) {
const sum = digits[i] + param;
const pass = Math.floor(sum / 101);
let sum = digits[i] + param;
let pass = Math.floor(sum / 101);
digits[i] = sum % 101;
return pass ? add(digits, pass, i + 1) : digits;
}
Expand All @@ -22,24 +22,24 @@ export function day(input) {
}, {});
}

const ingredients = input
let ingredients = input
.split('\n')
.map(x => x.match(/^.*: (.*)$/))
.map(([, s]) => parseMap(s, ', ', ' '));

let part1 = 0,
part2 = 0;
const spoons = new Array(ingredients.length).fill(0);
let spoons = new Array(ingredients.length).fill(0);
while (add(spoons, 100)) {
if (spoons.reduce((prev, x) => prev + x) === 100) {
const amounts = ingredients.map((x, index) =>
let amounts = ingredients.map((x, index) =>
objMap(x, property => property * spoons[index]),
);
const sum = amounts.reduce((prev, x) =>
let sum = amounts.reduce((prev, x) =>
objMap(x, (value, key) => prev[key] + value),
);
const properties = Object.keys(sum).filter(x => x !== 'calories');
const result = properties
let properties = Object.keys(sum).filter(x => x !== 'calories');
let result = properties
.map(x => Math.max(0, sum[x]))
.reduce((prev, x) => prev * x);

Expand Down
4 changes: 2 additions & 2 deletions src/2015/day15.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const input = readInput(import.meta.url);

describe('day15 2015', () => {
test('it should work for example', () => {
const { part1, part2 } = day(
let { part1, part2 } = day(
[
'Butterscotch: capacity -1, durability -2, flavor 6, texture 3, calories 8',
'Cinnamon: capacity 2, durability 3, flavor -2, texture -1, calories 3',
Expand All @@ -16,7 +16,7 @@ describe('day15 2015', () => {
});

test('it should work for input', () => {
const { part1, part2 } = day(input);
let { part1, part2 } = day(input);
expect(part1).toEqual(13882464);
expect(part2).toEqual(11171160);
});
Expand Down
Loading

0 comments on commit 705e5f2

Please sign in to comment.