old stuff

This commit is contained in:
Niccolo Borgioli 2024-12-08 21:34:32 +01:00
parent f1755e2bd3
commit 9ef0275a61
9 changed files with 446 additions and 3 deletions

11
2019/3/README.md Normal file
View File

@ -0,0 +1,11 @@
# 3
Description
<details>
<summary>Solutions</summary>
<ol>
<li>1</li>
<li>2</li>
</ol>
</details>

96
2019/3/python/main.py Normal file
View File

@ -0,0 +1,96 @@
#!/usr/bin/env python
from os.path import join, dirname
# Day 3
# Common
def read_input(filename):
data = join(dirname(__file__), '..', filename)
with open(data) as f:
return f.read().strip()
test = read_input('test.txt')
data = read_input('input.txt')
# Running
Point = tuple[int, int]
class Line:
def __init__(self, start:Point, end: Point) -> None:
pass
class Wire:
def __init__(self, points: list[Point]) -> None:
self.points = points
def __iter__(self):
self.i = 0
self.max = len(self.points) - 1
return self
def __next__(self):
if self.i >= self.max:
raise StopIteration
value = (self.points[self.i], self.points[self.i +1])
self.i += 1
return value
def __str__(self) -> str:
return ', '.join([f'({x}, {y})' for x, y in self.points])
def find_intersections(self, other: Line) -> list[Point]:
matches: list[Point] = []
for a in self:
for b in other:
if a[0][0] < b[0]
# if a[0] == b[0] and a[1] == b[1]:
# matches.append(a[0])
return matches
@staticmethod
def parse(raw: str):
commands = raw.split(',')
current: Point = (0, 0)
points: list[Point] = [current]
for command in commands:
direction = command[0]
length = int(command[1:])
match direction:
case 'L':
current = (current[0] - length, current[1])
case 'R':
current = (current[0] + length, current[1])
case 'U':
current = (current[0], current[1] + length)
case 'D':
current = (current[0], current[1] - length)
points.append(current)
return Wire(points)
class Panel():
def __init__(self, wires: list[Wire]) -> None:
self.wires = wires
def find_closes(self):
matches = self.wires[0].find_intersections(self.wires[1].line)
distances = [abs(x)+abs(y) for x, y in matches]
return min(distances)
@staticmethod
def parse(raw: str):
return Panel(wires=[Wire.parse(line) for line in raw.splitlines()])
p = Panel.parse(test)
# print([str(w) for w in p.wires])
for wire in p.wires:
print(wire)
for line in wire:
print(line)
# print(p.find_closes())

View File

@ -5,16 +5,16 @@ extern crate test;
#[cfg(test)]
mod tests {
use super::*;
use test::Bencher;
use test::{black_box, Bencher};
#[bench]
fn bench_a(b: &mut Bencher) {
b.iter(|| part_a(INPUT));
b.iter(|| black_box(part_a(INPUT)));
}
#[bench]
fn bench_b(b: &mut Bencher) {
b.iter(|| part_b(INPUT));
b.iter(|| black_box(part_b(INPUT)));
}
}

103
2023/01/rust/main.rs Normal file
View File

@ -0,0 +1,103 @@
#![feature(test)]
extern crate test;
#[cfg(test)]
mod tests {
use super::*;
use test::Bencher;
#[bench]
fn bench_a(b: &mut Bencher) {
b.iter(|| part_a(INPUT));
}
#[bench]
fn bench_b(b: &mut Bencher) {
b.iter(|| part_b(INPUT));
}
}
const INPUT: &str = include_str!("../input.txt");
const TEST: &str = include_str!("../test.txt");
fn part_a(input: &str) {
let result = input
.trim()
.split("\n")
.map(|line| {
let mut first: u16 = 0;
for char in line.chars() {
if first != 0 {
break;
}
match char {
'1' => first = 1,
'2' => first = 2,
'3' => first = 3,
'4' => first = 4,
'5' => first = 5,
'6' => first = 6,
'7' => first = 7,
'8' => first = 8,
'9' => first = 9,
_ => continue,
}
}
let mut last: u16 = 0;
for char in line.chars().rev() {
if last != 0 {
break;
}
match char {
'1' => last = 1,
'2' => last = 2,
'3' => last = 3,
'4' => last = 4,
'5' => last = 5,
'6' => last = 6,
'7' => last = 7,
'8' => last = 8,
'9' => last = 9,
_ => continue,
}
}
return first * 10 + last;
})
.sum::<u16>();
println!("{}", result);
}
fn part_b(input: &str) {
let result = input
.trim()
.split("\n")
.map(|line| {
let mut first: usize = 0;
let nums = [
"1", "2", "3", "4", "5", "6", "7", "8", "9", "one", "two", "three", "four", "five",
"six", "seven", "eight", "nine",
];
for (i, s) in nums.iter().enumerate() {
if line.starts_with(s) {
first = if i < 9 { i + 1 } else { i - 8 };
break;
}
line.split_at(mid)
}
let mut last = 0 as usize;
return first * 10 + last;
})
.sum::<usize>();
println!("{}", result);
}
fn main() {
println!("Part A:");
part_a(TEST);
part_a(INPUT);
println!("\nPart B:");
part_b(TEST);
// part_b(INPUT);
}

