Skip to content

Instantly share code, notes, and snippets.

@tluyben
Last active Dec 6, 2021
Embed
What would you like to do?
import { timeStamp } from 'console';
import { accessSync, promises as fs } from 'fs';
(async()=>{
const input = await fs.readFile("./interpretertests/aoc_21_6_input.txt", 'utf8')
const f = (days) => {
const timers = input.split(',').map(Number)
let days8 = timers.map(t=>[1, t])
const timersLen = timers.length
for(let i = 0; i<days; i++){
const days8Len = days8.length
for(let j=0;j<days8Len;j++) {
if (days8[j]) {
days8[j][1]--
if (days8[j][1]<0) {
days8[j][1] = 6
if (days8[i+timersLen]) days8[i+timersLen][0]+=days8[j][0]; else days8[i+timersLen] = [days8[j][0], 8]
}
}
}
}
return days8.reduce((acc, cur)=> acc+cur[0], 0)
}
console.log('part 1', f(80))
console.log('part 2', f(256))
// improvement after submit
const f1 = (days) => {
let fish = Array.apply(null, Array(9)).map(()=>0)
input.split(',').map(Number).forEach(a => fish[a]++);
for(let i = 0; i<days; i++){
const newFish = fish.shift()
fish[6] += newFish
fish.push(newFish)
}
return fish.reduce((acc, cur)=> acc+cur, 0)
}
console.log('part 1', f1(80))
console.log('part 2', f1(256))
// wanted to remove remove the ugly (imho) array rotate and lack thereof in js
const f2 = (days) => {
let fish = Array.apply(null, Array(9)).map(()=>0)
input.split(',').map(Number).forEach(a => fish[a]++);
[...Array(days)].map((_, i) => fish[(i+7)%9] += fish[i%9])
return fish.reduce((acc, cur)=> acc+cur, 0)
}
console.log('part 1', f2(80))
console.log('part 2', f2(256))
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment