Compare commits

...

3 Commits

Author SHA1 Message Date
Alfred Melch 7d77a1cfcf Solve 2021/02 3 years ago
Alfred Melch f1cdeded01 Restructure: split rust projects
Seperates each days into a seperate crate again.
3 years ago
Alfred Melch b9ef175cf6 Helper script: fetch puzzle description
Extended the helper script to fetch the puzzle description. It will be
put into a README.md. The input will now be stored into a input folder
along with examples extracted from the puzzle description.

Adjusted the parsing of the day from the folder name. It will now take
any number that appears in the folder name. Previously: folder name must
be a number, leading zeros will be stripped (e.g 01, 02). Now: there can
be other characters as well (e.g day01, day02)
3 years ago

@ -1,19 +0,0 @@
[package]
name = "aoc-2021"
version = "0.1.0"
edition = "2021"
[dependencies]
itertools = "0.10.1"
[lib]
name = "util"
path = "util/main.rs"
[[bin]]
name = "day-01-part-1"
path = "01/part1.rs"
[[bin]]
name = "day-01-part-2"
path = "01/part2.rs"

@ -3,7 +3,7 @@
version = 3
[[package]]
name = "aoc-2021"
name = "day01"
version = "0.1.0"
dependencies = [
"itertools",

@ -0,0 +1,17 @@
[package]
name = "day01"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
itertools = "0.10.1"
[[bin]]
name = "part1"
path = "src/part1.rs"
[[bin]]
name = "part2"
path = "src/part2.rs"

@ -0,0 +1,131 @@
https://adventofcode.com/2021/day/1
## \--- Day 1: Sonar Sweep ---
You're minding your own business on a ship at sea when the overboard alarm
goes off! You rush to see if you can help. Apparently, one of the Elves
tripped and accidentally sent the sleigh keys flying into the ocean!
Before you know it, you're inside a submarine the Elves keep ready for
situations like this. It's covered in Christmas lights (because of course it
is), and it even has an experimental antenna that should be able to track the
keys if you can boost its signal strength high enough; there's a little meter
that indicates the antenna's signal strength by displaying 0-50 _stars_.
Your instincts tell you that in order to save Christmas, you'll need to get
all _fifty stars_ by December 25th.
Collect stars by solving puzzles. Two puzzles will be made available on each
day in the Advent calendar; the second puzzle is unlocked when you complete
the first. Each puzzle grants _one star_. Good luck!
As the submarine drops below the surface of the ocean, it automatically
performs a sonar sweep of the nearby sea floor. On a small screen, the sonar
sweep report (your puzzle input) appears: each line is a measurement of the
sea floor depth as the sweep looks further and further away from the
submarine.
For example, suppose you had the following report:
[code]
199
200
208
210
200
207
240
269
260
263
[/code]
This report indicates that, scanning outward from the submarine, the sonar
sweep found depths of `199`, `200`, `208`, `210`, and so on.
The first order of business is to figure out how quickly the depth increases,
just so you know what you're dealing with - you never know if the keys will
get carried into deeper water by an ocean current or a fish or something.
To do this, count _the number of times a depth measurement increases_ from the
previous measurement. (There is no measurement before the first measurement.)
In the example above, the changes are as follows:
[code]
199 (N/A - no previous measurement)
200 ( _increased_ )
208 ( _increased_ )
210 ( _increased_ )
200 (decreased)
207 ( _increased_ )
240 ( _increased_ )
269 ( _increased_ )
260 (decreased)
263 ( _increased_ )
[/code]
In this example, there are _`7`_ measurements that are larger than the
previous measurement.
_How many measurements are larger than the previous measurement?_
Your puzzle answer was `1754`.
## \--- Part Two ---
Considering every single measurement isn't as useful as you expected: there's
just too much noise in the data.
Instead, consider sums of a _three-measurement sliding window_. Again
considering the above example:
[code]
199 A
200 A B
208 A B C
210 B C D
200 E C D
207 E F D
240 E F G
269 F G H
260 G H
263 H
[/code]
Start by comparing the first and second three-measurement windows. The
measurements in the first window are marked `A` (`199`, `200`, `208`); their
sum is `199 + 200 + 208 = 607`. The second window is marked `B` (`200`, `208`,
`210`); its sum is `618`. The sum of measurements in the second window is
larger than the sum of the first, so this first comparison _increased_.
Your goal now is to count _the number of times the sum of measurements in this
sliding window increases_ from the previous sum. So, compare `A` with `B`,
then compare `B` with `C`, then `C` with `D`, and so on. Stop when there
aren't enough measurements left to create a new three-measurement sum.
In the above example, the sum of each three-measurement window is as follows:
[code]
A: 607 (N/A - no previous sum)
B: 618 ( _increased_ )
C: 618 (no change)
D: 617 (decreased)
E: 647 ( _increased_ )
F: 716 ( _increased_ )
G: 769 ( _increased_ )
H: 792 ( _increased_ )
[/code]
In this example, there are _`5`_ sums that are larger than the previous sum.
Consider sums of a three-measurement sliding window. _How many sums are larger
than the previous sum?_

