-
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
6 changed files
with
164 additions
and
0 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,50 @@ | ||
import { readFileSync } from 'fs'; | ||
import { join } from 'path'; | ||
import { DayOpts } from 'src/day'; | ||
|
||
function day1(input: string): any { | ||
const lines = input.split('\n'); | ||
let left: number[] = []; | ||
let right: number[] = []; | ||
|
||
for (const line of lines) { | ||
const [ l, r ] = line.split(/ +/).map(Number); | ||
left.push(l); | ||
right.push(r); | ||
} | ||
|
||
left.sort((a, b) => a - b); | ||
right.sort((a, b) => a - b); | ||
|
||
let sum = 0; | ||
for (let i = 0; i < left.length; i++) { | ||
sum += Math.abs(left[i] - right[i]); | ||
} | ||
|
||
return sum; | ||
} | ||
|
||
export function day(input: string): any { | ||
const lines = input.split('\n'); | ||
let left: number[] = []; | ||
let right = new Map<number, number>(); | ||
|
||
for (const line of lines) { | ||
const [ l, r ] = line.split(/ +/).map(Number); | ||
left.push(l); | ||
right.set(r, (right.get(r) ?? 0) + 1); | ||
} | ||
|
||
let sum = 0; | ||
for (const l of left) { | ||
sum += l * (right.get(l) ?? 0); | ||
} | ||
|
||
return sum; | ||
} | ||
|
||
export const opts: DayOpts = { | ||
input: readFileSync(join(__dirname, './input.txt')).toString(), | ||
sampleInput: readFileSync(join(__dirname, './sample.txt')).toString(), | ||
sampleAnswer: 11, | ||
}; |
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,6 @@ | ||
3 4 | ||
4 3 | ||
2 5 | ||
1 3 | ||
3 9 | ||
3 3 |
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,67 @@ | ||
import { readFileSync } from 'fs'; | ||
import { join } from 'path'; | ||
import { DayOpts } from 'src/day'; | ||
|
||
const isLevelSafe = (level: number[]) => { | ||
let pos = level[0] > level[1]; | ||
for (let i = 0; i < level.length; i++) { | ||
const curr = level[i]; | ||
const next = level[i + 1]; | ||
const diff = pos ? (curr - next) : (next - curr); | ||
if (diff < 1 || diff > 3) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
}; | ||
|
||
function day1(input: string): any { | ||
const lines = input.split('\n'); | ||
let sum = 0; | ||
const levels: number[][] = []; | ||
|
||
for (const line of lines) { | ||
levels.push(line.split(' ').map(Number)); | ||
} | ||
|
||
for (const level of levels) { | ||
if (isLevelSafe(level)) { | ||
sum++; | ||
} | ||
} | ||
|
||
return sum; | ||
} | ||
|
||
export function day(input: string): any { | ||
const lines = input.split('\n'); | ||
let sum = 0; | ||
const levels: number[][] = []; | ||
|
||
for (const line of lines) { | ||
levels.push(line.split(' ').map(Number)); | ||
} | ||
|
||
for (const level of levels) { | ||
let safe = isLevelSafe(level); | ||
|
||
for (let i = 0; i <= level.length; i++) { | ||
const l2 = [ ...level ]; | ||
l2.splice(i, 1); | ||
safe ||= isLevelSafe(l2); | ||
} | ||
|
||
if (safe) { | ||
sum++; | ||
} | ||
} | ||
|
||
return sum; | ||
} | ||
|
||
export const opts: DayOpts = { | ||
input: readFileSync(join(__dirname, './input.txt')).toString(), | ||
sampleInput: readFileSync(join(__dirname, './sample.txt')).toString(), | ||
sampleAnswer: 4, | ||
}; |
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,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 |
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,34 @@ | ||
import { readFileSync } from 'fs'; | ||
import { join } from 'path'; | ||
import { DayOpts } from 'src/day'; | ||
|
||
export function day(input: string): any { | ||
const lines = input.split('\n'); | ||
let sum = 0; | ||
|
||
let doing = true; | ||
|
||
for (let line of lines) { | ||
const matches = line.match(/(mul\((\d+),(\d+)\))|(do\(\))|(don't\(\))/g); | ||
for (const match of matches ?? []) { | ||
if (match === 'do()') { | ||
doing = true; | ||
} else if (match === 'don\'t()') { | ||
doing = false; | ||
} | ||
|
||
if (match.startsWith('mul') && doing) { | ||
const [ _, a, b ] = match.match(/mul\((\d+),(\d+)\)/); | ||
sum += Number(a) * Number(b); | ||
} | ||
} | ||
} | ||
|
||
return sum; | ||
} | ||
|
||
export const opts: DayOpts = { | ||
input: readFileSync(join(__dirname, './input.txt')).toString(), | ||
sampleInput: readFileSync(join(__dirname, './sample.txt')).toString(), | ||
sampleAnswer: 48, | ||
}; |
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 @@ | ||
xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5)) |