Initial Commit

Signed-off-by: Avior <github@avior.me>
This commit is contained in:
2021-12-06 18:42:38 +01:00
commit dc39e86744
28 changed files with 5861 additions and 0 deletions

12
d3/ex.txt Normal file
View File

@@ -0,0 +1,12 @@
00100
11110
10110
10111
10101
01111
00111
11100
10000
11001
00010
01010

1000
d3/input.txt Normal file

File diff suppressed because it is too large Load Diff

27
d3/part1.ts Normal file
View File

@@ -0,0 +1,27 @@
import fs from 'fs'
const input = fs.readFileSync(__dirname + '/input.txt').toString()
.split('\n')
const list: Array<{0: number, 1: number}> = []
for (const line of input) {
const splitted = line.split('')
for (let i = 0; i < splitted.length; i++) {
const bit = splitted[i];
if (!list[i]) {
list[i] = {0: 0, 1: 0}
}
list[i][bit === '0' ? 0 : 1]++
}
}
let gamma = ''
let epsilon = ''
for (const item of list) {
gamma = gamma + ((item[0] > item[1]) ? '0' : '1')
epsilon = epsilon + ((item[0] < item[1]) ? '0' : '1')
}
console.log(list)
console.log(`Result: ${parseInt(gamma, 2) * parseInt(epsilon, 2)}`)

38
d3/part2.ts Normal file
View File

@@ -0,0 +1,38 @@
import fs from 'fs'
const input = fs.readFileSync(__dirname + '/input.txt').toString()
.split('\n')
function find(mostUsed = true) {
let list = input
for (let i = 0; i < list[0].length; i++) {
const counts = {'0': 0, '1': 0}
for (const line of list) {
const bit = line[i] as '0' | '1'
counts[bit]++
}
const comparator = counts['0'] > counts['1'] ? '0' : '1'
let fn: (item: string) => boolean
if (mostUsed) {
fn = (line) => line[i] === comparator
} else {
fn = (line) => line[i] !== comparator
}
list = list.filter(fn)
if (list.length === 1) {
break
}
}
return list[0]
}
const resORG = find()
console.log(`ORG: ${resORG} or ${parseInt(resORG, 2)}`)
const resCSR = find(false)
console.log(`CSR: ${resCSR} or ${parseInt(resCSR, 2)}`)
console.log(`Result: ${parseInt(resORG, 2) * parseInt(resCSR, 2)}`)