@ -0,0 +1,10 @@
199 (N/A - no previous measurement)
200 ( _increased_ )
208 ( _increased_ )
210 ( _increased_ )
200 (decreased)
207 ( _increased_ )
240 ( _increased_ )
269 ( _increased_ )
260 (decreased)
263 ( _increased_ )

@ -0,0 +1,10 @@
199 A
200 A B
208 A B C
210 B C D
200 E C D
207 E F D
240 E F G
269 F G H
260 G H
263 H

@ -0,0 +1,8 @@
A: 607 (N/A - no previous sum)
B: 618 ( _increased_ )
C: 618 (no change)
D: 617 (decreased)
E: 647 ( _increased_ )
F: 716 ( _increased_ )
G: 769 ( _increased_ )
H: 792 ( _increased_ )

@ -0,0 +1,3 @@
fn main() {
println!("Hello, world!");
}

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

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

@ -0,0 +1,82 @@
https://adventofcode.com/2021/day/2
## \--- Day 2: Dive! ---
Now, you need to figure out how to pilot this thing.
It seems like the submarine can take a series of commands like `forward 1`,
`down 2`, or `up 3`:
* `forward X` increases the horizontal position by `X` units.
* `down X` _increases_ the depth by `X` units.
* `up X` _decreases_ the depth by `X` units.
Note that since you're on a submarine, `down` and `up` affect your _depth_ ,
and so they have the opposite result of what you might expect.
The submarine seems to already have a planned course (your puzzle input). You
should probably figure out where it's going. For example:
[code]
forward 5
down 5
forward 8
up 3
down 8
forward 2
[/code]
Your horizontal position and depth both start at `0`. The steps above would
then modify them as follows:
* `forward 5` adds `5` to your horizontal position, a total of `5`.
* `down 5` adds `5` to your depth, resulting in a value of `5`.
* `forward 8` adds `8` to your horizontal position, a total of `13`.
* `up 3` decreases your depth by `3`, resulting in a value of `2`.
* `down 8` adds `8` to your depth, resulting in a value of `10`.
* `forward 2` adds `2` to your horizontal position, a total of `15`.
After following these instructions, you would have a horizontal position of
`15` and a depth of `10`. (Multiplying these together produces `_150_`.)
Calculate the horizontal position and depth you would have after following the
planned course. _What do you get if you multiply your final horizontal
position by your final depth?_
## \--- Part Two ---
Based on your calculations, the planned course doesn't seem to make any sense.
You find the submarine manual and discover that the process is actually
slightly more complicated.
In addition to horizontal position and depth, you'll also need to track a
third value, _aim_ , which also starts at `0`. The commands also mean
something entirely different than you first thought:
* `down X` _increases_ your aim by `X` units.
* `up X` _decreases_ your aim by `X` units.
* `forward X` does two things:
* It increases your horizontal position by `X` units.
* It increases your depth by your aim _multiplied by_ `X`.
Again note that since you're on a submarine, `down` and `up` do the opposite
of what you might expect: "down" means aiming in the positive direction.
Now, the above example does something different:
* `forward 5` adds `5` to your horizontal position, a total of `5`. Because your aim is `0`, your depth does not change.
* `down 5` adds `5` to your aim, resulting in a value of `5`.
* `forward 8` adds `8` to your horizontal position, a total of `13`. Because your aim is `5`, your depth increases by `8*5=40`.
* `up 3` decreases your aim by `3`, resulting in a value of `2`.
* `down 8` adds `8` to your aim, resulting in a value of `10`.
* `forward 2` adds `2` to your horizontal position, a total of `15`. Because your aim is `10`, your depth increases by `2*10=20` to a total of `60`.
After following these new instructions, you would have a horizontal position
of `15` and a depth of `60`. (Multiplying these produces `_900_`.)
Using this new interpretation of the commands, calculate the horizontal
position and depth you would have after following the planned course. _What do
you get if you multiply your final horizontal position by your final depth?_

