Completed: 1, 2, 3, 4, 5.1, 6.1, 7, 8

This commit is contained in:
Crispy 2023-12-09 22:55:14 +01:00
parent 6344e9a504
commit 01116b01fe
44 changed files with 6587 additions and 0 deletions

1000
day_01/1.txt Normal file

File diff suppressed because it is too large Load diff

7
day_01/Cargo.lock generated Normal file
View file

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "day_01"
version = "0.1.0"

8
day_01/Cargo.toml Normal file
View file

@ -0,0 +1,8 @@
[package]
name = "day_01"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

25
day_01/day_01.c Normal file
View file

@ -0,0 +1,25 @@
#include <stdio.h>
int main() {
char c = 0;
unsigned int first_digit = 0;
unsigned int last_digit = 0;
unsigned int sum = 0;
while (c != EOF) {
c = getchar();
// putchar(c);
unsigned char possible_digit = c - 48;
if (possible_digit < 10) {
// printf("%d\n", possible_digit);
if (first_digit == 0) {
first_digit = possible_digit * 10;
}
last_digit = possible_digit;
} else if (c == 10) {
sum += first_digit + last_digit;
first_digit = 0;
last_digit = 0;
}
}
printf("%d", sum);
}

1000
day_01/input.txt Normal file

File diff suppressed because it is too large Load diff

1
day_01/rustfmt.toml Normal file
View file

@ -0,0 +1 @@
hard_tabs = true

80
day_01/src/main.rs Normal file
View file

@ -0,0 +1,80 @@
use std::fs;
fn main() {
// for _ in 0..1000 {
// a();
// }
a();
b();
}
fn a() {
/*
let data = include_str!("../1.txt");
let sum: i32 = data
.lines()
.map(|line| {
let nums: Vec<_> = line.chars().filter(|c| c.is_numeric()).collect();
let mut num = nums[0].to_string();
num.push(*nums.last().unwrap());
num.parse::<i32>().unwrap()
})
.sum();
println!("{sum:?}");
*/
let bytes = fs::read("input.txt").expect("input.txt not found");
let mut sum = 0;
let mut first_digit = 0;
let mut last_digit = 0;
for byte in bytes {
if byte <= b'9' && byte > b'0' {
let digit = byte - 48;
last_digit = digit;
if first_digit == 0 {
first_digit = digit * 10;
}
} else if byte == b'\n' {
sum += last_digit as u16;
sum += first_digit as u16;
last_digit = 0;
first_digit = 0;
}
}
println!("{sum}");
}
fn b() {
let data = include_str!("../1.txt");
let sum: i32 = data
.lines()
.map(|line| {
let mut digits = Vec::new();
for i in 0..line.len() {
let c = line.chars().nth(i).unwrap();
if c.is_numeric() {
digits.push(c);
} else {
for (digit, &text) in [
"one", "two", "three", "four", "five", "six", "seven", "eight", "nine",
]
.iter()
.enumerate()
.map(|(i, t)| ((i + 1).to_string().chars().next().unwrap(), t))
{
if line[i..].starts_with(text) {
digits.push(digit);
break;
}
}
}
}
let mut num = digits[0].to_string();
num.push(*digits.last().unwrap());
num.parse::<i32>().unwrap()
})
.sum();
println!("{sum:?}");
}