Day 01&02
This commit is contained in:
commit
a3fefe26a6
6 changed files with 4836 additions and 0 deletions
7
README.md
Normal file
7
README.md
Normal file
|
@ -0,0 +1,7 @@
|
|||
# Advent of Code 2022
|
||||
This Repository contains my solutions for the [Advent of Code 2022](https://adventofcode.com/2022).
|
||||
|
||||
## Execute Solutions
|
||||
(Make sure you have [Deno](https://deno.land) installed.)
|
||||
To run the code for a given day just run the existing task for it.
|
||||
For example to run the code for the first day run `deno task Day01`.
|
31
deno.json
Normal file
31
deno.json
Normal file
|
@ -0,0 +1,31 @@
|
|||
{
|
||||
"compilerOptions": {
|
||||
"allowJs": false,
|
||||
"strict": true,
|
||||
},
|
||||
"fmt": {
|
||||
"files": {
|
||||
"include": ["src/"],
|
||||
},
|
||||
"options": {
|
||||
"useTabs": false,
|
||||
"lineWidth": 120,
|
||||
"indentWidth": 2,
|
||||
"singleQuote": true,
|
||||
"proseWrap": "preserve"
|
||||
}
|
||||
},
|
||||
"lint": {
|
||||
"files": {
|
||||
"include": ["src/"],
|
||||
},
|
||||
"rules": {
|
||||
"tags": ["recommended"],
|
||||
"include": ["camelcase", "eqeqeq"]
|
||||
}
|
||||
},
|
||||
"tasks": {
|
||||
"Day01": "cd ./src/01_Calories && deno run --allow-read=./input.txt Calories.ts",
|
||||
"Day02": "cd ./src/02_RockPaperScissors && deno run --allow-read=./input.txt RockPaperScissors.ts",
|
||||
}
|
||||
}
|
19
src/01_Calories/Calories.ts
Normal file
19
src/01_Calories/Calories.ts
Normal file
|
@ -0,0 +1,19 @@
|
|||
const input = Deno.readTextFileSync('./input.txt');
|
||||
|
||||
const elveCal = [];
|
||||
let curElve = 0;
|
||||
let curCal = 0;
|
||||
for (const line of input.split('\n')) {
|
||||
if (line === '') {
|
||||
elveCal[curElve] = curCal;
|
||||
curCal = 0;
|
||||
curElve += 1;
|
||||
} else {
|
||||
curCal += parseInt(line);
|
||||
}
|
||||
}
|
||||
elveCal.sort().reverse();
|
||||
|
||||
console.log('Top Score:\t', elveCal[0]);
|
||||
const top3 = elveCal.slice(0, 3).reduce((prev, cur) => prev + cur, 0);
|
||||
console.log('Top three sum:\t', top3);
|
2236
src/01_Calories/input.txt
Normal file
2236
src/01_Calories/input.txt
Normal file
File diff suppressed because it is too large
Load diff
43
src/02_RockPaperScissors/RockPaperScissors.ts
Normal file
43
src/02_RockPaperScissors/RockPaperScissors.ts
Normal file
|
@ -0,0 +1,43 @@
|
|||
const input = Deno.readTextFileSync('./input.txt').split('\n');
|
||||
|
||||
type OP = 'A' | 'B' | 'C';
|
||||
type ME = 'X' | 'Y' | 'Z';
|
||||
type RPC = 0 | 1 | 2;
|
||||
|
||||
const signVal: Record<OP | ME, RPC> = {
|
||||
A: 0,
|
||||
X: 0,
|
||||
B: 1,
|
||||
Y: 1,
|
||||
C: 2,
|
||||
Z: 2,
|
||||
};
|
||||
|
||||
function rpcPlay(op: RPC, me: RPC): number {
|
||||
if (op === me) return me + 4;
|
||||
if (me - op === 1 || me - op === -2) return me + 7;
|
||||
return me + 1;
|
||||
}
|
||||
|
||||
function getSign(op: RPC, winType: ME): RPC {
|
||||
if (winType === 'Y') return op;
|
||||
if (winType === 'X') return ((op + 2) % 3) as RPC;
|
||||
return ((op + 1) % 3) as RPC;
|
||||
}
|
||||
|
||||
function calcScore(firstPuzzle = true): number {
|
||||
let myScore = 0;
|
||||
for (const line of input) {
|
||||
if (line === '') continue;
|
||||
const [op, me] = line.split(' ') as [OP, ME];
|
||||
if (firstPuzzle) myScore += rpcPlay(signVal[op], signVal[me]);
|
||||
else {
|
||||
const ops = signVal[op];
|
||||
myScore += rpcPlay(ops, getSign(ops, me));
|
||||
}
|
||||
}
|
||||
return myScore;
|
||||
}
|
||||
|
||||
console.log('Solution first Puzzle:\t', calcScore());
|
||||
console.log('Solution second Puzzle:\t', calcScore(false));
|
2500
src/02_RockPaperScissors/input.txt
Normal file
2500
src/02_RockPaperScissors/input.txt
Normal file
File diff suppressed because it is too large
Load diff
Loading…
Reference in a new issue