11
2023/03/README.md Normal file
View File

@ -0,0 +1,11 @@
# 03
Description
<details>
<summary>Solutions</summary>
<ol>
<li>1</li>
<li>2</li>
</ol>
</details>

64
2023/03/rust/main.rs Normal file
View File

@ -0,0 +1,64 @@
#![feature(test)]
extern crate test;
#[cfg(test)]
mod tests {
use super::*;
use test::Bencher;
#[bench]
fn bench_a(b: &mut Bencher) {
b.iter(|| part_a(INPUT));
}
#[bench]
fn bench_b(b: &mut Bencher) {
b.iter(|| part_b(INPUT));
}
}
const INPUT: &str = include_str!("../input.txt");
const TEST: &str = include_str!("../test.txt");
struct Point {
x: usize,
y: usize,
}
fn part_a(input: &str) {
let result = input.trim();
let lines = result.split("\n").collect::<Vec<&str>>();
let mut symbols: Vec<Vec<usize>> = Vec::new();
for _ in [0..lines.len()] {
symbols.push(vec![0; 0]);
}
for (y, line) in result.split("\n").enumerate() {
for (x, char) in line.chars().enumerate() {
match char {
'*' | '&' | '+' | '#' | '$' => symbols[y].push(x),
_ => {}
}
}
}
println!("{:?}", symbols);
}
fn part_b(input: &str) {
let result = input.trim();
println!("{}", result);
}
fn main() {
println!("Part A:");
part_a(TEST);
// part_a(INPUT);
println!("\nPart B:");
// part_b(TEST);
// part_b(INPUT);
}

11
2023/04/README.md Normal file
View File

@ -0,0 +1,11 @@
# 04
Description
<details>
<summary>Solutions</summary>
<ol>
<li>1</li>
<li>2</li>
</ol>
</details>

127
2023/04/rust/main.rs Normal file
View File

@ -0,0 +1,127 @@
#![feature(test)]
use std::collections::HashMap;
extern crate test;
#[cfg(test)]
mod tests {
use super::*;
use test::Bencher;
#[bench]
fn bench_a(b: &mut Bencher) {
b.iter(|| part_a(INPUT));
}
#[bench]
fn bench_b(b: &mut Bencher) {
b.iter(|| part_b(INPUT));
}
}
const INPUT: &str = include_str!("../input.txt");
const TEST: &str = include_str!("../test.txt");
fn get_matches_per_card(input: &str) -> Vec<usize> {
input
.trim()
.split("\n")
.map(|line| {
let splitted: Vec<&str> = line.split(":").collect();
let body = splitted[1]
.split("|")
.map(|part| {
let mut numbers = part
.trim()
.split(" ")
.filter(|x| x.len() > 0)
.map(|s| s.parse::<usize>().expect("not a number"))
.collect::<Vec<usize>>();
numbers.sort();
numbers
})
.collect::<Vec<Vec<usize>>>();
let numbers = &body[0];
let winning = &body[1];
let mut matches = 0_usize;
for number in numbers {
match winning.binary_search(number) {
Err(_) => {}
Ok(_) => matches += 1,
}
}
matches
})
.collect()
}
fn part_a(input: &str) {
let matches = get_matches_per_card(input);
let total: usize = matches
.iter()
.map(|matches| {
if *matches > 0 {
2_usize.pow(*matches as u32 - 1)
} else {
0
}
})
.sum();
println!("{total}");
}
fn add_points(matches: &Vec<usize>, cache: &mut HashMap<usize, usize>, number: usize) -> usize {
let cached = cache.get(&number);
match cached {
Some(x) => *x,
None => {
let m = matches[number];
if m == 0 {
return 0;
}
let mut total = 0;
for n in number + 1..(number + m) {
total += add_points(matches, cache, n);
}
cache.insert(number, total);
total
}
}
}
fn part_b(input: &str) {
let matches = get_matches_per_card(input);
let mut cache: HashMap<usize, usize> = HashMap::new();
let mut scores = vec![0, matches.len()];
let mut total: usize = 0;
for m in matches {
// total += add_points(&matches, &mut cache, m);
}
// let total: usize = matches
// .iter()
// .map(|matches| {
// if *matches > 0 {
// 2_usize.pow(matches - 1)
// } else {
// 0
// }
// })
// .sum();
// println!("{total}");
}
fn main() {
println!("Part A:");
part_a(TEST);
part_a(INPUT);
println!("\nPart B:");
part_b(TEST);
// part_b(INPUT);
}

View File

@ -14,4 +14,24 @@ path = "./2022/01/rust/main.rs"
name = "2022-02"
path = "./2022/02/rust/main.rs"
[[bin]]
name = "2022-03"
path = "./2022/03/rust/main.rs"
[[bin]]
name = "2023-01"
path = "./2023/01/rust/main.rs"
[[bin]]
name = "2023-02"
path = "./2023/02/rust/main.rs"
[[bin]]
name = "2023-03"
path = "./2023/03/rust/main.rs"
[[bin]]
name = "2023-04"
path = "./2023/04/rust/main.rs"
# INJECT HERE