Solutions for 2022, as well as 2015-2018 and 2019 up to day 11

This commit is contained in:
Chris Alge 2023-03-12 15:20:02 +01:00
commit 1895197c49
722 changed files with 375457 additions and 0 deletions

View file

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

View file

@ -0,0 +1,69 @@
\--- Day 8: Two-Factor Authentication ---
----------
You come across a door implementing what you can only assume is an implementation of [two-factor authentication](https://en.wikipedia.org/wiki/Multi-factor_authentication) after a long game of [requirements](https://en.wikipedia.org/wiki/Requirement) [telephone](https://en.wikipedia.org/wiki/Chinese_whispers).
To get past the door, you first swipe a keycard (no problem; there was one on a nearby desk). Then, it displays a code on a [little screen](https://www.google.com/search?q=tiny+lcd&tbm=isch), and you type that code on a keypad. Then, presumably, the door unlocks.
Unfortunately, the screen has been smashed. After a few minutes, you've taken everything apart and figured out how it works. Now you just have to work out what the screen *would* have displayed.
The magnetic strip on the card you swiped encodes a series of instructions for the screen; these instructions are your puzzle input. The screen is *`50` pixels wide and `6` pixels tall*, all of which start *off*, and is capable of three somewhat peculiar operations:
* `rect AxB` turns *on* all of the pixels in a rectangle at the top-left of the screen which is `A` wide and `B` tall.
* `rotate row y=A by B` shifts all of the pixels in row `A` (0 is the top row) *right* by `B` pixels. Pixels that would fall off the right end appear at the left end of the row.
* `rotate column x=A by B` shifts all of the pixels in column `A` (0 is the left column) *down* by `B` pixels. Pixels that would fall off the bottom appear at the top of the column.
For example, here is a simple sequence on a smaller screen:
* `rect 3x2` creates a small rectangle in the top-left corner:
```
###....
###....
.......
```
* `rotate column x=1 by 1` rotates the second column down by one pixel:
```
#.#....
###....
.#.....
```
* `rotate row y=0 by 4` rotates the top row right by four pixels:
```
....#.#
###....
.#.....
```
* `rotate column x=1 by 1` again rotates the second column down by one pixel, causing the bottom pixel to wrap back to the top:
```
.#..#.#
#.#....
.#.....
```
As you can see, this display technology is extremely powerful, and will soon dominate the tiny-code-displaying-screen market. That's what the advertisement on the back of the display tries to convince you, anyway.
There seems to be an intermediate check of the voltage used by the display: after you swipe your card, if the screen did work, *how many pixels should be lit?*
Your puzzle answer was `110`.
\--- Part Two ---
----------
You notice that the screen is only capable of displaying capital letters; in the font it uses, each letter is `5` pixels wide and `6` tall.
After you swipe your card, *what code is the screen trying to display?*
Your puzzle answer was `ZJHRKCPLYJ`.
Both parts of this puzzle are complete! They provide two gold stars: \*\*
At this point, all that is left is for you to [admire your Advent calendar](/2016).
If you still want to see it, you can [get your puzzle input](8/input).

View file

@ -0,0 +1,90 @@
#[derive(Debug)]
struct Screen {
pixels: [[u8; 50]; 6],
}
impl Screen {
fn new() -> Self {
Self {
pixels: [[0; 50]; 6],
}
}
fn rect(&mut self, (x, y): (&str, &str)) {
let (x, y) = (x.parse().unwrap(), y.parse().unwrap());
(0..x).for_each(|row| (0..y).for_each(|col| self.pixels[col][row] = 1));
}
fn rotate_row(&mut self, y: usize, by: usize) {
let old_row = self.pixels[y];
(0..50).for_each(|x| self.pixels[y][x] = old_row[(50 + x - by) % 50]);
}
fn rotate_column(&mut self, x: usize, by: usize) {
let old_col: Vec<u8> = self.pixels.iter().map(|row| row[x]).collect();
(0..6).for_each(|y| self.pixels[y][x] = old_col[(6 + y - by) % 6]);
}
fn perform(&mut self, line: &str) {
let components: Vec<_> = line.trim().split(' ').collect();
assert!((2..=5).contains(&components.len()));
match (components[0], components[1]) {
("rect", dim) => self.rect(dim.split_once('x').unwrap()),
("rotate", "row") => self.rotate_row(components[2][2..].parse().unwrap(), components[4].parse().unwrap()),
("rotate", "column") => self.rotate_column(components[2][2..].parse().unwrap(), components[4].parse().unwrap()),
_ => panic!("Unexpected Command: {line}"),
}
}
fn render(&self) -> String {
self.pixels.iter().map(|row| row.iter().map(|px| match px { 0=>" ", _=>"#",}).collect::<String>() + "\n").collect()
}
}
pub fn run(input: &str) -> (usize, String) {
let mut the_screen = Screen::new();
input.lines().for_each(|line| the_screen.perform(line));
let first = the_screen.pixels.into_iter().map(|row| row.into_iter().map(|pixel| pixel as usize).sum::<usize>()).sum();
let second = the_screen.render();
(first, second)
}
#[cfg(test)]
mod tests {
use super::*;
use std::fs::read_to_string;
fn read_file(name: &str) -> String {
read_to_string(name).expect(&format!("Unable to read file: {}", name)[..])
}
#[test]
fn test_sample() {
let sample_input = read_file("tests/sample_input");
let mut the_screen = Screen::new();
sample_input.lines().for_each(|line| the_screen.perform(line));
assert_eq!(run(&sample_input), (6,
"
# #
# #
#
#
"[1..].to_string()));
}
#[test]
fn test_challenge() {
let challenge_input = read_file("tests/challenge_input");
assert_eq!(run(&challenge_input), (110,
"
#### ## # # ### # # ## ### # # # ##
# # # # # # # # # # # # # # # #
# # #### # # ## # # # # # # #
# # # # ### # # # ### # # #
# # # # # # # # # # # # # # # #
#### ## # # # # # # ## # #### # ##
"[1..].to_string()));
}
}

View file

@ -0,0 +1,153 @@
rect 1x1
rotate row y=0 by 2
rect 1x1
rotate row y=0 by 5
rect 1x1
rotate row y=0 by 3
rect 1x1
rotate row y=0 by 3
rect 2x1
rotate row y=0 by 5
rect 1x1
rotate row y=0 by 5
rect 4x1
rotate row y=0 by 2
rect 1x1
rotate row y=0 by 2
rect 1x1
rotate row y=0 by 5
rect 4x1
rotate row y=0 by 3
rect 2x1
rotate row y=0 by 5
rect 4x1
rotate row y=0 by 2
rect 1x2
rotate row y=1 by 6
rotate row y=0 by 2
rect 1x2
rotate column x=32 by 1
rotate column x=23 by 1
rotate column x=13 by 1
rotate row y=0 by 6
rotate column x=0 by 1
rect 5x1
rotate row y=0 by 2
rotate column x=30 by 1
rotate row y=1 by 20
rotate row y=0 by 18
rotate column x=13 by 1
rotate column x=10 by 1
rotate column x=7 by 1
rotate column x=2 by 1
rotate column x=0 by 1
rect 17x1
rotate column x=16 by 3
rotate row y=3 by 7
rotate row y=0 by 5
rotate column x=2 by 1
rotate column x=0 by 1
rect 4x1
rotate column x=28 by 1
rotate row y=1 by 24
rotate row y=0 by 21
rotate column x=19 by 1
rotate column x=17 by 1
rotate column x=16 by 1
rotate column x=14 by 1
rotate column x=12 by 2
rotate column x=11 by 1
rotate column x=9 by 1
rotate column x=8 by 1
rotate column x=7 by 1
rotate column x=6 by 1
rotate column x=4 by 1
rotate column x=2 by 1
rotate column x=0 by 1
rect 20x1
rotate column x=47 by 1
rotate column x=40 by 2
rotate column x=35 by 2
rotate column x=30 by 2
rotate column x=10 by 3
rotate column x=5 by 3
rotate row y=4 by 20
rotate row y=3 by 10
rotate row y=2 by 20
rotate row y=1 by 16
rotate row y=0 by 9
rotate column x=7 by 2
rotate column x=5 by 2
rotate column x=3 by 2
rotate column x=0 by 2
rect 9x2
rotate column x=22 by 2
rotate row y=3 by 40
rotate row y=1 by 20
rotate row y=0 by 20
rotate column x=18 by 1
rotate column x=17 by 2
rotate column x=16 by 1
rotate column x=15 by 2
rotate column x=13 by 1
rotate column x=12 by 1
rotate column x=11 by 1
rotate column x=10 by 1
rotate column x=8 by 3
rotate column x=7 by 1
rotate column x=6 by 1
rotate column x=5 by 1
rotate column x=3 by 1
rotate column x=2 by 1
rotate column x=1 by 1
rotate column x=0 by 1
rect 19x1
rotate column x=44 by 2
rotate column x=40 by 3
rotate column x=29 by 1
rotate column x=27 by 2
rotate column x=25 by 5
rotate column x=24 by 2
rotate column x=22 by 2
rotate column x=20 by 5
rotate column x=14 by 3
rotate column x=12 by 2
rotate column x=10 by 4
rotate column x=9 by 3
rotate column x=7 by 3
rotate column x=3 by 5
rotate column x=2 by 2
rotate row y=5 by 10
rotate row y=4 by 8
rotate row y=3 by 8
rotate row y=2 by 48
rotate row y=1 by 47
rotate row y=0 by 40
rotate column x=47 by 5
rotate column x=46 by 5
rotate column x=45 by 4
rotate column x=43 by 2
rotate column x=42 by 3
rotate column x=41 by 2
rotate column x=38 by 5
rotate column x=37 by 5
rotate column x=36 by 5
rotate column x=33 by 1
rotate column x=28 by 1
rotate column x=27 by 5
rotate column x=26 by 5
rotate column x=25 by 1
rotate column x=23 by 5
rotate column x=22 by 1
rotate column x=21 by 2
rotate column x=18 by 1
rotate column x=17 by 3
rotate column x=12 by 2
rotate column x=11 by 2
rotate column x=7 by 5
rotate column x=6 by 5
rotate column x=5 by 4
rotate column x=3 by 5
rotate column x=2 by 5
rotate column x=1 by 3
rotate column x=0 by 4

View file

@ -0,0 +1,4 @@
rect 3x2
rotate column x=1 by 1
rotate row y=0 by 4
rotate column x=1 by 1