@ -0,0 +1,6 @@
forward 5
down 5
forward 8
up 3
down 8
forward 2

File diff suppressed because it is too large Load Diff

@ -0,0 +1,65 @@
use std::env;
use std::io::stdin;
use std::io::BufRead;
fn main() {
let args: Vec<String> = env::args().collect();
if args.len() > 1 && args[1] == "part1" {
part1();
} else {
part2();
}
}
fn part1() {
let mut horizontal = 0;
let mut depth = 0;
for line in stdin().lock().lines() {
let line_result = line.unwrap();
let vec: Vec<&str> = line_result.split(' ').collect();
let direction = vec[0];
let amount: i32 = vec[1].parse().unwrap();
match direction {
"forward" => horizontal += amount,
"down" => depth += amount,
"up" => depth -= amount,
_ => (),
}
dbg!(direction, amount);
}
dbg!(horizontal, depth, horizontal * depth);
}
fn part2() {
let mut horizontal = 0;
let mut depth = 0;
let mut aim = 0;
for line in stdin().lock().lines() {
let line_result = line.unwrap();
let vec: Vec<&str> = line_result.split(' ').collect();
let direction = vec[0];
let amount: i32 = vec[1].parse().unwrap();
match direction {
"forward" => {
horizontal += amount;
depth += aim * amount
}
"down" => aim += amount,
"up" => aim -= amount,
_ => (),
}
dbg!(direction, amount);
}
dbg!(horizontal, depth, horizontal * depth);
}

@ -1,11 +0,0 @@
use std::fs::File;
use std::io::BufRead;
use std::io::BufReader;
use std::io::Lines;
pub mod math;
pub fn file_lines(path: String) -> Lines<BufReader<File>> {
let input = File::open(path).unwrap();
return BufReader::new(input).lines();
}

@ -1,7 +0,0 @@
pub fn sum(x: i32, y: i32) -> i32 {
x + y
}
pub fn sub(x: i32, y: i32) -> i32 {
x - y
}

@ -3,14 +3,34 @@
PARENT_DIR=$(dirname $PWD)
YEAR=${PARENT_DIR##*/}
DAY_RAW=${PWD##*/}
DAY=$((10#$DAY_RAW))
DAY=$((10#${DAY_RAW//[^0-9]/}))
SESSION_KEY=$(cat ~/.aocrc)
echo "Fetching ${YEAR} ${DAY}"
echo $SESSION_KEY
curl -b session=$SESSION_KEY https://adventofcode.com/${YEAR}/day/${DAY}/input > input
URL_DAY="https://adventofcode.com/${YEAR}/day/${DAY}"
URL_INPUT="$URL_DAY/input"
echo "Visit: https://adventofcode.com/${YEAR}/day/${DAY}"
mkdir -p input
echo -e "$URL_DAY\n" > README.md
curl -b session=$SESSION_KEY $URL_DAY | sed -n '/<article class="day-desc">/,/<\/article>/p' | html2markdown --mark-code >> README.md
curl -b session=$SESSION_KEY $URL_INPUT > input/input
cat README.md
echo "Extracting example code blocks..."
# extract example code blocks
cat README.md |
sed -n '/\[code\]/,/\[\/code\]/p' | # extract code blocks
sed '/^\s*$/d' | # remove empty lines
sed '/^\[\/code\]/d' | # remove closing bracket
sed 's/^ //' | # remove indentation
csplit - --suppress-matched --elide-empty-files --prefix='input/example' --suffix='%d' '/\[code\]/' '{*}'
echo "done."
echo
echo "Visit: $URL_DAY"

Loading…
Cancel
Save