mirror of
https://github.com/Aviortheking/advent-of-code.git
synced 2025-08-04 17:32:00 +00:00
12
d3/ex.txt
Normal file
12
d3/ex.txt
Normal file
@@ -0,0 +1,12 @@
|
||||
00100
|
||||
11110
|
||||
10110
|
||||
10111
|
||||
10101
|
||||
01111
|
||||
00111
|
||||
11100
|
||||
10000
|
||||
11001
|
||||
00010
|
||||
01010
|
1000
d3/input.txt
Normal file
1000
d3/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
27
d3/part1.ts
Normal file
27
d3/part1.ts
Normal 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
38
d3/part2.ts
Normal 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)}`)
|
Reference in New Issue
Block a user