Commit eaf7f184 authored by Kai Brandes's avatar Kai Brandes

add more solutions to webapp based on wasm

parent 2356e31c
use wasm_bindgen::prelude::*;
const NO_DIGIT_ERROR: &'static str = "input contains some non-digits";
fn as_input_vec(input: String) -> Vec<u32> {
......@@ -11,6 +13,7 @@ fn as_input_vec(input: String) -> Vec<u32> {
as_digits.clone()
}
#[wasm_bindgen]
pub fn calc_day_1_a(input: String) -> u32 {
as_input_vec(input).iter()
.fold((0, 0),
......@@ -25,6 +28,7 @@ fn to_digit_tuple((l, r): (char, char)) -> (u32, u32) {
.expect(&NO_DIGIT_ERROR))
}
#[wasm_bindgen]
pub fn calc_day_1_b(input: String) -> u32 {
let c = input.clone();
let (first, second) = c.split_at(input.len() / 2);
......
use wasm_bindgen::prelude::*;
use std::ops::Rem;
fn min_max_line_diff(digits: Vec<u32>) -> u32 {
......@@ -31,6 +32,7 @@ fn line_to_digits(line: && str) -> Vec<u32> {
}).collect();
}
#[wasm_bindgen]
pub fn calc_day_2_a(input: String) -> u32 {
let lines: Vec<&str> = input.split_terminator("\n").collect();
return lines.iter()
......@@ -38,7 +40,7 @@ pub fn calc_day_2_a(input: String) -> u32 {
.fold(0, |old_sum, digits: Vec<u32>| old_sum + min_max_line_diff(digits));
}
#[wasm_bindgen]
pub fn calc_day_2_b(input: String) -> u32 {
let lines: Vec<&str> = input.split_terminator("\n").collect();
return lines.iter()
......
use std::cmp;
use wasm_bindgen::prelude::*;
fn rel_position_and_circle(pos: &u32) -> (u32, u32) {
let mut c = pos.clone() as i32;
......@@ -51,6 +52,7 @@ fn nr_elements_touched(circle_iter_ele_index: u32, width: u32) -> u32 {
ele
}
#[wasm_bindgen]
pub fn calc_day_3_b(search_value: u32) -> u32 {
let mut current_val = 0;
let mut elements: Vec<u32> = vec![1, 1];
......@@ -82,6 +84,7 @@ pub fn calc_day_3_b(search_value: u32) -> u32 {
}
#[wasm_bindgen]
pub fn calc_day_3_a(position: u32) -> u32 {
let (rel_pos, circle_nr) = rel_position_and_circle(&position);
let width = 1 + circle_nr * 2;
......
......@@ -4264,6 +4264,11 @@
"object-visit": "^1.0.0"
}
},
"materialize-css": {
"version": "1.0.0-rc.2",
"resolved": "https://registry.npmjs.org/materialize-css/-/materialize-css-1.0.0-rc.2.tgz",
"integrity": "sha512-FuQmSyq4Qv0ov7A2qXw0E6/jbQzSWx2P1pg2/XQDYTkkSc/GyiFAxu3fw9zgShwuTvyumEiw5jkxQWT9siqMBQ=="
},
"md5.js": {
"version": "1.3.5",
"resolved": "https://registry.npmjs.org/md5.js/-/md5.js-1.3.5.tgz",
......
......@@ -14,6 +14,7 @@
],
"author": "Kai Brandes <kai.brandes@codecentric.de>",
"dependencies": {
"materialize-css": "^1.0.0-rc.2",
"react": "^16.5.2",
"react-dom": "^16.5.2",
"react-hot-loader": "^4.3.11",
......
......@@ -5,9 +5,7 @@
</head>
<body>
<script src="./dist/bundle.js"></script>
<section>
<h1>Day 1</h1>
<div id="day1"></div>
</section>
<section id="solutions"></section>
<section id="solutions2"></section>
</body>
</html>
import React from "react";
import * as wasm from "adventofcode_2017";
export class Day1 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_1_a(event.target.value)});
}
resultB(event) {
this.setState({b: wasm.calc_day_1_b(event.target.value)});
}
render() {
return (
<div>
<h1><a href="https://adventofcode.com/2017/day/1" target="_blank">Day 1</a></h1>
<div>
<h2>a</h2>
<input type="text" onInput={this.resultA} defaultValue={'91212129'}/>
Result: {this.state.a}
</div>
<div>
<h2>b</h2>
<input type="text" onInput={this.resultB} defaultValue={'123425'}/>
Result: {this.state.b}
</div>
</div>
);
}
}
\ No newline at end of file
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 Day1 extends React.Component {
constructor(props) {
super(props);
this.state = {
value: null
};
this.onInput = this.onInput.bind(this);
}
onInput(event) {
this.setState({value: wasm.result_1a(event.target.value)});
}
render() {
return (
<div>
<h1>Hallo</h1>
<input type="text" onInput={this.onInput} />
Result: {this.state.value}
</div>
);
}
}
\ No newline at end of file
import {Day1} from './day1';
import {Day1} from './components/day1';
import {Day2} from './components/day2';
import {Day3} from './components/day3';
import React from "react";
import ReactDOM from "react-dom";
export function render() {
ReactDOM.render(React.createElement(Day1), document.querySelector('#day1'));
let solutions = document.querySelector('#solutions');
ReactDOM.render([
React.createElement(Day1),
React.createElement(Day2),
React.createElement(Day3)
], 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