-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
683 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { readFile } from "fs/promises"; | ||
import { taskOne, taskTwo } from "./task"; | ||
|
||
async function main() { | ||
let lastArgument = process.argv.pop() as string; | ||
let taskNumber = 1; | ||
let isTest = false; | ||
|
||
if (lastArgument === "test") { | ||
isTest = true; | ||
taskNumber = parseInt(process.argv.pop() as string); | ||
} else { | ||
taskNumber = parseInt(lastArgument) | ||
} | ||
|
||
const fileToLoad = isTest ? "test.in" : "solve.in"; | ||
const fileContents = await readFile(fileToLoad, "utf-8") | ||
|
||
const lines = fileContents.split("\n"); | ||
|
||
if (taskNumber === 1) { | ||
await taskOne(lines); | ||
} | ||
if (taskNumber === 2) { | ||
await taskTwo(lines); | ||
} | ||
} | ||
|
||
void main(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
export async function taskOne(input: string[]): Promise<void> { | ||
let count = 0; | ||
for (let i = 0; i < input[0].length; i++) { | ||
if (input[0][i] == '(') { | ||
count++; | ||
} else { | ||
count-- | ||
} | ||
} | ||
console.log(count); | ||
} | ||
|
||
export async function taskTwo(input: string[]): Promise<void> { | ||
let count = 0 | ||
for (let i = 0; i < input[0].length; i++) { | ||
if (input[0][i] == '(') { | ||
count++ | ||
} else { | ||
count-- | ||
} | ||
if (count == -1) { | ||
console.log(i+1); | ||
return; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { readFile } from "fs/promises"; | ||
import { taskOne, taskTwo } from "./task"; | ||
|
||
async function main() { | ||
let lastArgument = process.argv.pop() as string; | ||
let taskNumber = 1; | ||
let isTest = false; | ||
|
||
if (lastArgument === "test") { | ||
isTest = true; | ||
taskNumber = parseInt(process.argv.pop() as string); | ||
} else { | ||
taskNumber = parseInt(lastArgument) | ||
} | ||
|
||
const fileToLoad = isTest ? "test.in" : "solve.in"; | ||
const fileContents = await readFile(fileToLoad, "utf-8") | ||
|
||
const lines = fileContents.split("\n"); | ||
|
||
if (taskNumber === 1) { | ||
await taskOne(lines); | ||
} | ||
if (taskNumber === 2) { | ||
await taskTwo(lines); | ||
} | ||
} | ||
|
||
void main(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { parseNumberList } from '../../base/parse' | ||
|
||
export async function taskOne(input: string[]): Promise<void> { | ||
console.log( | ||
input.map(getPresentArea).reduce((a,b) => a+b, 0) | ||
) | ||
} | ||
|
||
export async function taskTwo(input: string[]): Promise<void> { | ||
console.log( | ||
input.map(getRibbonLength).reduce((a,b) => a+b, 0) | ||
) | ||
} | ||
|
||
function getPresentArea(line: string) { | ||
const l = parseNumberList(line, 'x'); | ||
const sides = [l[0]*l[1], l[1]*l[2], l[0]*l[2]] | ||
return 2*(sides[0]+sides[1]+sides[2]) + Math.min(...sides); | ||
} | ||
|
||
function getRibbonLength(line: string) { | ||
const l = parseNumberList(line, 'x'); | ||
l.sort((a,b) => a-b) | ||
|
||
return 2*(l[0]+l[1]) + l[0]*l[1]*l[2]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { readFile } from "fs/promises"; | ||
import { taskOne, taskTwo } from "./task"; | ||
|
||
async function main() { | ||
let lastArgument = process.argv.pop() as string; | ||
let taskNumber = 1; | ||
let isTest = false; | ||
|
||
if (lastArgument === "test") { | ||
isTest = true; | ||
taskNumber = parseInt(process.argv.pop() as string); | ||
} else { | ||
taskNumber = parseInt(lastArgument) | ||
} | ||
|
||
const fileToLoad = isTest ? "test.in" : "solve.in"; | ||
const fileContents = await readFile(fileToLoad, "utf-8") | ||
|
||
const lines = fileContents.split("\n"); | ||
|
||
if (taskNumber === 1) { | ||
await taskOne(lines); | ||
} | ||
if (taskNumber === 2) { | ||
await taskTwo(lines); | ||
} | ||
} | ||
|
||
void main(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { JsonSet } from '../../base/simpleStructure' | ||
|
||
export async function taskOne(_input: string[]): Promise<void> { | ||
const input = _input[0].split(''); | ||
const set = new JsonSet(); | ||
|
||
const c = [0, 0] | ||
set.add(c) | ||
for(const i of input) { | ||
if (i == '>') { | ||
c[0]++ | ||
} else if (i == '<') { | ||
c[0]-- | ||
} else if(i == '^') { | ||
c[1]++ | ||
} else { | ||
c[1]-- | ||
} | ||
set.add(c) | ||
} | ||
console.log(set.get().size) | ||
} | ||
|
||
export async function taskTwo(_input: string[]): Promise<void> { | ||
const input = _input[0].split(''); | ||
const set = new JsonSet(); | ||
|
||
const s = [0, 0] | ||
const r = [0, 0] | ||
set.add(s) | ||
let santaMoves = true; | ||
for(const i of input) { | ||
if (santaMoves) { | ||
if (i == '>') { | ||
s[0]++ | ||
} else if (i == '<') { | ||
s[0]-- | ||
} else if(i == '^') { | ||
s[1]++ | ||
} else { | ||
s[1]-- | ||
} | ||
set.add(s) | ||
} else { | ||
if (i == '>') { | ||
r[0]++ | ||
} else if (i == '<') { | ||
r[0]-- | ||
} else if(i == '^') { | ||
r[1]++ | ||
} else { | ||
r[1]-- | ||
} | ||
set.add(r) | ||
} | ||
santaMoves = !santaMoves | ||
} | ||
console.log(set.get().size) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { readFile } from "fs/promises"; | ||
import { taskOne, taskTwo } from "./task"; | ||
|
||
async function main() { | ||
let lastArgument = process.argv.pop() as string; | ||
let taskNumber = 1; | ||
let isTest = false; | ||
|
||
if (lastArgument === "test") { | ||
isTest = true; | ||
taskNumber = parseInt(process.argv.pop() as string); | ||
} else { | ||
taskNumber = parseInt(lastArgument) | ||
} | ||
|
||
const fileToLoad = isTest ? "test.in" : "solve.in"; | ||
const fileContents = await readFile(fileToLoad, "utf-8") | ||
|
||
const lines = fileContents.split("\n"); | ||
|
||
if (taskNumber === 1) { | ||
await taskOne(lines); | ||
} | ||
if (taskNumber === 2) { | ||
await taskTwo(lines); | ||
} | ||
} | ||
|
||
void main(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
export async function taskOne(input: string[]): Promise<void> { | ||
console.log(input.filter(isNice).length) | ||
|
||
function isNice(text: string) { | ||
const le = text.split('') | ||
if (le.filter(i => ['a','e','i','o','u'].includes(i)).length < 3) { | ||
return false | ||
} | ||
let last = '' | ||
let double = false | ||
for (const l of le) { | ||
if (last == l) double = true | ||
if (last == 'a' && l == 'b') return false | ||
if (last == 'c' && l == 'd') return false | ||
if (last == 'p' && l == 'q') return false | ||
if (last == 'x' && l == 'y') return false | ||
last = l | ||
} | ||
return double | ||
} | ||
} | ||
|
||
export async function taskTwo(input: string[]): Promise<void> { | ||
console.log(input.filter(isNice).length) | ||
|
||
function isNice(text: string) { | ||
const le = text.split('') | ||
|
||
let last = '' | ||
let beforeLast = '' | ||
const pairs: [number, string][] = [] | ||
let withGap = false | ||
for (const [i,l] of le.entries()) { | ||
if (l == beforeLast) { | ||
withGap = true | ||
} | ||
if (last != '') { | ||
pairs.push([i-1, last + l]) | ||
} | ||
beforeLast = last | ||
last = l | ||
} | ||
if (!withGap) return false | ||
|
||
return pairs.filter(([i, w]) => pairs.findIndex(([i2, w2]) => w == w2 && Math.abs(i-i2) > 1) >= 0).length > 0 | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { readFile } from "fs/promises"; | ||
import { taskOne, taskTwo } from "./task"; | ||
|
||
async function main() { | ||
let lastArgument = process.argv.pop() as string; | ||
let taskNumber = 1; | ||
let isTest = false; | ||
|
||
if (lastArgument === "test") { | ||
isTest = true; | ||
taskNumber = parseInt(process.argv.pop() as string); | ||
} else { | ||
taskNumber = parseInt(lastArgument) | ||
} | ||
|
||
const fileToLoad = isTest ? "test.in" : "solve.in"; | ||
const fileContents = await readFile(fileToLoad, "utf-8") | ||
|
||
const lines = fileContents.split("\n"); | ||
|
||
if (taskNumber === 1) { | ||
await taskOne(lines); | ||
} | ||
if (taskNumber === 2) { | ||
await taskTwo(lines); | ||
} | ||
} | ||
|
||
void main(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
let grid: number[][] = [] | ||
|
||
export async function taskOne(input: string[]): Promise<void> { | ||
grid = Array.from({length: 1000}, () => Array.from({length: 1000}, () => 0)) | ||
input.forEach(doInstrcution) | ||
console.log(count()) | ||
|
||
function doInstrcution(ins: string) { | ||
const r = /([^0-9]) ([0-9]+),([0-9]+) through ([0-9]+),([0-9]+)/.exec(ins) | ||
if (r == null) throw ins | ||
const x1 = parseInt(r[2]) | ||
const y1 = parseInt(r[3]) | ||
const x2 = parseInt(r[4]) | ||
const y2 = parseInt(r[5]) | ||
let task = 0 | ||
if (r[1] == 'n') task = 1 | ||
if (r[1] == 'e') task = 2 | ||
for(let x = x1; x <= x2; x++) { | ||
for (let y = y1; y <= y2; y++) { | ||
if (task == 0) grid[x][y] = 0 | ||
if (task == 1) grid[x][y] = 1 | ||
if (task == 2) grid[x][y] = grid[x][y] == 1 ? 0:1 | ||
} | ||
} | ||
} | ||
|
||
function count() { | ||
let i = 0; | ||
for (let x = 0; x < 1000; x++) { | ||
for (let y = 0; y < 1000; y++) { | ||
if (grid[x][y] > 0) i++ | ||
} | ||
} | ||
return i | ||
} | ||
} | ||
|
||
export async function taskTwo(input: string[]): Promise<void> { | ||
grid = Array.from({length: 1000}, () => Array.from({length: 1000}, () => 0)) | ||
input.forEach(doInstrcution) | ||
console.log(count()) | ||
|
||
function doInstrcution(ins: string) { | ||
const r = /([^0-9]) ([0-9]+),([0-9]+) through ([0-9]+),([0-9]+)/.exec(ins) | ||
if (r == null) throw ins | ||
const x1 = parseInt(r[2]) | ||
const y1 = parseInt(r[3]) | ||
const x2 = parseInt(r[4]) | ||
const y2 = parseInt(r[5]) | ||
let task = 0 | ||
if (r[1] == 'n') task = 1 | ||
if (r[1] == 'e') task = 2 | ||
for(let x = x1; x <= x2; x++) { | ||
for (let y = y1; y <= y2; y++) { | ||
if (task == 0) { | ||
grid[x][y]-- | ||
if (grid[x][y] < 0) grid[x][y] = 0 | ||
} | ||
if (task == 1) grid[x][y]++ | ||
if (task == 2) grid[x][y] += 2 | ||
} | ||
} | ||
} | ||
|
||
function count() { | ||
let i = 0; | ||
for (let x = 0; x < 1000; x++) { | ||
for (let y = 0; y < 1000; y++) { | ||
i += grid[x][y] | ||
} | ||
} | ||
return i | ||
} | ||
} | ||
|
Oops, something went wrong.