Skip to content

Commit

Permalink
refactor: Day 6, 🐟, use shift/push on TS, and fixed array on Rust
Browse files Browse the repository at this point in the history
  • Loading branch information
icyJoseph committed Dec 6, 2021
1 parent f4612eb commit cee7763
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 11 deletions.
2 changes: 1 addition & 1 deletion 2021/day-6/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ fn solve(raw: String) -> () {
.collect::<Vec<u64>>();

let simulate = |target: u32| {
let mut fish_by_day: Vec<u64> = vec![0; 9];
let mut fish_by_day: [u64; 9] = [0; 9];

for &fish in school.iter() {
fish_by_day[fish as usize] += 1;
Expand Down
15 changes: 5 additions & 10 deletions 2021/deno/day-6.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,12 @@ const simulate = (target: number) => {
let day = 0;

while (day < target) {
let babies = byDay[0];
let atZero = byDay.shift();
// keep ts happy
if (atZero == null) throw new Error("Impossible...");

for (let i = 0; i < byDay.length; i++) {
if (i > 0) {
byDay[i - 1] = byDay[i - 1] + byDay[i];
}
byDay[i] = 0;
}

byDay[8] = byDay[8] + babies;
byDay[6] = byDay[6] + babies;
byDay.push(atZero);
byDay[6] = byDay[6] + atZero;

day = day + 1;
}
Expand Down

0 comments on commit cee7763

Please sign in to comment.