mirror of
https://github.com/Aviortheking/advent-of-code.git
synced 2025-08-04 17:32:00 +00:00
1
d6/input.txt
Normal file
1
d6/input.txt
Normal file
@@ -0,0 +1 @@
|
||||
5,4,3,5,1,1,2,1,2,1,3,2,3,4,5,1,2,4,3,2,5,1,4,2,1,1,2,5,4,4,4,1,5,4,5,2,1,2,5,5,4,1,3,1,4,2,4,2,5,1,3,5,3,2,3,1,1,4,5,2,4,3,1,5,5,1,3,1,3,2,2,4,1,3,4,3,3,4,1,3,4,3,4,5,2,1,1,1,4,5,5,1,1,3,2,4,1,2,2,2,4,1,2,5,5,1,4,5,2,4,2,1,5,4,1,3,4,1,2,3,1,5,1,3,4,5,4,1,4,3,3,3,5,5,1,1,5,1,5,5,1,5,2,1,5,1,2,3,5,5,1,3,3,1,5,3,4,3,4,3,2,5,2,1,2,5,1,1,1,1,5,1,1,4,3,3,5,1,1,1,4,4,1,3,3,5,5,4,3,2,1,2,2,3,4,1,5,4,3,1,1,5,1,4,2,3,2,2,3,4,1,3,4,1,4,3,4,3,1,3,3,1,1,4,1,1,1,4,5,3,1,1,2,5,2,5,1,5,3,3,1,3,5,5,1,5,4,3,1,5,1,1,5,5,1,1,2,5,5,5,1,1,3,2,2,3,4,5,5,2,5,4,2,1,5,1,4,4,5,4,4,1,2,1,1,2,3,5,5,1,3,1,4,2,3,3,1,4,1,1
|
28
d6/part1.ts
Normal file
28
d6/part1.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import fs from 'fs'
|
||||
|
||||
const input = fs.readFileSync(__dirname + '/input.txt').toString()
|
||||
.split(',').map((it) => parseInt(it))
|
||||
|
||||
const numberOfDays = 80
|
||||
const LOG = false
|
||||
|
||||
if (LOG) console.log("Initial state:", input)
|
||||
|
||||
for (let i = 1; i <= numberOfDays; i++) {
|
||||
const len = input.length
|
||||
for (let j = 0; j < len; j++) {
|
||||
if (input[j] === 0) {
|
||||
input[j] = 6
|
||||
input.push(8)
|
||||
} else {
|
||||
input[j]--
|
||||
}
|
||||
}
|
||||
|
||||
if (LOG) console.log("After", i, "days:", input)
|
||||
|
||||
}
|
||||
|
||||
console.log(
|
||||
"Result:", input.length
|
||||
)
|
43
d6/part2.ts
Normal file
43
d6/part2.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import fs from 'fs'
|
||||
|
||||
const input = fs.readFileSync(__dirname + '/input.txt').toString()
|
||||
.split(',').map((it) => parseInt(it))
|
||||
|
||||
const numberOfDays = 256
|
||||
const LOG = false
|
||||
|
||||
|
||||
const list: Record<number, number> = {}
|
||||
|
||||
for (const fishy of input) {
|
||||
if (!(fishy in list)) {
|
||||
list[fishy] = 0
|
||||
}
|
||||
list[fishy]++
|
||||
}
|
||||
|
||||
if (LOG) console.log("Initial state:", list, Object.values(list).reduce((p, c) => p + c, 0))
|
||||
|
||||
for (let i = 1; i <= numberOfDays; i++) {
|
||||
let newBorns = 0
|
||||
for (let v = 0; v <= 8; v++) {
|
||||
if (!(v in list)) {
|
||||
list[v] = 0
|
||||
}
|
||||
if (v === 0) {
|
||||
newBorns = list[v] ?? 0
|
||||
} else {
|
||||
list[v - 1] = list[v]
|
||||
}
|
||||
}
|
||||
list[8] = newBorns
|
||||
list[6] = newBorns + (list[6] ?? 0)
|
||||
|
||||
|
||||
if (LOG) console.log("After", i, "days:", list, Object.values(list).reduce((p, c) => p + c, 0))
|
||||
|
||||
}
|
||||
|
||||
console.log(
|
||||
"Result:", Object.values(list).reduce((p, c) => p + c, 0)
|
||||
)
|
Reference in New Issue
Block a user