|
|
@ -2,33 +2,36 @@ import './App.css';
|
|
|
|
import reactLogo from './assets/react.svg';
|
|
|
|
import reactLogo from './assets/react.svg';
|
|
|
|
import { ChangeEvent, useCallback, useRef, useState } from 'react';
|
|
|
|
import { ChangeEvent, useCallback, useRef, useState } from 'react';
|
|
|
|
import { DayPicker } from 'react-day-picker';
|
|
|
|
import { DayPicker } from 'react-day-picker';
|
|
|
|
import { getPuzzlesByDay } from 'data/puzzles/getPuzzlesByDay';
|
|
|
|
|
|
|
|
import { PuzzleResult } from 'models';
|
|
|
|
import { PuzzleResult } from 'models';
|
|
|
|
import { solvePuzzle } from 'data/solutions/solvePuzzle';
|
|
|
|
import { solvePuzzle } from 'data/solvePuzzle';
|
|
|
|
|
|
|
|
import { NotificationBox } from 'components/notification-box/NotificationBox';
|
|
|
|
|
|
|
|
|
|
|
|
import 'react-day-picker/dist/style.css';
|
|
|
|
import 'react-day-picker/dist/style.css';
|
|
|
|
import 'components/notification-box/NotificationBox.css';
|
|
|
|
import { Toggler } from 'components/toggler/Toggler';
|
|
|
|
import NotificationBox from 'components/notification-box/NotificationBox';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function App() {
|
|
|
|
function App() {
|
|
|
|
let puzzles: string[] = [];
|
|
|
|
const errorText = `Uh oh, we couldn't find an answer for that puzzle!`;
|
|
|
|
const puzzleInputRef = useRef<HTMLTextAreaElement | null>(null);
|
|
|
|
const puzzleInputRef = useRef<HTMLTextAreaElement | null>(null);
|
|
|
|
|
|
|
|
|
|
|
|
const [selectedDate, setSelectedDate] = useState<Date>();
|
|
|
|
const [selectedDate, setSelectedDate] = useState<Date>();
|
|
|
|
// const [ selectedPuzzle, setSelectedPuzzle ] = useState<Date>();
|
|
|
|
|
|
|
|
const [puzzleInput, setPuzzleInput] = useState<string>();
|
|
|
|
const [puzzleInput, setPuzzleInput] = useState<string>();
|
|
|
|
const [answer, setAnswer] = useState<PuzzleResult>();
|
|
|
|
const [answer, setAnswer] = useState<PuzzleResult | null>(null);
|
|
|
|
|
|
|
|
const [badAnswer, setBadAnswer] = useState<boolean>(false);
|
|
|
|
|
|
|
|
const [answerPart2, setAnswerPart2] = useState<boolean>(false);
|
|
|
|
|
|
|
|
|
|
|
|
const handleChangeDate = async (date: Date) => {
|
|
|
|
const handleChangeDate = async (date: Date) => {
|
|
|
|
puzzles = [];
|
|
|
|
setAnswer(null);
|
|
|
|
setSelectedDate(date);
|
|
|
|
setSelectedDate(date);
|
|
|
|
const day = date.getDate();
|
|
|
|
};
|
|
|
|
const thisDaysPuzzles = await getPuzzlesByDay(day);
|
|
|
|
|
|
|
|
puzzles.push(...thisDaysPuzzles);
|
|
|
|
const handleToggle = async (checked: boolean) => {
|
|
|
|
|
|
|
|
setAnswer(null);
|
|
|
|
|
|
|
|
setAnswerPart2(checked);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const handleChangePuzzleInput = useCallback(
|
|
|
|
const handleChangePuzzleInput = useCallback(
|
|
|
|
(e: ChangeEvent<HTMLTextAreaElement>) => {
|
|
|
|
(e: ChangeEvent<HTMLTextAreaElement>) => {
|
|
|
|
|
|
|
|
setAnswer(null);
|
|
|
|
const { value } = e.currentTarget;
|
|
|
|
const { value } = e.currentTarget;
|
|
|
|
setPuzzleInput(value);
|
|
|
|
setPuzzleInput(value);
|
|
|
|
},
|
|
|
|
},
|
|
|
@ -36,14 +39,15 @@ function App() {
|
|
|
|
);
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
const handleSolvePuzzle = async () => {
|
|
|
|
const handleSolvePuzzle = async () => {
|
|
|
|
|
|
|
|
setAnswer(null);
|
|
|
|
const day = selectedDate?.getDate();
|
|
|
|
const day = selectedDate?.getDate();
|
|
|
|
const answer = !!day && (await solvePuzzle(day, puzzleInput || ''));
|
|
|
|
const answer = !!day && (await solvePuzzle(day, answerPart2, puzzleInput || ''));
|
|
|
|
answer && setAnswer(answer);
|
|
|
|
answer && setAnswer(answer);
|
|
|
|
|
|
|
|
const badAnswer = !!(answer && answer.answer === '' && !answer.values.length);
|
|
|
|
|
|
|
|
setBadAnswer(badAnswer);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const disabled = !(puzzleInput && selectedDate);
|
|
|
|
const disabled = !(puzzleInput && selectedDate);
|
|
|
|
const badAnswer = !!(answer && answer.answer === '' && !answer.values.length);
|
|
|
|
|
|
|
|
const errorText = `Uh oh, we couldn't find an answer for that puzzle!`;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
return (
|
|
|
|
<div className="App">
|
|
|
|
<div className="App">
|
|
|
@ -54,29 +58,16 @@ function App() {
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<h1>Advent of Code Solutions</h1>
|
|
|
|
<h1>Advent of Code Solutions</h1>
|
|
|
|
<p className="instructions">
|
|
|
|
<p className="instructions">
|
|
|
|
Please select a date and a puzzle, then enter some input, and click
|
|
|
|
Please select a date and enter some input, and click "Solve" to see the
|
|
|
|
"Solve" to see the output.
|
|
|
|
answer!
|
|
|
|
</p>
|
|
|
|
</p>
|
|
|
|
<div className="card split-card">
|
|
|
|
<div className="card split-card">
|
|
|
|
<div>
|
|
|
|
|
|
|
|
<h4>Date</h4>
|
|
|
|
|
|
|
|
<DayPicker
|
|
|
|
<DayPicker
|
|
|
|
required={true}
|
|
|
|
required={true}
|
|
|
|
mode="single"
|
|
|
|
mode="single"
|
|
|
|
selected={selectedDate}
|
|
|
|
selected={selectedDate}
|
|
|
|
onDayClick={handleChangeDate}
|
|
|
|
onDayClick={handleChangeDate}
|
|
|
|
/>
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
{/* <div>
|
|
|
|
|
|
|
|
<h4>Puzzle</h4>
|
|
|
|
|
|
|
|
<ul>
|
|
|
|
|
|
|
|
{puzzles.map((puzzle, index) =>
|
|
|
|
|
|
|
|
<li key={ index }>{puzzle}</li>
|
|
|
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
</ul>
|
|
|
|
|
|
|
|
</div> */}
|
|
|
|
|
|
|
|
<div>
|
|
|
|
|
|
|
|
<h4>Input</h4>
|
|
|
|
|
|
|
|
<textarea
|
|
|
|
<textarea
|
|
|
|
className="large-editor-textarea"
|
|
|
|
className="large-editor-textarea"
|
|
|
|
ref={puzzleInputRef}
|
|
|
|
ref={puzzleInputRef}
|
|
|
@ -86,12 +77,25 @@ function App() {
|
|
|
|
value={puzzleInput}
|
|
|
|
value={puzzleInput}
|
|
|
|
/>
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className="card">
|
|
|
|
|
|
|
|
<Toggler checked={answerPart2} onChange={handleToggle} />
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div className="card">
|
|
|
|
<div className="card">
|
|
|
|
<button disabled={disabled} onClick={handleSolvePuzzle}>
|
|
|
|
<button disabled={disabled} onClick={handleSolvePuzzle}>
|
|
|
|
Solve
|
|
|
|
Solve
|
|
|
|
</button>
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
{selectedDate && answer && !badAnswer && (
|
|
|
|
|
|
|
|
<div className="card">
|
|
|
|
|
|
|
|
<h4>
|
|
|
|
|
|
|
|
The answer to Puzzle {selectedDate?.getDate()} Part{' '}
|
|
|
|
|
|
|
|
{answerPart2 ? '2' : '1'} is: {answer.answer}
|
|
|
|
|
|
|
|
</h4>
|
|
|
|
|
|
|
|
{answer.values.length && (
|
|
|
|
|
|
|
|
<p className="instructions">{answer.values.join(', ')}</p>
|
|
|
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
)}
|
|
|
|
<NotificationBox visible={badAnswer} text={errorText} />
|
|
|
|
<NotificationBox visible={badAnswer} text={errorText} />
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
);
|
|
|
|