-
-
Save natrys/d16622e9397e39fec5b2482e0f2046c3 to your computer and use it in GitHub Desktop.
AoC, day2, GUI in Vala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Gtk; | |
class Simulator { | |
private int[] code; | |
private int[] initial; | |
private int ip; | |
public Simulator(int[] initial) { | |
this.initial = initial; | |
} | |
private void run() { | |
while (true) { | |
switch(this.code[this.ip]) { | |
case 1: | |
this.code[this.code[this.ip+3]] = | |
this.code[this.code[this.ip+1]] + this.code[this.code[this.ip+2]]; | |
break; | |
case 2: | |
this.code[this.code[this.ip+3]] = | |
this.code[this.code[this.ip+1]] * this.code[this.code[this.ip+2]]; | |
break; | |
case 99: | |
return; | |
} | |
this.ip += 4; | |
} | |
} | |
public void reset(int noun, int verb) { | |
this.code = this.initial; | |
this.ip = 0; | |
this.code[1] = noun; | |
this.code[2] = verb; | |
} | |
public int search() { | |
this.run(); | |
return this.code[0]; | |
} | |
} | |
void main(string[] args) { | |
int[] initial = {}; | |
foreach (string num in stdin.read_line().split(",")) { | |
initial += int.parse(num); | |
} | |
var computer = new Simulator(initial); | |
Gtk.init(ref args); | |
var window = new Window(); | |
window.title = "Advent of Code (Day 2)"; | |
window.border_width = 10; | |
window.window_position = WindowPosition.CENTER; | |
window.set_default_size(350, 70); | |
window.destroy.connect(Gtk.main_quit); | |
var noun_entry = new Entry(); | |
var verb_entry = new Entry(); | |
noun_entry.set_placeholder_text("Enter noun...."); | |
verb_entry.set_placeholder_text("Enter verb...."); | |
var vbox = new Box(Orientation.VERTICAL, 20); | |
vbox.pack_start (noun_entry, false, true, 10); | |
vbox.pack_start (verb_entry, false, false, 10); | |
var button = new Button(); | |
button.set_label("calculate"); | |
var label = new Label(null); | |
button.clicked.connect(() => { | |
var noun = int.parse(noun_entry.get_text()); | |
var verb = int.parse(verb_entry.get_text()); | |
computer.reset(noun, verb); | |
label.set_label(computer.search().to_string()); | |
}); | |
var bbox = new Box(Orientation.VERTICAL, 20); | |
bbox.add(button); | |
bbox.add(label); | |
vbox.add(bbox); | |
window.add(vbox); | |
window.show_all(); | |
Gtk.main (); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment