Skip to content

Commit

Permalink
aoc2024 day 5 solution
Browse files Browse the repository at this point in the history
  • Loading branch information
aspizu committed Dec 21, 2024
1 parent 9260123 commit 9c0079c
Show file tree
Hide file tree
Showing 12 changed files with 84 additions and 88 deletions.
2 changes: 1 addition & 1 deletion examples/aoc2024/day1/main.gs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ proc parse_input {
delete list2;
local i = 1;
repeat length(input) {
split_once input[i], " ";
split_once input[i], sep: " ";
add split_once_left to list1;
add split_once_right to list2;
i++;
Expand Down
6 changes: 3 additions & 3 deletions examples/aoc2024/day2/main.gs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ proc parse_input {
delete reports;
local i = 1;
repeat length(input) {
split input[i], " ";
split input[i], sep: " ";
add length(split) to reports;
local j = 1;
repeat length(split) {
Expand Down Expand Up @@ -79,7 +79,7 @@ proc count_safe_reports {
count_safe_reports = 0;
local i = 1;
until i > length(reports) {
is_report_safe i, 0;
is_report_safe i, skip_idx: 0;
if is_report_safe == true {
count_safe_reports++;
}
Expand All @@ -91,7 +91,7 @@ proc count_safe_reports_with_problem_dampener {
count_safe_reports_with_problem_dampener = 0;
local i = 1;
until i > length(reports) {
is_report_safe i, 0;
is_report_safe i, skip_idx: 0;
if is_report_safe == false {
is_report_safe_with_problem_dampener i;
}
Expand Down
4 changes: 2 additions & 2 deletions examples/aoc2024/day3/main.gs
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ proc run text, enable_do_donts {

onflag {
join_input;
run join_input, false;
run join_input, enable_do_donts: false;
without_do_donts = run;
run join_input, true;
run join_input, enable_do_donts: true;
say
"Without do() and don't() enabled: "
& without_do_donts
Expand Down
45 changes: 37 additions & 8 deletions examples/aoc2024/day5/main.gs
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ proc parse_input {
delete pages;
local i = 1;
repeat length(input) {
strfindchar input[i], "|";
strfindchar input[i], char: "|";
if strfindchar > 0 {
strsplitchar input[i], "|";
add Rule { left = strsplitchar[1], right = strsplitchar[2] } to rules;
add Rule { left: strsplitchar[1], right: strsplitchar[2] } to rules;
} else {
strfindchar input[i], ",";
strfindchar input[i], char: ",";
if strfindchar > 0 {
strsplitchar input[i], ",";
strsplitchar input[i], char: ",";
add length(strsplitchar) to pages;
local j = 1;
repeat length(strsplitchar) {
Expand All @@ -63,8 +63,8 @@ proc parse_input {
proc page_find_idx page_ptr, value {
page_find_idx = 0;
local i = $page_ptr + 1;
repeat length(pages[$page_ptr]) {
if pages[$page_ptr] == $value {
repeat pages[$page_ptr] {
if pages[i] == $value {
page_find_idx = i - $page_ptr;
stop_this_script;
}
Expand Down Expand Up @@ -108,9 +108,38 @@ proc main {
middle_number i;
sum += middle_number;
}
i += pages[i];
i += pages[i] + 1;
}
say "Result: " & sum;
local result = 0;
i = 1;
until i > length(pages) {
rules_in_page i;
if rule_in_page == false {
until rule_in_page == true {
local j = 1;
until j > length(rules) {
page_find_idx i, rules[j].left;
local left_idx = page_find_idx;
page_find_idx i, rules[j].right;
local right_idx = page_find_idx;
if left_idx > 0 and right_idx > 0 {
if left_idx > right_idx {
local temp = pages[i + left_idx];
pages[i + left_idx] = pages[i + right_idx];
pages[i + right_idx] = temp;
j = length(rules);
}
}
j++;
}
rules_in_page i;
}
middle_number i;
result += middle_number;
}
i += pages[i] + 1;
}
say "Result 1: " & sum & "\nResult 2: " & result;
}

onflag {
Expand Down
53 changes: 0 additions & 53 deletions examples/aoc2024/day5/main.py

This file was deleted.

2 changes: 2 additions & 0 deletions src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod enum_variant;
mod event;
mod event_kind;
mod expr;
mod kwarg;
mod list;
mod name;
mod proc;
Expand All @@ -25,6 +26,7 @@ pub use enum_variant::*;
pub use event::*;
pub use event_kind::*;
pub use expr::*;
pub use kwarg::*;
pub use list::*;
pub use name::*;
pub use proc::*;
Expand Down
11 changes: 11 additions & 0 deletions src/ast/kwarg.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use logos::Span;
use smol_str::SmolStr;

use super::Expr;
use crate::misc::Rrc;

#[derive(Debug)]
pub struct Kwarg {
pub value: Rrc<Expr>,
pub name: Option<(SmolStr, Span)>,
}
6 changes: 3 additions & 3 deletions src/ast/stmt.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use logos::Span;
use smol_str::SmolStr;

use super::{expr::Expr, type_::Type, Name};
use super::{expr::Expr, type_::Type, Kwarg, Name};
use crate::{blocks::Block, misc::Rrc};

#[derive(Debug)]
Expand Down Expand Up @@ -57,12 +57,12 @@ pub enum Stmt {
Block {
block: Block,
span: Span,
args: Vec<Rrc<Expr>>,
args: Vec<Kwarg>,
},
ProcCall {
name: SmolStr,
span: Span,
args: Vec<Rrc<Expr>>,
args: Vec<Kwarg>,
},
}

Expand Down
24 changes: 12 additions & 12 deletions src/codegen/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use super::{
sb3::{qualify_struct_var_name, QualifiedName, Sb3, D, S},
};
use crate::{
ast::{Expr, Name, Stmt, Type},
ast::{Expr, Kwarg, Name, Stmt, Type},
blocks::Block,
codegen::mutation::Mutation,
diagnostic::DiagnosticKind,
Expand Down Expand Up @@ -298,7 +298,7 @@ where T: Write + Seek
this_id: NodeID,
block: &Block,
span: &Span,
args: &Vec<Rrc<Expr>>,
args: &Vec<Kwarg>,
) -> io::Result<()> {
if block.args().len() != args.len() {
d.report(
Expand All @@ -316,7 +316,7 @@ where T: Write + Seek
let mut menu_is_default = menu_id.is_some();
for ((&arg_name, arg_value), &arg_id) in block.args().iter().zip(args).zip(&arg_ids) {
if block.menu().is_some_and(|menu| menu.input == arg_name) {
if let Expr::Value { value, span: _ } = &*arg_value.borrow() {
if let Expr::Value { value, span: _ } = &*arg_value.value.borrow() {
menu_value = Some(value.clone());
continue;
} else {
Expand All @@ -325,13 +325,13 @@ where T: Write + Seek
s,
d,
arg_name,
&arg_value.borrow(),
&arg_value.value.borrow(),
arg_id,
menu_id.unwrap(),
)?;
}
} else {
self.input(s, d, arg_name, &arg_value.borrow(), arg_id)?;
self.input(s, d, arg_name, &arg_value.value.borrow(), arg_id)?;
}
}
if menu_is_default {
Expand All @@ -348,8 +348,8 @@ where T: Write + Seek
write!(self, r#","fields":{fields}"#)?;
}
self.end_obj()?; // node
for (arg, arg_id) in args.iter().zip(arg_ids) {
self.expr(s, d, &arg.borrow(), arg_id, this_id)?;
for (kwarg, arg_id) in args.iter().zip(arg_ids) {
self.expr(s, d, &kwarg.value.borrow(), arg_id, this_id)?;
}
if let Some(menu) = block.menu() {
self.begin_node(
Expand All @@ -374,7 +374,7 @@ where T: Write + Seek
this_id: NodeID,
name: &SmolStr,
span: &Span,
args: &Vec<Rrc<Expr>>,
args: &Vec<Kwarg>,
) -> io::Result<()> {
let Some(proc) = s.sprite.procs.get(name) else {
d.report(DiagnosticKind::UnrecognizedProcedure(name.clone()), span);
Expand All @@ -392,13 +392,13 @@ where T: Write + Seek
let mut qualified_args: Vec<(SmolStr, NodeID)> = Vec::new();
let mut qualified_arg_values: Vec<Rrc<Expr>> = Vec::new();
self.begin_inputs()?;
for (arg, arg_value) in proc.args.iter().zip(args) {
for (arg, kwarg) in proc.args.iter().zip(args) {
match &arg.type_ {
Type::Value => {
let arg_id = self.id.new_id();
self.input(s, d, &arg.name, &arg_value.borrow(), arg_id)?;
self.input(s, d, &arg.name, &kwarg.value.borrow(), arg_id)?;
qualified_args.push((arg.name.clone(), arg_id));
qualified_arg_values.push(arg_value.clone());
qualified_arg_values.push(kwarg.value.clone());
}
Type::Struct {
name: type_name,
Expand All @@ -407,7 +407,7 @@ where T: Write + Seek
let Some(struct_) = s.sprite.structs.get(type_name) else {
continue;
};
let arg_value = &*arg_value.borrow();
let arg_value = &*kwarg.value.borrow();
let struct_literal_fields = match arg_value {
Expr::StructLiteral {
name: struct_literal_name,
Expand Down
2 changes: 2 additions & 0 deletions src/lexer/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ pub enum Token {
Percent,
#[token(";")]
Semicolon,
#[token(":")]
Colon,
#[token("length")]
Length,
#[token("round")]
Expand Down
9 changes: 7 additions & 2 deletions src/parser/grammar.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,16 @@ Stmt: Stmt = {
Stmt::SetListIndex { name: name.clone(), index: index.clone(), value: BinOp::Join.to_expr(name.span().clone(), BinOp::Of.to_expr(name.span().clone(), Expr::Name(name).into(), index).into(), value).into() }
},
INSERT <value:Expr> AT <name:Name> "[" <index:Expr> "]" ";" => Stmt::InsertAtList { name, index, value },
<l:@L> <name:NAME> <r:@R> <a:Comma<Expr>> ";" => match Block::from_shape(&name, a.len()) {
<l:@L> <name:NAME> <r:@R> <a:Comma<Kwarg>> ";" => match Block::from_shape(&name, a.len()) {
Some(block) => Stmt::Block { block, span: l..r, args: a },
None => Stmt::ProcCall { name, span: l..r, args: a },
},
}

Kwarg: Kwarg = <n:KwargName?> <v:Expr> => Kwarg { name: n, value: v.into() };

KwargName: (SmolStr, Span) = <l:@L> <n:NAME> <r:@R> ":" => (n, l..r);

If: Stmt = {
IF <cond:IfExpr> <if_body:Stmts> => Stmt::Branch { cond, if_body, else_body: Default::default() },
IF <cond:IfExpr> <if_body:Stmts> ELSE <else_body:Stmts> => Stmt::Branch { cond, if_body, else_body },
Expand Down Expand Up @@ -286,7 +290,7 @@ Term: Rrc<Expr> = {
}

StructLiteral: Rrc<Expr> = <l:@L> <n:NAME> <r:@R> "{" <f:Comma<StructLiteralField>> "}" => Expr::StructLiteral { name: n, span: l..r, fields: f }.into();
StructLiteralField: StructLiteralField = <l:@L> <n:NAME> <r:@R> "=" <v:Expr> => StructLiteralField { name: n, span: l..r, value: v };
StructLiteralField: StructLiteralField = <l:@L> <n:NAME> <r:@R> ":" <v:Expr> => StructLiteralField { name: n, span: l..r, value: v };

Name: Name = {
<NameName>,
Expand Down Expand Up @@ -385,6 +389,7 @@ extern {
"//" => Token::FloorDiv,
"%" => Token::Percent,
";" => Token::Semicolon,
":" => Token::Colon,
LENGTH => Token::Length,
ROUND => Token::Round,
ABS => Token::Abs,
Expand Down
8 changes: 4 additions & 4 deletions src/visitor/pass1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,17 +207,17 @@ fn visit_stmt(stmt: &mut Stmt, s: S, d: D) {
span: _,
args,
} => {
for arg in args {
visit_expr(arg, s, d);
for kwarg in args {
visit_expr(&mut kwarg.value, s, d);
}
}
Stmt::ProcCall {
name: _,
span: _,
args,
} => {
for arg in args {
visit_expr(arg, s, d);
for kwarg in args {
visit_expr(&mut kwarg.value, s, d);
}
}
}
Expand Down

0 comments on commit 9c0079c

Please sign in to comment.