Skip to content

Instantly share code, notes, and snippets.

@tluyben
Last active December 6, 2021 06:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tluyben/ad19e8da3372583b90dce9342ff1c893 to your computer and use it in GitHub Desktop.
Save tluyben/ad19e8da3372583b90dce9342ff1c893 to your computer and use it in GitHub Desktop.
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