Skip to content

Instantly share code, notes, and snippets.

@Fuyukai
Created December 3, 2023 03:50
Show Gist options
  • Select an option

  • Save Fuyukai/295233ee63467980e58b85fe7111ae38 to your computer and use it in GitHub Desktop.

Select an option

Save Fuyukai/295233ee63467980e58b85fe7111ae38 to your computer and use it in GitHub Desktop.
aoc 2023 megafile
#![allow(clippy::all)]
#![allow(unused)]
#![allow(non_camel_case_types)]
#![allow(while_true)]
#![feature(ascii_char)]
use std::io;
fn day1_fvdhsfsdf(first: &char, second: &char) -> u32 {
let mut acc: u32 = 0;
acc += (first.as_ascii().unwrap().as_u8() as u32 - 0x30) * 10;
acc += (second.as_ascii().unwrap().as_u8() as u32 - 0x30);
return acc;
}
fn day01_1() {
let all_lines = std::fs::read_to_string("./day01_1.txt").unwrap();
let lines = all_lines.split("\n").collect::<Vec<&str>>();
let mut acc: u32 = 0;
for line in lines {
let nums = line.chars()
.filter(|it| it.is_numeric())
.collect::<Vec<char>>();
if nums.is_empty() {
println!("{}", line);
continue;
}
let first = nums.first().unwrap();
let last = nums.last().unwrap();
acc += day1_fvdhsfsdf(first, last);
}
println!("{}", acc);
}
fn day01_2() {
let all_lines = std::fs::read_to_string("./day01_1.txt").unwrap();
let lines = all_lines.split("\n").collect::<Vec<&str>>();
let mut acc: u32 = 0;
for line in lines {
let mut found_first = false;
let mut found: Vec<char> = Vec::new();
// FUCK iterators
let chars = line.chars().collect::<Vec<char>>();
let mut idx: usize = 0;
let mut check_next = |index: usize, full: &str, put_char: char| -> usize {
println!("{}, {}, {}", index, full.len(), index + full.len() > chars.len());
if index + full.len() > chars.len() {
return 1;
}
let slice = &chars[index .. index + full.len()].iter().collect::<String>();
if slice == full {
found.push(put_char);
let mut length = full.len();
//
if length > 1 {
length -= 1;
}
return length;
} else {
return 1;
}
};
while (idx < chars.len()) {
let char = chars[idx];
if (char >= '0' && char <= '9') {
// borrow checker hack
let char_string = char.to_string();
let real_char_string = char_string.as_str();
idx += check_next(idx, real_char_string, char);
continue;
}
// yuck.
if (idx + 3 > chars.len()) {
idx += 1;
continue;
}
match char {
'o' => {
idx += check_next(idx, "one", '1');
continue;
}
't' => {
// may be either two or three
match chars[idx + 1] {
'w' => {
idx += check_next(idx, "two", '2');
continue;
}
'h' => {
idx += check_next(idx, "three", '3');
continue;
}
_ => {
idx += 1;
}
}
}
'f' => {
// may be either four or five
match chars[idx + 1] {
'o' => {
idx += check_next(idx, "four", '4');
continue;
}
'i' => {
idx += check_next(idx, "five", '5');
continue;
}
_ => {
idx += 1;
}
}
}
's' => {
// may be either six or seven
match chars[idx + 1] {
'i' => {
idx += check_next(idx, "six", '6');
continue;
}
'e' => {
idx += check_next(idx, "seven", '7');
continue;
}
_ => {
idx += 1;
}
}
},
'e' => {
idx += check_next(idx, "eight", '8');
continue;
}
'n' => {
idx += check_next(idx, "nine", '9');
continue;
}
_ => {
idx += 1;
}
}
}
let first = found.first();
let last = found.last();
println!("line={}, first={:?}, last={:?}", line, first, last);
if (first.is_none()) { continue; }
acc += day1_fvdhsfsdf(first.unwrap(), last.unwrap());
}
println!("{}", acc);
}
fn main() {
let mut output = String::new();
io::stdin().read_line(&mut output).unwrap();
match output.as_str() {
"01 1\n" => day01_1(),
"01 2\n" => day01_2(),
_ => {
println!("{}", output.as_str());
println!("Kill yourself");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment