Commit 6be4efce authored by Kai Brandes's avatar Kai Brandes

add all solutions to wasm-web + better comp-structure

parent d734d878
......@@ -48,6 +48,7 @@ fn as_numbers(input: &String) -> Vec<u32> {
.collect::<Vec<_>>()
}
#[wasm_bindgen]
pub fn calc_day_6_a(input: String) -> u32 {
let input = as_numbers(&input);
return detect_loop_for(&input).0;
......
import React from "react";
import * as wasm from "adventofcode_2017";
export class Day2 extends React.Component {
constructor(props) {
super(props);
this.state = {
a: null,
b: null
};
this.resultA = this.resultA.bind(this);
this.resultB = this.resultB.bind(this);
}
resultA(event) {
this.setState({a: wasm.calc_day_2_a(event.target.value)});
}
resultB(event) {
this.setState({b: wasm.calc_day_2_b(event.target.value)});
}
render() {
return (
<div>
<h1><a href="https://adventofcode.com/2017/day/2" target="_blank">Day 2</a></h1>
<div>
<h2>a</h2>
<textarea cols={50} rows={10} type="text" onInput={this.resultA} defaultValue={"5 1 9 5\n7 5 3\n2 4 6 8"}/>
Result: {this.state.a}
</div>
<div>
<h2>b</h2>
<textarea cols={50} rows={10} type="text" onInput={this.resultB} defaultValue={"5 9 2 8\n9 4 7 3\n3 8 6 5"}/>
Result: {this.state.b}
</div>
</div>
);
}
}
\ No newline at end of file
import React from "react";
import * as wasm from "adventofcode_2017";
export class Day3 extends React.Component {
constructor(props) {
super(props);
this.state = {
a: null,
b: null,
currentns: [0]
};
this.resultA = this.resultA.bind(this);
this.resultB = this.resultB.bind(this);
this.addNext = this.addNext.bind(this);
}
resultA(event) {
this.setState({a: wasm.calc_day_3_a(event.target.value)});
}
resultB(event) {
this.setState({b: wasm.calc_day_3_b(event.target.value)});
}
addNext() {
let newState = this.state.currentns;
newState.push(wasm.calc_day_3_b(this.state.currentns[this.state.currentns.length-1]));
this.setState({currentns: newState});
}
render() {
return (
<div>
<h1><a href="https://adventofcode.com/2017/day/2" target="_blank">Day 3</a></h1>
<div>
<h2>a</h2>
<input type="text" onInput={this.resultA} defaultValue={'312051'}/>
Result: {this.state.a}
</div>
<div>
<h2>b</h2>
<input type="text" onInput={this.resultA} defaultValue={'312051'}/>
Result: {this.state.b}
<div>
<button onClick={this.addNext}>+</button>
{this.state.currentns.map((n,i) => (<span key={i}>&nbsp;<b>{n}</b>&nbsp;</span>))}
</div>
</div>
</div>
);
}
}
\ No newline at end of file
import React from "react";
import * as wasm from "adventofcode_2017";
export class Day4 extends React.Component {
constructor(props) {
super(props);
this.state = {
a: null,
b: null
};
this.resultA = this.resultA.bind(this);
this.resultB = this.resultB.bind(this);
}
resultA(event) {
this.setState({a: wasm.calc_day_4_a(event.target.value)});
}
resultB(event) {
this.setState({b: wasm.calc_day_4_b(event.target.value)});
}
render() {
return (
<div>
<h1><a href="https://adventofcode.com/2017/day/4" target="_blank">Day 4</a></h1>
<div>
<h2>a</h2>
<textarea cols={50} rows={10} type="text" onInput={this.resultA} defaultValue={"aa bb cc dd aa\naa bb cc dd aaa\n"}/>
Result: {this.state.a}
</div>
<div>
<h2>b</h2>
<textarea cols={50} rows={10} type="text" onInput={this.resultB} defaultValue={"bqs bqs dbutvgf mmzb izpyud rap izpyud xlzeb mnj hjncs\ncirkjq nmjuu xtgejv gtexvj vjcmtqq unjmu\nofbh bdrsk rdrjj elaxvk jrjdr\n"}/>
Result: {this.state.b}
</div>
</div>
);
}
}
\ No newline at end of file
This diff is collapsed.
import React from "react";
import * as wasm from "adventofcode_2017";
export class Day1 extends React.Component {
export class SolutionComponent extends React.Component {
constructor(props) {
super(props);
this.state = {
......@@ -13,25 +12,32 @@ export class Day1 extends React.Component {
}
resultA(event) {
this.setState({a: wasm.calc_day_1_a(event.target.value)});
this.setState({a: this.props.fn_a(event.target.value)});
}
resultB(event) {
this.setState({b: wasm.calc_day_1_b(event.target.value)});
this.setState({b: this.props.fn_b(event.target.value)});
}
renderInput(resultTriggerFn, defaultVal) {
if (this.props.cols && this.props.rows) {
return <textarea cols={this.props.cols} rows={this.props.rows} onInput={resultTriggerFn} defaultValue={defaultVal}/>;
}
return <input type="text" onInput={resultTriggerFn} defaultValue={defaultVal}/>;
}
render() {
return (
<div>
<h1><a href="https://adventofcode.com/2017/day/1" target="_blank">Day 1</a></h1>
<h1><a href="https://adventofcode.com/2017/day/{this.props.day}" target="_blank">Day {this.props.day}</a></h1>
<div>
<h2>a</h2>
<input type="text" onInput={this.resultA} defaultValue={'91212129'}/>
{this.renderInput(this.resultA, this.props.default_a)}
Result: {this.state.a}
</div>
<div>
<h2>b</h2>
<input type="text" onInput={this.resultB} defaultValue={'123425'}/>
{this.renderInput(this.resultB, this.props.default_b)}
Result: {this.state.b}
</div>
</div>
......
import {Day1} from './components/day1';
import {Day2} from './components/day2';
import {Day3} from './components/day3';
import {Day4} from './components/day4';
import {Day5} from './components/day5';
import {SolutionComponent} from './components/solution-component';
import React from "react";
import ReactDOM from "react-dom";
import * as wasm from "adventofcode_2017";
const TEXTAREA_COLS = 50;
const TEXTAREA_ROWS = 10;
function day1() {
return <SolutionComponent day={1}
fn_a={wasm.calc_day_1_a}
default_a={'91212129'}
fn_b={wasm.calc_day_1_b}
default_b={'123425'}
/>;
}
function day2() {
return <SolutionComponent day={2}
fn_a={wasm.calc_day_2_a}
default_a={'91212129'}
fn_b={wasm.calc_day_2_b}
default_b={'123425'}
cols={TEXTAREA_COLS}
rows={TEXTAREA_ROWS}
/>;
}
function day3() {
return <SolutionComponent day={3}
fn_a={wasm.calc_day_3_a}
default_a={'312051'}
fn_b={wasm.calc_day_3_b}
default_b={'312051'}
/>;
}
function day4() {
return <SolutionComponent day={4}
fn_a={wasm.calc_day_4_a}
default_a={'aa bb cc dd aa\naa bb cc dd aaa\n'}
fn_b={wasm.calc_day_4_b}
default_b={'bqs bqs dbutvgf mmzb izpyud rap izpyud xlzeb mnj hjncs\ncirkjq nmjuu xtgejv gtexvj vjcmtqq unjmu\nofbh bdrsk rdrjj elaxvk jrjdr\n'}
cols={TEXTAREA_COLS}
rows={TEXTAREA_ROWS}
/>;
}
function day5() {
const inputFor5B = "2\n" +
"2\n" +
"1\n" +
"2\n" +
"-3\n" +
"-4\n" +
"2\n" +
"-4\n" +
"-6";
return <SolutionComponent day={5}
fn_a={wasm.calc_day_5_a}
default_a={'0\n3\n0\n1\n-3'}
fn_b={wasm.calc_day_5_b}
default_b={inputFor5B}
cols={TEXTAREA_COLS}
rows={TEXTAREA_ROWS}
/>;
}
function day6() {
return <SolutionComponent day={6}
fn_a={wasm.calc_day_6_a}
default_a={'0 2 7 0'}
fn_b={wasm.calc_day_6_b}
default_b={'0 2 7 0'}
/>;
}
function day7() {
const day7input = "pbga (66)\nxhth (57)\nebii (61)\nhavc (66)\nktlj (57)\nfwft (72) -> ktlj, cntj, xhth\nqoyq (66)\npadx (45) -> pbga, havc, qoyq\ntknk (41) -> ugml, padx, fwft\njptl (61)\nugml (68) -> gyxo, ebii, jptl\ngyxo (61)\ncntj (57)";
return <SolutionComponent day={7}
fn_a={wasm.calc_day_7_a}
default_a={day7input}
fn_b={wasm.calc_day_7_b}
default_b={day7input}
cols={TEXTAREA_COLS}
rows={TEXTAREA_ROWS}
/>;
}
export function render() {
let solutions = document.querySelector('#solutions');
ReactDOM.render([
React.createElement(Day1),
React.createElement(Day2),
React.createElement(Day3),
React.createElement(Day4),
React.createElement(Day5)
], solutions);
ReactDOM.render(
<div>
{day1()}
{day2()}
{day3()}
{day4()}
{day5()}
{day6()}
{day7()}
</div>, solutions);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment