Compare commits
2 Commits
ead01162cb
...
3a333dee53
Author | SHA1 | Date | |
---|---|---|---|
3a333dee53 | |||
adc6e0d0a7 |
@ -1,14 +1,16 @@
|
|||||||
# [Day 1](https://adventofcode.com/2022/day/1)
|
# [Day 1](https://adventofcode.com/2023/day/1)
|
||||||
:gift:
|
:gift:
|
||||||
|
|
||||||
Today's language: **Python**
|
Today's language: **Python**
|
||||||
|
Execution time: **0,021 s**
|
||||||
|
|
||||||
Both tasks are solved in one hell of a oneliner each:
|
Both tasks are solved in one hell of a oneliner each:
|
||||||
|
|
||||||
- Task 1: 153 characters / line
|
- Task 1: 153 characters / line
|
||||||
- Task 2: 363 characters / line
|
- Task 2: 363 characters / line
|
||||||
|
|
||||||
<!-- one-liners -->
|
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
p day01.py
|
python3 day01.py
|
||||||
```
|
```
|
||||||
|
<!-- one-liners -->
|
||||||
|
@ -2,12 +2,12 @@
|
|||||||
:gift::gift:
|
:gift::gift:
|
||||||
|
|
||||||
Today's language: **Rust**
|
Today's language: **Rust**
|
||||||
|
Execution time: **0,002 s**
|
||||||
|
|
||||||
My very first rust program!
|
My very first rust program!
|
||||||
|
|
||||||
<!-- my very first rust program -->
|
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
rustc day02.rs
|
rustc day02.rs
|
||||||
./day02
|
./day02
|
||||||
```
|
```
|
||||||
|
<!-- my very first rust program -->
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
:gift::gift::gift:
|
:gift::gift::gift:
|
||||||
|
|
||||||
Today's language: **Rust**
|
Today's language: **Rust**
|
||||||
|
Execution time: **0,006 s**
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
rustc day03.rs
|
rustc day03.rs
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
:gift::gift::gift::gift:
|
:gift::gift::gift::gift:
|
||||||
|
|
||||||
Today's language: **Rust**
|
Today's language: **Rust**
|
||||||
|
Execution time: **0,004 s**
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
rustc day04.rs
|
rustc day04.rs
|
||||||
|
@ -2,12 +2,12 @@
|
|||||||
:gift::gift::gift::gift::gift:
|
:gift::gift::gift::gift::gift:
|
||||||
|
|
||||||
Today's language: **Rust**
|
Today's language: **Rust**
|
||||||
|
Execution time: **0,009 s**
|
||||||
|
|
||||||
Super fast, it runs both tasks in 0,002s on my pc.
|
Super fast!
|
||||||
|
|
||||||
<!-- very efficient -->
|
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
rustc day05.rs
|
rustc day05.rs
|
||||||
./day05
|
./day05
|
||||||
```
|
```
|
||||||
|
<!-- very efficient -->
|
||||||
|
@ -2,12 +2,12 @@
|
|||||||
:gift::gift::gift::gift::gift::gift:
|
:gift::gift::gift::gift::gift::gift:
|
||||||
|
|
||||||
Today's language: **Rust**
|
Today's language: **Rust**
|
||||||
|
Execution time: **0,001 s**
|
||||||
|
|
||||||
*Midnight formula go brrrrr*
|
*Midnight formula go brrrrr*
|
||||||
|
|
||||||
<!-- quadratic formula go *brrrr* -->
|
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
rustc day06.rs
|
rustc day06.rs
|
||||||
./day06
|
./day06
|
||||||
```
|
```
|
||||||
|
<!-- quadratic formula go *brrr* -->
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
:gift::gift::gift::gift::gift::gift::gift:
|
:gift::gift::gift::gift::gift::gift::gift:
|
||||||
|
|
||||||
Today's language: **Rust**
|
Today's language: **Rust**
|
||||||
|
Execution time: **0,007 s**
|
||||||
|
|
||||||
Not pretty, but pretty fast.
|
Not pretty, but pretty fast.
|
||||||
|
|
||||||
@ -9,4 +10,4 @@ Not pretty, but pretty fast.
|
|||||||
rustc day07.rs
|
rustc day07.rs
|
||||||
./day07
|
./day07
|
||||||
```
|
```
|
||||||
<!-- -->
|
<!-- not pretty, but pretty fast -->
|
||||||
|
12
08/README.md
Normal file
12
08/README.md
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
# [Day 8](https://adventofcode.com/2023/day/8)
|
||||||
|
:gift::gift::gift::gift::gift::gift::gift::gift:
|
||||||
|
|
||||||
|
Today's language: **Rust**
|
||||||
|
Execution time: **0,039 s**
|
||||||
|
|
||||||
|
Next time, I should just use 64 bit right from the start...
|
||||||
|
|
||||||
|
```shell
|
||||||
|
rustc day08.rs
|
||||||
|
./day08
|
||||||
|
```
|
114
08/day08.rs
Normal file
114
08/day08.rs
Normal file
@ -0,0 +1,114 @@
|
|||||||
|
use std::io::{self, BufRead};
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
struct Junction(u32, u32);
|
||||||
|
|
||||||
|
fn hash(s: &str) -> u32 {
|
||||||
|
let mut h: u32 = 0;
|
||||||
|
for (i, c) in s.as_bytes().iter().enumerate() {
|
||||||
|
h |= (*c as u32) << (8 * i);
|
||||||
|
// println!("i: {}, s={}, Char: {:8b}, h: {:32b}", i, s, c, h);
|
||||||
|
}
|
||||||
|
return h;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn ends_with(h: &u32, c: char) -> bool {
|
||||||
|
return (h >> (8 * 2)) == (c as u32);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn gcd(mut x: u64, mut y: u64) -> u64 {
|
||||||
|
let mut d: u64;
|
||||||
|
assert!(x != 0 && y != 0);
|
||||||
|
while y != 0 {
|
||||||
|
d = y;
|
||||||
|
y = x % y;
|
||||||
|
x = d;
|
||||||
|
}
|
||||||
|
return x
|
||||||
|
}
|
||||||
|
|
||||||
|
fn lcm(a: u64, b: u64) -> u64 {
|
||||||
|
return (a * b) / gcd(a, b);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let input = "input.txt";
|
||||||
|
// let input = "example.txt";
|
||||||
|
let mut lines = read_lines(&input);
|
||||||
|
let Ok(instructions) = lines.next().expect("No line found") else { panic!("No line found") };
|
||||||
|
lines.next();
|
||||||
|
let mut junctions = HashMap::new();
|
||||||
|
let mut junctions_a = Vec::new();
|
||||||
|
for line in lines.map(|r| r.ok().unwrap()) {
|
||||||
|
let junction = hash(&line[0..3]);
|
||||||
|
junctions.insert(junction, Junction(hash(&line[7..10]), hash(&line[12..15])));
|
||||||
|
if ends_with(&junction, 'A') {
|
||||||
|
junctions_a.push(junction);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let match_junction = |junction: &u32, instruction: &char| -> u32 {
|
||||||
|
match instruction {
|
||||||
|
'R' => junctions[&junction].1,
|
||||||
|
'L' => junctions[&junction].0,
|
||||||
|
_ => panic!("Unknown instruction {}", instruction),
|
||||||
|
}
|
||||||
|
};
|
||||||
|
// task 1
|
||||||
|
let mut steps: u32 = 0;
|
||||||
|
let target = hash("ZZZ");
|
||||||
|
let mut junction = hash("AAA");
|
||||||
|
println!("Check endswidth {} {} {} {}", ends_with(&hash("AAA"), 'A'), ends_with(&hash("BFZ"), 'Z'), ends_with(&hash("AVD"), 'A'), ends_with(&hash("ZZS"), 'Z'));
|
||||||
|
'outer: loop {
|
||||||
|
for instruction in instructions.chars() {
|
||||||
|
if junction == target { break 'outer; }
|
||||||
|
steps += 1;
|
||||||
|
junction = match_junction(&junction, &instruction);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
println!("Required steps(1): {}", steps);
|
||||||
|
let mut steps_to_z = vec![0 as u32; junctions_a.len()];
|
||||||
|
let mut steps_cycle = vec![0 as u32; junctions_a.len()];
|
||||||
|
let mut first_z = vec![0 as u32; junctions_a.len()];
|
||||||
|
for i in 0..junctions_a.len() {
|
||||||
|
steps = 0;
|
||||||
|
'outer: loop {
|
||||||
|
for instruction in instructions.chars() {
|
||||||
|
if ends_with(&junctions_a[i], 'Z') {
|
||||||
|
if steps_to_z[i] == 0 {
|
||||||
|
steps_to_z[i] = steps;
|
||||||
|
first_z[i] = junctions_a[i];
|
||||||
|
steps = 0;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
assert_eq!(first_z[i], junctions_a[i], "Z->Z is not a cycle!");
|
||||||
|
steps_cycle[i] = steps;
|
||||||
|
break 'outer;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// print!("Step {:012}: junction[{}]={:024b}\r", steps, i, junctions_a[i]);
|
||||||
|
junctions_a[i] = match_junction(&junctions_a[i], &instruction);
|
||||||
|
steps += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for i in 0..junctions_a.len() {
|
||||||
|
println!("A-Z: {}, Z->Z: {}", steps_to_z[i], steps_cycle[i]);
|
||||||
|
}
|
||||||
|
let mut steps: u64 = steps_cycle[0] as u64;
|
||||||
|
for i in 1..steps_cycle.len() {
|
||||||
|
print!("lcm({:12}, {:8})", steps, steps_cycle[i]);
|
||||||
|
steps = lcm(steps, steps_cycle[i] as u64);
|
||||||
|
println!("={}", steps);
|
||||||
|
}
|
||||||
|
println!("Required steps(2): {}", steps);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
fn read_lines<P>(filename: P) -> io::Lines<io::BufReader<std::fs::File>>
|
||||||
|
where P: AsRef<std::path::Path>, {
|
||||||
|
return match std::fs::File::open(filename) {
|
||||||
|
Err(why) => panic!("Could not open file. {}", why),
|
||||||
|
Ok(file) => std::io::BufReader::new(file).lines()
|
||||||
|
};
|
||||||
|
}
|
18
README.md
18
README.md
@ -2,11 +2,13 @@
|
|||||||
This is a repository for my solutions for the [advent of code 2023](https://adventofcode.com/2023).
|
This is a repository for my solutions for the [advent of code 2023](https://adventofcode.com/2023).
|
||||||
|
|
||||||
<!-- table begin -->
|
<!-- table begin -->
|
||||||
| Day | Language | Execuction time | Comment |
|
| Day | Language | Execuction time | Comment |
|
||||||
|:---: |:---: | ---: | --- |
|
|:---: |:---: | ---: | --- |
|
||||||
| [01](01) | Python | 0,015 s | one-liners |
|
| [01](01) | Python | 0,021 s | one-liners |
|
||||||
| [02](02) | Rust | 0,001 s | my very first rust program |
|
| [02](02) | Rust | 0,002 s | my very first rust program |
|
||||||
| [03](03) | Rust | 0,001 s | |
|
| [03](03) | Rust | 0,006 s | |
|
||||||
| [04](04) | Rust | 0,001 s | |
|
| [04](04) | Rust | 0,004 s | |
|
||||||
| [05](05) | Rust | 0,001 s | very efficient |
|
| [05](05) | Rust | 0,009 s | very efficient |
|
||||||
| [06](06) | Rust | 0,001 s | quadratic formula go *brrrr* |
|
| [06](06) | Rust | 0,001 s | quadratic formula go *brrr* |
|
||||||
|
| [07](07) | Rust | 0,007 s | not pretty, but pretty fast |
|
||||||
|
| [08](08) | Rust | 0,039 s | |
|
||||||
|
@ -1,9 +1,10 @@
|
|||||||
# [Day X](https://adventofcode.com/2023/day/X)
|
# [Day DAY](https://adventofcode.com/2023/day/DAY)
|
||||||
:gift:
|
:gift:
|
||||||
|
|
||||||
Today's language: **Y**
|
Today's language: **LANG**
|
||||||
|
Execution time: **EXECTIME**
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
|
HOWTORUN
|
||||||
```
|
```
|
||||||
<!-- -->
|
<!-- COMMENT -->
|
||||||
|
89
make_day_readme.sh
Executable file
89
make_day_readme.sh
Executable file
@ -0,0 +1,89 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
# pass comment as param $1 and select day with day=X ./make_day_readme.sh
|
||||||
|
maxday=$(ls -d */ | sed -r 's|0*(.*)/|\1|g' | tail -1)
|
||||||
|
[[ -z $day ]] && day=$maxday
|
||||||
|
day_dir=$(printf "%02d" $day)
|
||||||
|
readme=$day_dir/README.md
|
||||||
|
|
||||||
|
FMT_VAR='\e[32m%s: \e[0m%s\n'
|
||||||
|
FMT_ERR='\e[31m%s\e[0m\n'
|
||||||
|
FMT_WARN='\e[33m%s\e[0m\n'
|
||||||
|
|
||||||
|
echo Making Readme of day $day in $readme
|
||||||
|
|
||||||
|
function find_lang {
|
||||||
|
# 1: fileext, 2: lang, 3: howto
|
||||||
|
sourcefile=$(ls $day_dir/*.$1 2> /dev/null)
|
||||||
|
[[ $? != 0 ]] && return
|
||||||
|
sourcefile=$(basename $sourcefile)
|
||||||
|
printf "$FMT_VAR" "Determined Langugae" "$2 ($sourcefile)"
|
||||||
|
sed -i "s/LANG/$2/" $readme
|
||||||
|
howto=$(echo "$3" | sed "s|SOURCE|$sourcefile|")
|
||||||
|
sed -i "s(HOWTORUN($howto(" $readme
|
||||||
|
}
|
||||||
|
|
||||||
|
function get_time {
|
||||||
|
cd $day_dir
|
||||||
|
exe=$(find . -type f -executable)
|
||||||
|
exe=$(basename $exe)
|
||||||
|
if [[ $(echo $exe | wc -w) != 1 ]]; then
|
||||||
|
printf "$FMT_ERR" "Found multiple or no executables for day $day: '$exe'"
|
||||||
|
return 1
|
||||||
|
time=Error
|
||||||
|
else
|
||||||
|
time=$({ time ./$exe; } 2>&1)
|
||||||
|
if [[ $? != 0 ]]; then
|
||||||
|
printf "$FMT_ERR" "Execution error in './$exe' in '$(pwd)'. Output:"
|
||||||
|
printf "\e[34m"
|
||||||
|
echo -e "$time"
|
||||||
|
printf "\e[0m"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
time=$(echo -e "$time" | tail -3 | head -1 | awk '{print $2}')
|
||||||
|
re='([[:digit:]]+)m([[:digit:]]+),([[:digit:]]+)s'
|
||||||
|
[[ $time =~ $re ]]
|
||||||
|
min=${BASH_REMATCH[1]}
|
||||||
|
sec=${BASH_REMATCH[2]}
|
||||||
|
msec=${BASH_REMATCH[3]}
|
||||||
|
if [[ $min != 0 ]]; then
|
||||||
|
time="$min m $sec,$msec s"
|
||||||
|
else
|
||||||
|
time="$sec,$msec s"
|
||||||
|
fi
|
||||||
|
# elif [[ $sec != 0 ]]; then
|
||||||
|
# time="$sec,$msec s"
|
||||||
|
# else
|
||||||
|
# time="$((msec+0)) ms"
|
||||||
|
# fi
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
gifts=""
|
||||||
|
for i in $(seq $day); do gifts+=":gift:"; done
|
||||||
|
sed "s/DAY/$day/g" README.md.temp | sed "s/:gift:/$gifts/" > $day_dir/README.md
|
||||||
|
get_time
|
||||||
|
if [[ $? == 0 ]]; then
|
||||||
|
cd ..
|
||||||
|
printf "$FMT_VAR" "exectime" "$time"
|
||||||
|
sed -i "s/EXECTIME/$time/" $readme
|
||||||
|
else
|
||||||
|
cd ..
|
||||||
|
printf "$FMT_WARN" "No execution time determined"
|
||||||
|
sed -i '/.*EXECTIME.*/d' $readme
|
||||||
|
fi
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if [[ -z $1 ]]; then
|
||||||
|
printf "$FMT_WARN" "No comment provided"
|
||||||
|
sed -i "/<!-- COMMENT -->/d" $readme
|
||||||
|
else
|
||||||
|
printf "$FMT_VAR" "Comment" "'$1'"
|
||||||
|
sed -i "s|COMMENT|$1|" $readme
|
||||||
|
fi
|
||||||
|
|
||||||
|
find_lang py Python "python3 SOURCE"
|
||||||
|
find_lang rs Rust "rustc SOURCE\n./day${day_dir}"
|
||||||
|
find_lang cpp C++ "g++ SOURCE -o day${day_dir}\n ./day${day_dir}"
|
@ -1,38 +1,14 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
table="| Day | Language | Execuction time | Comment |\n"
|
table="| Day | Language | Execuction time | Comment |\n"
|
||||||
table+="|:---:|:---:| ---:| --- |\n"
|
table+="|:---:|:---:| ---:| --- |\n"
|
||||||
for day in $(seq -f "%02g" 1 25); do
|
for day in $(seq -f "%02g" 1 25); do
|
||||||
[[ ! -d "$day" ]] && continue
|
[[ ! -d "$day" ]] && continue
|
||||||
# echo $day
|
# echo $day
|
||||||
lang=$(grep -E "Today's language: "'\*\*[a-zA-Z0-9_]+\*\*' $day/README.md | sed -r 's/.*\*\*(.+)\*\*.*/\1/')
|
lang=$(grep -E "Today's language:" $day/README.md | sed -r 's/.*\*\*(.+)\*\*.*/\1/')
|
||||||
exe=$(find $day -type f -executable)
|
exectime=$(grep -E "Execution time:" $day/README.md | sed -r 's/.*\*\*(.+)\*\*.*/\1/')
|
||||||
if [[ $(echo $exe | wc -w) != 1 ]]; then
|
|
||||||
echo "Found multiple or no executables for day $day: '$exe'"
|
|
||||||
time=Error
|
|
||||||
else
|
|
||||||
time=$({ time ./$exe; } 2>&1 | tail -3 | head -1 | awk '{print $2}')
|
|
||||||
re='([[:digit:]]+)m([[:digit:]]+),([[:digit:]]+)s'
|
|
||||||
[[ $time =~ $re ]]
|
|
||||||
min=${BASH_REMATCH[1]}
|
|
||||||
sec=${BASH_REMATCH[2]}
|
|
||||||
msec=${BASH_REMATCH[3]}
|
|
||||||
if [[ $min != 0 ]]; then
|
|
||||||
time="$min m $sec,$msec s"
|
|
||||||
else
|
|
||||||
time="$sec,$msec s"
|
|
||||||
fi
|
|
||||||
# elif [[ $sec != 0 ]]; then
|
|
||||||
# time="$sec,$msec s"
|
|
||||||
# else
|
|
||||||
# time="$((msec+0)) ms"
|
|
||||||
# fi
|
|
||||||
fi
|
|
||||||
comment=$(grep -E "<!-- .* -->" $day/README.md | head -1 | sed -r 's/.*<!-- *(.*) *-->.*/\1/')
|
comment=$(grep -E "<!-- .* -->" $day/README.md | head -1 | sed -r 's/.*<!-- *(.*) *-->.*/\1/')
|
||||||
table+="| [$day]($day) | $lang | $time | $comment |\n"
|
table+="| [$day]($day) | $lang | $exectime | $comment |\n"
|
||||||
|
|
||||||
done
|
done
|
||||||
# remove table from readme
|
# remove table from readme
|
||||||
sed -i '/<!-- table begin -->/q' README.md
|
sed -i '/<!-- table begin -->/q' README.md
|
||||||
|
Loading…
Reference in New Issue
Block a user