Skip to content

Commit

Permalink
[Rust/2015/06] Add solution
Browse files Browse the repository at this point in the history
  • Loading branch information
Defelo committed Sep 23, 2024
1 parent 48dbd98 commit 483ee78
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 3 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@
|[**19**](https://adventofcode.com/2016/day/19) [<img height=12 src=".assets/py.svg">](Python/2016/19.py "Python solution for 2016/19")|[**20**](https://adventofcode.com/2016/day/20) [<img height=12 src=".assets/py.svg">](Python/2016/20.py "Python solution for 2016/20")|[**21**](https://adventofcode.com/2016/day/21) [<img height=12 src=".assets/py.svg">](Python/2016/21.py "Python solution for 2016/21")|[**22**](https://adventofcode.com/2016/day/22) [<img height=12 src=".assets/py.svg">](Python/2016/22.py "Python solution for 2016/22")|[**23**](https://adventofcode.com/2016/day/23) [<img height=12 src=".assets/py.svg">](Python/2016/23.py "Python solution for 2016/23")|[**24**](https://adventofcode.com/2016/day/24) [<img height=12 src=".assets/py.svg">](Python/2016/24.py "Python solution for 2016/24")|[**25**](https://adventofcode.com/2016/day/25) [<img height=12 src=".assets/py.svg">](Python/2016/25.py "Python solution for 2016/25")|
|26|27|28|29|30|31||

## [2015](https://adventofcode.com/2015) ([<img height=18 src=".assets/py.svg"> Python](Python/2015): 25/25 | [<img height=18 src=".assets/rs.svg"> Rust](Rust/2015): 5/25 | [<img height=18 src=".assets/ua.png"> Uiua](Uiua/2015): 3/25 | [<img height=18 src=".assets/nix.svg"> Nix](Nix/2015): 1/25)
## [2015](https://adventofcode.com/2015) ([<img height=18 src=".assets/py.svg"> Python](Python/2015): 25/25 | [<img height=18 src=".assets/rs.svg"> Rust](Rust/2015): 6/25 | [<img height=18 src=".assets/ua.png"> Uiua](Uiua/2015): 3/25 | [<img height=18 src=".assets/nix.svg"> Nix](Nix/2015): 1/25)
|Mo|Tu|We|Th|Fr|Sa|Su|
|-|-|-|-|-|-|-|
||[**1**](https://adventofcode.com/2015/day/1) [<img height=12 src=".assets/rs.svg">](Rust/2015/01.rs "Rust solution for 2015/01") [<img height=12 src=".assets/py.svg">](Python/2015/01.py "Python solution for 2015/01") [<img height=12 src=".assets/ua.png">](https://uiua.org/pad?src=JnNjCgotwqwuPUAoCuKKgygrMeKKl8KvMVwrKS8rCg== "Uiua solution for 2015/01") [<img height=12 src=".assets/nix.svg">](Nix/2015/01.nix "Nix solution for 2015/01")|[**2**](https://adventofcode.com/2015/day/2) [<img height=12 src=".assets/rs.svg">](Rust/2015/02.rs "Rust solution for 2015/02") [<img height=12 src=".assets/py.svg">](Python/2015/02.py "Python solution for 2015/02") [<img height=12 src=".assets/ua.png">](https://uiua.org/pad?src=JnNjCgriipwo4oqc4ouV4omgQHguKeKJoEBcbi4KCuKIqSgvKyspIOKJoeKKgygvw5d8w5cyLeKKgy_ihqUvK3zDt-KKgy_ihqUvw5d8w5cyLyviiaEvw5fil6sy4oavWzRdKQo= "Uiua solution for 2015/02")|[**3**](https://adventofcode.com/2015/day/3) [<img height=12 src=".assets/rs.svg">](Rust/2015/03.rs "Rust solution for 2015/03") [<img height=12 src=".assets/py.svg">](Python/2015/03.py "Python solution for 2015/03") [<img height=12 src=".assets/ua.png">](https://uiua.org/pad?src=JnNjCgrilr3iiIosInZePD4iCuKItSjiioLiioMoLeKIqT1AXixAdikoLeKIqT1APCxAPikpCgriiKko4qe74oqdKSDiioMo4oqC4oipKFwr4oqCMF8wKSDiiKko4o2J4oqiKeKHjC7ijYnihq_iioI6Ml8yw7cy4qe7LikgKFwr4oqCMF8wKQo= "Uiua solution for 2015/03")|[**4**](https://adventofcode.com/2015/day/4) [<img height=12 src=".assets/rs.svg">](Rust/2015/04.rs "Rust solution for 2015/04") [<img height=12 src=".assets/py.svg">](Python/2015/04.py "Python solution for 2015/04")|[**5**](https://adventofcode.com/2015/day/5) [<img height=12 src=".assets/rs.svg">](Rust/2015/05.rs "Rust solution for 2015/05") [<img height=12 src=".assets/py.svg">](Python/2015/05.py "Python solution for 2015/05")|[**6**](https://adventofcode.com/2015/day/6) [<img height=12 src=".assets/py.svg">](Python/2015/06.py "Python solution for 2015/06")|
||[**1**](https://adventofcode.com/2015/day/1) [<img height=12 src=".assets/rs.svg">](Rust/2015/01.rs "Rust solution for 2015/01") [<img height=12 src=".assets/py.svg">](Python/2015/01.py "Python solution for 2015/01") [<img height=12 src=".assets/ua.png">](https://uiua.org/pad?src=JnNjCgotwqwuPUAoCuKKgygrMeKKl8KvMVwrKS8rCg== "Uiua solution for 2015/01") [<img height=12 src=".assets/nix.svg">](Nix/2015/01.nix "Nix solution for 2015/01")|[**2**](https://adventofcode.com/2015/day/2) [<img height=12 src=".assets/rs.svg">](Rust/2015/02.rs "Rust solution for 2015/02") [<img height=12 src=".assets/py.svg">](Python/2015/02.py "Python solution for 2015/02") [<img height=12 src=".assets/ua.png">](https://uiua.org/pad?src=JnNjCgriipwo4oqc4ouV4omgQHguKeKJoEBcbi4KCuKIqSgvKyspIOKJoeKKgygvw5d8w5cyLeKKgy_ihqUvK3zDt-KKgy_ihqUvw5d8w5cyLyviiaEvw5fil6sy4oavWzRdKQo= "Uiua solution for 2015/02")|[**3**](https://adventofcode.com/2015/day/3) [<img height=12 src=".assets/rs.svg">](Rust/2015/03.rs "Rust solution for 2015/03") [<img height=12 src=".assets/py.svg">](Python/2015/03.py "Python solution for 2015/03") [<img height=12 src=".assets/ua.png">](https://uiua.org/pad?src=JnNjCgrilr3iiIosInZePD4iCuKItSjiioLiioMoLeKIqT1AXixAdikoLeKIqT1APCxAPikpCgriiKko4qe74oqdKSDiioMo4oqC4oipKFwr4oqCMF8wKSDiiKko4o2J4oqiKeKHjC7ijYnihq_iioI6Ml8yw7cy4qe7LikgKFwr4oqCMF8wKQo= "Uiua solution for 2015/03")|[**4**](https://adventofcode.com/2015/day/4) [<img height=12 src=".assets/rs.svg">](Rust/2015/04.rs "Rust solution for 2015/04") [<img height=12 src=".assets/py.svg">](Python/2015/04.py "Python solution for 2015/04")|[**5**](https://adventofcode.com/2015/day/5) [<img height=12 src=".assets/rs.svg">](Rust/2015/05.rs "Rust solution for 2015/05") [<img height=12 src=".assets/py.svg">](Python/2015/05.py "Python solution for 2015/05")|[**6**](https://adventofcode.com/2015/day/6) [<img height=12 src=".assets/rs.svg">](Rust/2015/06.rs "Rust solution for 2015/06") [<img height=12 src=".assets/py.svg">](Python/2015/06.py "Python solution for 2015/06")|
|[**7**](https://adventofcode.com/2015/day/7) [<img height=12 src=".assets/py.svg">](Python/2015/07.py "Python solution for 2015/07")|[**8**](https://adventofcode.com/2015/day/8) [<img height=12 src=".assets/py.svg">](Python/2015/08.py "Python solution for 2015/08")|[**9**](https://adventofcode.com/2015/day/9) [<img height=12 src=".assets/py.svg">](Python/2015/09.py "Python solution for 2015/09")|[**10**](https://adventofcode.com/2015/day/10) [<img height=12 src=".assets/py.svg">](Python/2015/10.py "Python solution for 2015/10")|[**11**](https://adventofcode.com/2015/day/11) [<img height=12 src=".assets/py.svg">](Python/2015/11.py "Python solution for 2015/11")|[**12**](https://adventofcode.com/2015/day/12) [<img height=12 src=".assets/py.svg">](Python/2015/12.py "Python solution for 2015/12")|[**13**](https://adventofcode.com/2015/day/13) [<img height=12 src=".assets/py.svg">](Python/2015/13.py "Python solution for 2015/13")|
|[**14**](https://adventofcode.com/2015/day/14) [<img height=12 src=".assets/py.svg">](Python/2015/14.py "Python solution for 2015/14")|[**15**](https://adventofcode.com/2015/day/15) [<img height=12 src=".assets/py.svg">](Python/2015/15.py "Python solution for 2015/15")|[**16**](https://adventofcode.com/2015/day/16) [<img height=12 src=".assets/py.svg">](Python/2015/16.py "Python solution for 2015/16")|[**17**](https://adventofcode.com/2015/day/17) [<img height=12 src=".assets/py.svg">](Python/2015/17.py "Python solution for 2015/17")|[**18**](https://adventofcode.com/2015/day/18) [<img height=12 src=".assets/py.svg">](Python/2015/18.py "Python solution for 2015/18")|[**19**](https://adventofcode.com/2015/day/19) [<img height=12 src=".assets/py.svg">](Python/2015/19.py "Python solution for 2015/19")|[**20**](https://adventofcode.com/2015/day/20) [<img height=12 src=".assets/py.svg">](Python/2015/20.py "Python solution for 2015/20")|
|[**21**](https://adventofcode.com/2015/day/21) [<img height=12 src=".assets/py.svg">](Python/2015/21.py "Python solution for 2015/21")|[**22**](https://adventofcode.com/2015/day/22) [<img height=12 src=".assets/py.svg">](Python/2015/22.py "Python solution for 2015/22")|[**23**](https://adventofcode.com/2015/day/23) [<img height=12 src=".assets/py.svg">](Python/2015/23.py "Python solution for 2015/23")|[**24**](https://adventofcode.com/2015/day/24) [<img height=12 src=".assets/py.svg">](Python/2015/24.py "Python solution for 2015/24")|[**25**](https://adventofcode.com/2015/day/25) [<img height=12 src=".assets/py.svg">](Python/2015/25.py "Python solution for 2015/25")|26|27|
Expand Down
83 changes: 83 additions & 0 deletions Rust/2015/06.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#![feature(test)]

use std::sync::LazyLock;

use regex::Regex;

type Input = Vec<Instruction>;

#[derive(Debug)]
struct Instruction {
cmd: Command,
start: Point,
end: Point,
}

#[derive(Debug)]
enum Command {
On,
Off,
Toggle,
}

type Point = (usize, usize);

fn setup(input: &str) -> Input {
input
.trim()
.lines()
.map(|line| {
static REGEX: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(r"^(turn on|turn off|toggle) (\d+),(\d+) through (\d+),(\d+)$").unwrap()
});
let caps = REGEX.captures(line).unwrap();
let cmd = match &caps[1] {
"turn on" => Command::On,
"turn off" => Command::Off,
"toggle" => Command::Toggle,
_ => unreachable!(),
};

let start = (caps[2].parse().unwrap(), caps[3].parse().unwrap());
let end = (caps[4].parse().unwrap(), caps[5].parse().unwrap());

Instruction { cmd, start, end }
})
.collect()
}

fn part1(input: &Input) -> usize {
let mut grid = vec![false; 1000 * 1000];
for Instruction { cmd, start, end } in input {
for x in start.0..=end.0 {
for y in start.1..=end.1 {
let i = y * 1000 + x;
grid[i] = match cmd {
Command::On => true,
Command::Off => false,
Command::Toggle => !grid[i],
}
}
}
}
grid.into_iter().filter(|&x| x).count()
}

fn part2(input: &Input) -> u64 {
let mut grid = vec![0u64; 1000 * 1000];
for Instruction { cmd, start, end } in input {
for x in start.0..=end.0 {
for y in start.1..=end.1 {
let i = y * 1000 + x;
grid[i] = match cmd {
Command::On => grid[i] + 1,
Command::Off => grid[i].saturating_sub(1),
Command::Toggle => grid[i] + 2,
}
}
}
}
grid.into_iter().sum()
}

aoc::main!(2015, 6);
2 changes: 1 addition & 1 deletion Rust/2015/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ aoc::year! {
"03.rs",
"04.rs",
"05.rs",
// "06.rs",
"06.rs",
// "07.rs",
// "08.rs",
// "09.rs",
Expand Down
3 changes: 3 additions & 0 deletions Rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ path = "2015/04.rs"
[[bin]]
name = "2015_05"
path = "2015/05.rs"
[[bin]]
name = "2015_06"
path = "2015/06.rs"

# 2020
[[bin]]
Expand Down

0 comments on commit 483ee78

Please sign in to comment.