day 2
This commit is contained in:
parent
463715f582
commit
b310dc969b
24
.vscode/launch.json
vendored
Normal file
24
.vscode/launch.json
vendored
Normal file
@ -0,0 +1,24 @@
|
||||
{
|
||||
// Use IntelliSense to learn about possible attributes.
|
||||
// Hover to view descriptions of existing attributes.
|
||||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"request": "launch",
|
||||
"name": "Launch Program",
|
||||
"type": "node",
|
||||
"program": "${workspaceFolder}/main.ts",
|
||||
"cwd": "${workspaceFolder}",
|
||||
"env": {},
|
||||
"runtimeExecutable": "/Users/okaup/.deno/bin/deno",
|
||||
"runtimeArgs": [
|
||||
"run",
|
||||
"--unstable",
|
||||
"--inspect-wait",
|
||||
"--allow-all"
|
||||
],
|
||||
"attachSimplePort": 9229
|
||||
}
|
||||
]
|
||||
}
|
||||
6
2/exampleInput.txt
Normal file
6
2/exampleInput.txt
Normal file
@ -0,0 +1,6 @@
|
||||
7 6 4 2 1
|
||||
1 2 7 8 9
|
||||
9 7 6 2 1
|
||||
1 3 2 4 5
|
||||
8 6 4 4 1
|
||||
1 3 6 7 9
|
||||
1000
2/input.txt
Normal file
1000
2/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
87
2/task.ts
Normal file
87
2/task.ts
Normal file
@ -0,0 +1,87 @@
|
||||
import { readLines } from "../utils.ts";
|
||||
|
||||
export async function solveTask2part1() {
|
||||
const lines = await readLines('./2/input.txt');
|
||||
|
||||
let safeReports = 0;
|
||||
for (const line of lines) {
|
||||
const numbers = line
|
||||
.split(' ')
|
||||
.map( n => parseInt(n.trim()) );
|
||||
|
||||
const checkNumbers = (numbers:number[]) => {
|
||||
let order:string|undefined = undefined;
|
||||
for (let i = 1; i < numbers.length; i++) {
|
||||
const prevN = numbers[i-1];
|
||||
const n = numbers[i];
|
||||
const diff = n - prevN;
|
||||
if (!order) {
|
||||
order = diff < 0 ? 'desc' : 'asc';
|
||||
} else {
|
||||
if (order === 'asc' && diff < 0) return false;
|
||||
if (order === 'desc' && diff > 0) return false;
|
||||
}
|
||||
const diffAbs = Math.abs(diff);
|
||||
if (diffAbs < 1) return false;
|
||||
if (diffAbs > 3) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
const result = checkNumbers(numbers);
|
||||
if (result) safeReports++;
|
||||
|
||||
}
|
||||
|
||||
console.log('safeReports', safeReports)
|
||||
}
|
||||
|
||||
|
||||
export async function solveTask2part2() {
|
||||
// const lines = await readLines('./2/exampleInput.txt');
|
||||
const lines = await readLines('./2/input.txt');
|
||||
|
||||
let safeReports = 0;
|
||||
for (const line of lines) {
|
||||
const numbers = line
|
||||
.split(' ')
|
||||
.map( n => parseInt(n.trim()) );
|
||||
|
||||
let badLevelRemoved = false;
|
||||
const checkNumbers = (numbers:number[]):boolean => {
|
||||
const handleBadLevel = ():boolean => {
|
||||
if (badLevelRemoved) return false;
|
||||
badLevelRemoved = true;
|
||||
const possibilities = new Array(numbers.length).fill(undefined)
|
||||
.map( () => structuredClone(numbers) );
|
||||
possibilities.forEach( (n, i) => n.splice(i, 1) );
|
||||
|
||||
const anyLvlRmvdWorks = possibilities
|
||||
.map( n => checkNumbers(n) )
|
||||
.some( check => check )
|
||||
|
||||
return anyLvlRmvdWorks;
|
||||
}
|
||||
let order:string|undefined = undefined;
|
||||
for (let i = 1; i < numbers.length; i++) {
|
||||
const prevN = numbers[i-1];
|
||||
const n = numbers[i];
|
||||
const diff = n - prevN;
|
||||
if (!order) {
|
||||
order = diff < 0 ? 'desc' : 'asc';
|
||||
} else {
|
||||
if (order === 'asc' && diff < 0) return handleBadLevel();
|
||||
if (order === 'desc' && diff > 0) return handleBadLevel();
|
||||
}
|
||||
const diffAbs = Math.abs(diff);
|
||||
if (diffAbs < 1) return handleBadLevel();
|
||||
if (diffAbs > 3) return handleBadLevel();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
const result = checkNumbers(numbers);
|
||||
if (result) safeReports++;
|
||||
|
||||
}
|
||||
|
||||
console.log('safeReports', safeReports)
|
||||
}
|
||||
8
main.ts
8
main.ts
@ -1,4 +1,8 @@
|
||||
import { solveTask1part1, solveTask1part2 } from "./1/task.ts";
|
||||
import { solveTask2part1, solveTask2part2 } from "./2/task.ts";
|
||||
|
||||
await solveTask1part1();
|
||||
await solveTask1part2();
|
||||
// await solveTask1part1();
|
||||
// await solveTask1part2();
|
||||
|
||||
await solveTask2part1();
|
||||
await solveTask2part2();
|
||||
11
utils.ts
Normal file
11
utils.ts
Normal file
@ -0,0 +1,11 @@
|
||||
export async function readLines(path: string) {
|
||||
const decoder = new TextDecoder("utf-8");
|
||||
const inputData = await Deno.readFile(path);
|
||||
const input = decoder.decode(inputData);
|
||||
|
||||
// console.log(input.substring(0,50) + '...');
|
||||
const lines = input.split('\n')
|
||||
.filter( l => l.trim() !== '');
|
||||
|
||||
return lines
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user