Skip to content

Commit

Permalink
AOC 2022 day 1!
Browse files Browse the repository at this point in the history
  • Loading branch information
JonathanBrouwer committed Nov 30, 2023
1 parent 24fad29 commit 59dcf5b
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 5 deletions.
8 changes: 6 additions & 2 deletions compiler/src/passes/explicate/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,12 @@ impl Display for CExpr<'_> {
CExpr::AccessField { strct, field } => {
write!(f, "{strct}.{field}")
}
CExpr::Asm { .. } => {
todo!()
CExpr::Asm { instrs } => {
writeln!(f, "asm {{")?;
for instr in instrs {
writeln!(indented(f), "{instr}")?;
}
write!(f, "}}")
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions compiler/src/passes/select/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ pub enum Instr<Arg: Display, IdentVars: Display> {
Popq { dst: Arg },
#[display(fmt = "retq")]
Retq,
#[display(fmt = "syscall\t// arity: {arity}")]
#[display(fmt = "syscall\t{arity}")]
Syscall { arity: usize },
#[display(fmt = "cmpq\t{src}\t{dst}")]
Cmpq { src: Arg, dst: Arg },
Expand All @@ -92,9 +92,9 @@ pub enum Instr<Arg: Display, IdentVars: Display> {
Setcc { cnd: Cnd }, //TODO allow setting other byteregs
#[display(fmt = "loadlbl\t{sym}\t{dst}")]
LoadLbl { sym: IdentVars, dst: Arg },
#[display(fmt = "call_direct\t{lbl}\t// arity: {arity}")]
#[display(fmt = "call_direct\t{lbl}\t{arity}")]
CallqDirect { lbl: IdentVars, arity: usize },
#[display(fmt = "call_indirect\t{src}\t// arity: {arity}")]
#[display(fmt = "call_indirect\t{src}\t{arity}")]
CallqIndirect { src: Arg, arity: usize },
}

Expand Down
52 changes: 52 additions & 0 deletions programs/aoc/y2022d01.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
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 next = read_char();

let mut best = 0;
let mut sum = 0;
let mut current = 0;
let mut last_was_newline = false;

while next != ASCII_NULL {
if next == ASCII_NEWLINE {
if last_was_newline {
// Found empty line
if sum > best {
best = sum;
};
sum = 0;
} else {
last_was_newline = true;
sum = sum + current;
current = 0;
}
} else {
last_was_newline = false;
current = current * 10 + next - ASCII_ZERO;
};
next = read_char();
};
print(best);
}

0 comments on commit 59dcf5b

Please sign in to comment.