Skip to content

Commit

Permalink
AOC 2023 day 1!
Browse files Browse the repository at this point in the history
  • Loading branch information
JonathanBrouwer committed Dec 1, 2023
1 parent 59dcf5b commit bcee8f6
Show file tree
Hide file tree
Showing 5 changed files with 203 additions and 8 deletions.
8 changes: 3 additions & 5 deletions compiler/src/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::collections::HashMap;
use std::vec::IntoIter;

pub trait IO {
fn read(&mut self) -> TLit;
fn read(&mut self) -> Option<TLit>;
fn print(&mut self, v: TLit);
}

Expand All @@ -29,10 +29,8 @@ impl TestIO {
}

impl IO for TestIO {
fn read(&mut self) -> TLit {
self.inputs
.next()
.expect("Test tried to read more input than were available.")
fn read(&mut self) -> Option<TLit> {
self.inputs.next()
}

fn print(&mut self, v: TLit) {
Expand Down
6 changes: 5 additions & 1 deletion compiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ pub mod interpreter;
pub mod passes;
pub mod utils;

use crate::interpreter::TestIO;
use crate::passes::parse::parse::parse_program;
use crate::utils::gen_sym::gen_sym;
use clap::ValueEnum;
use miette::{IntoDiagnostic, NamedSource, Report};
use std::fs::File;
Expand All @@ -17,6 +19,7 @@ pub enum Pass {
Reveal,
Atomize,
Explicate,
Select,
}

pub fn compile(program: &str, filename: &str, output: &Path) -> miette::Result<()> {
Expand Down Expand Up @@ -64,7 +67,8 @@ pub fn display(program: &str, filename: &str, pass: Pass) -> miette::Result<()>
);
let prg_revealed = display!(prg_validated.reveal(), pass, Reveal);
let prg_atomized = display!(prg_revealed.atomize(), pass, Atomize);
let _prg_explicated = display!(prg_atomized.explicate(), pass, Explicate);
let prg_explicated = display!(prg_atomized.explicate(), pass, Explicate);
let _prg_select = display!(prg_explicated.eliminate().select(), pass, Select);

Ok(())
}
Expand Down
10 changes: 8 additions & 2 deletions compiler/src/passes/select/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,8 +333,14 @@ impl<'p, I: IO> X86Interpreter<'p, I> {
assert!(buffer_len >= 1);

if self.read_buffer.is_empty() {
self.read_buffer = format!("{}\n", self.io.read().int()).into_bytes();
self.read_buffer.reverse();
if let Some(read) = self.io.read() {
self.read_buffer = format!("{}\n", read.int()).into_bytes();
self.read_buffer.reverse();
} else {
self.memory.insert(buffer, 0);
self.regs.insert(Reg::RAX, 0);
return;
}
}
let val = self.read_buffer.pop().unwrap();
self.memory.insert(buffer, val as i64);
Expand Down
44 changes: 44 additions & 0 deletions programs/aoc/y2023d01p1.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
fn read_char() -> I64 {
let mut v = 0;
let mut res = 0;
asm {
subq $8 %RSP // allocate stack space for reading char
movq $0 %RAX // read
movq $0 %RDI // stdin
movq %RSP %RSI // put read char at top of stack
movq $1 %RDX // read 1 byte
syscall 4 // arity of 4
movq %RAX {res} // result of system call
popq {v} // pop read char
};
if res == 0 {
return res
};
v
}

fn main() {
let ASCII_NEWLINE = 10;
let ASCII_ZERO = 48;
let ASCII_NULL = 0;

let mut total = 0;
let mut first = 0;
let mut last = 0;

let mut next = 0;
while (next = read_char(); next != ASCII_NULL) {
if next == ASCII_NEWLINE {
total = total + first * 10 + last;
first = 0;
};
next = next - ASCII_ZERO;
if next >= 0 && next < 10 {
if first == 0 {
first = next;
};
last = next;
};
};
print(total);
}
143 changes: 143 additions & 0 deletions programs/aoc/y2023d01p2.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
fn read_char() -> I64 {
let mut v = 0;
let mut res = 0;
asm {
subq $8 %RSP // allocate stack space for reading char
movq $0 %RAX // read
movq $0 %RDI // stdin
movq %RSP %RSI // put read char at top of stack
movq $1 %RDX // read 1 byte
syscall 4 // arity of 4
movq %RAX {res} // result of system call
popq {v} // pop read char
};
if res == 0 {
return res
};
v
}

fn main() {
//let ASCII_NEWLINE = 10;
//let ASCII_NULL = 0;
//let ASCII_E = 101i64;
//let ASCII_N = 110i64;
//let ASCII_O = 111i64;
//let ASCII_T = 116i64;
//let ASCII_W = 119i64;
//let ASCII_H = 104i64;
//let ASCII_R = 114i64;
//let ASCII_F = 102i64;
//let ASCII_U = 117i64;
//let ASCII_I = 105i64;
//let ASCII_V = 118i64;
//let ASCII_X = 120i64;
//let ASCII_S = 115i64;
//let ASCII_G = 103i64;


let mut total = 0;
let mut first = 0;
let mut last = 0;

let mut x0 = 0;
let mut x1 = 0;
let mut x2 = 0;
let mut x3 = 0;
let mut x4 = 0;

let mut next = 0;
while (next = read_char(); next != 0) {
x0 = x1;
x1 = x2;
x2 = x3;
x3 = x4;
x4 = next;
if next == 10 {
total = total + first * 10 + last;
first = 0;
continue
};

if x2 == 111i64 && x3 == 110i64 && x4 == 101i64 {
if first == 0 {
first = 1;
};
last = 1;
continue
};

if x2 == 116i64 && x3 == 119i64 && x4 == 111i64 {
if first == 0 {
first = 2;
};
last = 2;
continue
};

if x0 == 116i64 && x1 == 104i64 && x2 == 114i64 && x3 == 101i64 && x4 == 101i64 {
if first == 0 {
first = 3;
};
last = 3;
continue
};

if x1 == 102i64 && x2 == 111i64 && x3 == 117i64 && x4 == 114i64 {
if first == 0 {
first = 4;
};
last = 4;
continue
};

if x1 == 102i64 && x2 == 105i64 && x3 == 118i64 && x4 == 101i64 {
if first == 0 {
first = 5;
};
last = 5;
continue
};

if x2 == 115i64 && x3 == 105i64 && x4 == 120i64 {
if first == 0 {
first = 6;
};
last = 6;
continue
};

if x0 == 115i64 && x1 == 101i64 && x2 == 118i64 && x3 == 101i64 && x4 == 110i64 {
if first == 0 {
first = 7;
};
last = 7;
continue
};

if x0 == 101i64 && x1 == 105i64 && x2 == 103i64 && x3 == 104i64 && x4 == 116i64 {
if first == 0 {
first = 8;
};
last = 8;
continue
};

if x1 == 110i64 && x2 == 105i64 && x3 == 110i64 && x4 == 101i64 {
if first == 0 {
first = 9;
};
last = 9;
continue
};

next = next - 48;
if next >= 0 && next < 10 {
if first == 0 {
first = next;
};
last = next;
};
};
print(total);
}

0 comments on commit bcee8f6

Please sign in to comment.