-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day05.tsx
97 lines (87 loc) · 2.31 KB
/
Day05.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import * as React from 'react';
import { useState, useEffect, useRef } from 'react';
import autoAnimate from '@formkit/auto-animate';
import { clsx } from 'clsx';
import { inputDay05 } from '../data';
import { Crate, Stack, Move, parseData } from './Parse';
import './style.scss';
export const Day05 = () => {
const data = parseData(inputDay05);
return <Stacks {...data} />;
};
type Props = {
stacks: Stack[];
moves: Move[];
};
export const Stacks = (props: Props) => {
const [stacks, setStacks] = useState(props.stacks);
const [moves, setMoves] = useState(props.moves);
const parentRef = useRef(null);
useEffect(() => {
if (parentRef.current) {
autoAnimate(parentRef.current);
}
}, [parent]);
useEffect(() => {
const interval = setInterval(() => {
setMoves((moves) => {
const move = moves.shift();
setTimeout(() => {
setStacks((stacks) => {
const crate: Crate = stacks[move.from].pop();
stacks[move.to].push(crate);
return [...stacks];
});
}, 1);
return [...moves];
});
}, 200);
return () => clearInterval(interval);
}, []);
const currentMove = moves[0];
// console.log({ currentMove, nextMove: moves[1] });
return (
<div className="app">
<h1>Day5</h1>
<ol className="stacks">
{stacks.map((crates, i) => (
<li
key={i}
ref={parentRef}
className={clsx('stack', {
from: currentMove?.from === i,
to: currentMove?.to === i,
})}
>
{crates.map((crate, j) => (
<span
key={j}
className={clsx('crate', {
from: currentMove?.from === i && crates.length - 1 === j,
})}
>
{crate}
</span>
))}
<span
className={clsx('crate', 'placeholder', {
to: currentMove?.to === i,
})}
>
X
</span>
</li>
))}
</ol>
<div className="console">
{currentMove ? (
<span>
{currentMove.from + 1}s to {currentMove.to + 1}
</span>
) : (
<span></span>
)}
</div>
</div>
);
};