mirror of
https://github.com/danbulant/adventOfCode
synced 2026-05-22 21:58:45 +00:00
days 12, 14 and 15
This commit is contained in:
parent
c664e38c94
commit
e723ec5192
16 changed files with 1594 additions and 0 deletions
12
2023/12/haskelltest.py
Normal file
12
2023/12/haskelltest.py
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
import subprocess
|
||||
|
||||
for line in open("./input2"):
|
||||
chars = line.split(" ")[0]
|
||||
groups = [int(x) for x in line.split(" ")[1].split(",")]
|
||||
regex1 = "\.*" + ("".join([f"#{'{'}{number}{'}'}\.+" for number in groups]))[:-3] + "\.*"
|
||||
regex2 = chars.replace("?", "[#.]").replace(".", "\.")
|
||||
# docker exec -it 055a5629a751 "/root/.cabal/bin/genex"
|
||||
command = f"/root/.cabal/bin/genex \"{regex1}\" \"{regex2}\" | wc -l"
|
||||
print(command)
|
||||
found = subprocess.check_output(["docker", "exec", "-it", "055a5629a751", "bash", "-c", command])
|
||||
print(line, regex1, regex2, found)
|
||||
6
2023/12/input1
Normal file
6
2023/12/input1
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
???.### 1,1,3
|
||||
.??..??...?##. 1,1,3
|
||||
?#?#?#?#?#?#?#? 1,3,1,6
|
||||
????.#...#... 4,1,1
|
||||
????.######..#####. 1,6,5
|
||||
?###???????? 3,2,1
|
||||
1000
2023/12/input2
Normal file
1000
2023/12/input2
Normal file
File diff suppressed because it is too large
Load diff
41
2023/12/part1.py
Normal file
41
2023/12/part1.py
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
|
||||
def find_groups(str: str):
|
||||
groups = []
|
||||
cur = 0
|
||||
for c in str:
|
||||
if c == "#":
|
||||
cur += 1
|
||||
if c == ".":
|
||||
if cur > 0:
|
||||
groups.append(cur)
|
||||
cur = 0
|
||||
if cur > 0:
|
||||
groups.append(cur)
|
||||
return groups
|
||||
|
||||
def gen_parts(string: str):
|
||||
max_num = 2 ** string.count("?")
|
||||
for i in range(max_num):
|
||||
substr = string
|
||||
# replace each ? with a 0 or 1 based on i
|
||||
for j in range(string.count("?")):
|
||||
substr = substr.replace("?", str("." if (i >> j & 1) else "#"), 1)
|
||||
yield substr
|
||||
|
||||
|
||||
count = 0
|
||||
|
||||
for line in open("./input2"):
|
||||
chars = line.split(" ")[0]
|
||||
groups = [int(x) for x in line.split(" ")[1].split(",")]
|
||||
|
||||
print(line)
|
||||
|
||||
for part in gen_parts(chars):
|
||||
found = find_groups(part)
|
||||
# print("part", part, found)
|
||||
if found == groups:
|
||||
# print("found", part)
|
||||
count += 1
|
||||
|
||||
print(count)
|
||||
1
2023/14/.gitignore
vendored
Normal file
1
2023/14/.gitignore
vendored
Normal file
|
|
@ -0,0 +1 @@
|
|||
target
|
||||
7
2023/14/Cargo.lock
generated
Normal file
7
2023/14/Cargo.lock
generated
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "fourteenth"
|
||||
version = "0.1.0"
|
||||
8
2023/14/Cargo.toml
Normal file
8
2023/14/Cargo.toml
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "fourteenth"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
10
2023/14/input1
Normal file
10
2023/14/input1
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
O....#....
|
||||
O.OO#....#
|
||||
.....##...
|
||||
OO.#O....O
|
||||
.O.....O#.
|
||||
O.#..O.#.#
|
||||
..O..#O..O
|
||||
.......O..
|
||||
#....###..
|
||||
#OO..#....
|
||||
100
2023/14/input2
Normal file
100
2023/14/input2
Normal file
|
|
@ -0,0 +1,100 @@
|
|||
..O.O..O.#.#..#......O.....O.#.O..O.OO.O..OO...O..#.#OO......O.#..#OOOO..O.....O#.O.OO.O.O#......OO.
|
||||
..O........#O..O...O....O.......#.....#.#.O..#O#...........##.O.....O#.##O..#....#.#.....##.O.....#.
|
||||
......O..####...#O.#.O.#.#.O....O..#.....O.#O...#.#.#.....O#...#...O.....O....O....O........O.O...#.
|
||||
.O.O.O.....#......O...O..#....#...O##.OO.O.O.O##.O#O#.#.O##...OOO.....O...O....OO.#.....#..#....##..
|
||||
.#..O...O...........#....#.O#.....O.........O....OO.O.#.O#O#.#..O##...O...O...O....#OO.O..O#..#.....
|
||||
#.O#..#.....O###..##.....O..#....O#OO.OO......O..O.#....OO.OO#.O...O....#.#O..O.OO..O#.#.O..........
|
||||
O.#.O.OO...O..OO.O..#O......##O.O#.OO...#.......O...............OO.O..O.....O..#.O....O...#O..#..O..
|
||||
OO.##...#....O....#.##O..#....O#O.#......O##..#.O..O.#O#..##..#.O.O.O.#O.....#O.O.O....O......#...#O
|
||||
.##.........#.O#..#.#.#.#..O..#....OO.O.#O..#.............#....O..O.O.....O..........#.....#.O..O.O.
|
||||
..#......O.O#..O..#O.O..O.O..O#......#O...O........OO...O.#.OO...O..........O....O.#........#O....#O
|
||||
..#....#..#..O.O.#..#.#.......#.O.#.O#.#.#....####OO....##.O..#.OO#.##.#..O#.#....OOO#O.O.......#..O
|
||||
O..#OOO.O..#..O......#O#..O............#..O.OO.....#..O#.##...O...#....O#...#O.O..##..O.OO....#OOO.O
|
||||
OO#O.#..#..#.#.OOO.O#.O.#.O..O.##..#.#O#....#O.....#..O........#......#..#..O.#.O..O#OO#O....O.O.#.O
|
||||
.O.OO.......O..O........#.....#...##....##..#.........#.......#.OO.#..#....#..O.O.#.O##........#..O.
|
||||
.....#...#O......#.....O.#..#O##O....#O....#...##O.....#.....#....OO#...O....O#O...#.#.#.####.OO.#..
|
||||
.O#.O.O...##.O..O.O..#..O##.OO.#.#..........#........#.#.#....O.....O.O.#..O...O#.#.O....#.#...#.#..
|
||||
.#.#O............O#....#O.O##......#.OOO##.O.O.....OO##OO............O.O....##OO.O#.....#.O.#..O#...
|
||||
O.O.O......#OO..O#..O.#.#.#.O#...#.OO.O.O#OO.O.#.O....#...O#....O..O#...O...##.OO#..O....##O.#..O.O#
|
||||
.#.#........#...OOO#..O#O..#..O..O.O#..O#...#..#O.............O#.#O..O.OOOO.....OO##.#.O.###.#...O..
|
||||
.....O...OO#.O..O.O..##.#...O#..O#...O...O...O.###OO........#..O.O..OO#..#....#.##..O#..#......##..O
|
||||
OO.....#O#.#..#...#..OO.#..#O.......#.O.O.....O......O..OO#..........OO....O.....#.#O.#O.O..O..O....
|
||||
#.O.....#...O......#.#...#.OO....#O.O.O......O..OOO..O#.#...O#.O##..OO.OO.O##.#...#.O...#....OOOOO#O
|
||||
##O...#.O.#.O#.O##O#...#.#.O....#.........#......#O#.#O......#O..#O..#..#......O.....O.###O..O.O....
|
||||
O..##.#....O...OO#..#...OO##..#O.#O...O.................#..O..#..O.O.O###.O.O.#....O...#.O.......#..
|
||||
O#..#....O....O....OO.....#.O#.....#.......O..O.....O#OO..O..O..##...#O#.O............#.#.#..#......
|
||||
.OO..#....#...O..O....#O.......OO.##.O..O.#.O.O.#O.##.......O#......O...O...O.O##O##..#..#O.......#.
|
||||
OO#.OO.......#...#.#.O#.....#O.###.O...O#..O#..#..O#..........#......O........O..#...O#...OO#O.OO...
|
||||
.#.#.O.......##.#.O....#..###..OO#..#..O#O...............O.O...O#.#.....#OO..#O............O.O..O...
|
||||
.....#O..#.O......#O..O...........O.....O...O.#..O.O.#O#.#..OOO##..OO#....#O..#.#..#....O..O..O.#O..
|
||||
..O.#O....#.O..#O##.O##.#O.....#..#.....O...##..O........##.O.O......O...O#O###..#OO......#.OO.#O.#.
|
||||
OO..#........#..........O..##...#....OO..O..O...O.O.....O#.O......O...O.O.O..#.O.......#.O...#..#.#.
|
||||
.....#...#.....#.....###..OO...O.O#.O.#O..#...........OO....#O.O.O.......OO...OO...OO....#O..O#.O#..
|
||||
..O.O#.##...#.##.O..#.......#..#OO.OO.......O#OO..##...O##O.O..##.O..#O..#O...O..##..#.O#.##..O.O..O
|
||||
..OO.OO..O#.O#..O..O.#.O...#O##.O#....O....#..O#.O#O..OO.OO##O..OO..#..#.O.O#..#....O#O.#...O.......
|
||||
..O..#.....O.#..OO.....#..O....#...O.......O##.#..#OO....O...O..O.O#O#..O....O..#.#.OO.....##OO.....
|
||||
#.O.OOO...O.....O#.##.O..O#..#...OO..#OO#.#..O#....O..###...O#....O................#....OO#....O..#.
|
||||
.O..#...O#.#..#.O.##..####.#..#..O..#.......#......O....#.....O...#.O.#.OO.O...OO..#....O#.OO....O.O
|
||||
O.#..OO.....O.....O.O.#...#.OOO..####....O.O###....O.#.#O.#O#.#O..#.......O...........O...OO.....O..
|
||||
........O.OO.....O.O.#.O.O..O#.O.....#.....#....#..O......OOO#.#.#O........O.O.#..O...#OOO..#......O
|
||||
OO.....O#..#.#...OO...#...#......O...#..O.O#...#.#.O#..O.#O....#...OO#..#..#.OO.####....O#..#..O#...
|
||||
..O#.###OO.O...O#O....OOO...O#...O.OO...O#.#O.#..O......#..O#..O..#O...#O.....##..O#..O..##.#OO..O..
|
||||
#O.O.....#O#O#......#.O.OOOO....#..O..O.#.O.O...#O##.O....O..####OO#...#.#.....O..O.OO..##O#........
|
||||
#...#.#O.......O....O#.##..#O#..#.............O....O..#...OO.O..O#...O.#O...O..........#.O#..##OO#..
|
||||
.#.O..O.#O..#O#O##..O..........O#O#..##.#.OO.O.#...#.#.O....#..O#.#..#.#..#........#...O#......##..#
|
||||
...#OO.#.O#.......#...OO#..O#..O....#....#...O.#.OO.O......O...OOO......#.#.O..#..#..##O...O.#.#.O..
|
||||
#..#...#.#.......O...#.O....O##...O#O.#.#..#..##..O.#O.#O....O..O.O.#...OOO..#...OO.#.O##.#.#..O....
|
||||
..#...O....#.....O.O.....O........#..O....#..#....##.#O.#.#OO.O.OO.O..O..#.OO....O..O#..#...#..O#.O.
|
||||
...O###O...O..O#OO#.##.#O..O...#.#O...O#.OO..O...#.#O..O.OO#O...#O.#....#O#...O..O..#O.##.#.....O#..
|
||||
.##.....O....O...#.....O....#...............O.#.##..#.O........O#.O.#O#.OO#..O.#.O.O#.OO....#..O....
|
||||
....#...##O...O...O.O....#....OO#OO#.##..#OOO.....#.O..OOO#..##.......O.....O..O#.#...#.....OOO.....
|
||||
..O..O...#....O.O#O#.###..#...#O.#O#..##.O...##.#................O#....O.O..OO......OO.O##O......#..
|
||||
..#O..O.....O.#.O..........#..#..O...OO#OO...#..O..OO#....O..O..#O.#O..#..#...#...O............#.O#.
|
||||
.O......#.O.#.O...O.............#.#.#...#..OO........O..#.O.#.O.OO...#O.O#.............O#.#....O..OO
|
||||
####.#OO.#....#O.O..O.##.O#..#....O..O#......O.....O.#.#.O..O....O...O.O.........##....#....OO.O..O.
|
||||
##.OO.O#O.O.O.###O...###.O.O....O...#.........OO.#.#OOO#O..#O.........#....OO.....O...O.#.O.O..#.O..
|
||||
.......#.OO#.O.#.O#..O##O...O.###.O#....#.#O#.OO..#.......#..###..##.#O..O.O.........O.#OO.#.#.#.O..
|
||||
..#.O.#...O...##.O.O.#.......O............O.O..O....O...O#..O.#....O.#...O#...OO.#...#OO..##OO..O.#O
|
||||
#..OOO...O.OO.....O..O#...O....#..O..#....#.##OO...#..#.O....O........#OO....OOOO...##O....O.#.##..#
|
||||
.........#..#OO...OO....O.O#O##..O..O...O..#O#.O#......O..#..##..#......O.....O.......#..#.#.OOO....
|
||||
#...#..##...##.O...#.#O......OO....#...#.OO.OOO.##..##...O#O.#O....#.#....##...O.O..O......O...O....
|
||||
....O...#....O.OO.....OO............O#..#.......O...OO..O.O...#.#..##.....#.......O....O.....O..O..#
|
||||
O.....#.##...O.....OO.....##O.......#.O.O.#O.OO..O......O....##OO#...#O#..##...O.#...O.....#.O.O.#.O
|
||||
.O#..#....#.#..#.O.O.....O...#..##.O...#..O....O.O#O..O...#O...#.O..........O.O..O.##O#.#O##........
|
||||
.O#.#.#..#..#O...OO....#..OOO.......#..OOO.#O....O....O...#...#O##.....#.O.O#..#...O.OO..OO...#..O..
|
||||
.........O.OO...........O#..#..O##.......#.....O.#OO.....O...O..##...O...O...O#...#...O..OO....O....
|
||||
O#.....#...#....#....#O#....#..##OO#.O.#O...O#....O..OO#O.OO....#O#.#.O.#..O..##OO.O....##.O.#.O#O..
|
||||
O.#...O.O.#..##O..O.O.#O.#O.O##O#..#.#O.O..O..OOO...O#.#.#...OO.......#.....#O..##..##..O#......#...
|
||||
.O..#...O.O..#O...O.O.O..O.........#..OO..#..#O.#.O.OO.#O.OO.##.......O.........#...O.#OO....OOO.O..
|
||||
...#O#....O#....O.#......O..........O....###....OO#O.......O..OO....#O...O.OO.O..O......#...O.....##
|
||||
O.##.#...##..........O#...#O.O............#..O..#O#O#OO.O..O.......O#.....O#...O..O###OOO.O.#.....#.
|
||||
O#.#O..O..#...O#..O.#..#..#.......O.#...O.........#.OO..O..O#O...O#..#O..O#...O#.#..........#O.O...#
|
||||
O..####.....#....#OO#.O#O..#..#...O..##....O.......#.O.OO....O...O.......O#O..#.....#.#......O.O....
|
||||
..O#..O...O#..O.......#..#O#...##.......O.#O.#..O.#..O...###.....O.O...#.....#.#......O...OOO.....#.
|
||||
.....#O...#..#..O..#.OO#..#.O......OO.#..O#.###O.....#.OO..........OOO..##OO...##..OOO#O.#O.##......
|
||||
O...#..O.OO.O..OO..#..OO.#....#....#.O.....#..#...O...OO#........##O.......O..#.#.......OO#...O#.OO.
|
||||
O...##............O...#..#OO#O........O.#..O....O#..OOO.....O.O........##.##.....##..#.#O..##....O..
|
||||
..#...#...OO..O..O...OO.....O.O.O....O.....OO.OOO..O...#O...O.O..#...O.##....#..#...OO##.O.O#.#.#.#.
|
||||
.OO.....#.#...O.#...#.O.OO..###..O..#O.#.O##..O...O.O..#O.O.#...O...#.#O.O.O...#.#....#..O.......#.O
|
||||
...O.O#O......O.........OO..#O..OO.....OO##O.O..#.O##O.O.#.....O..#.#.....OO#.#.O.#.O#...#O.#....O#.
|
||||
..#...O..#.O...#.#..##..O..O..O.O.O.O...O.O..OO......O....#..O.O#...##..OO..O.#O..........#....#O.#.
|
||||
.O...O##.#O.OO##.O..#.#.....O#....#....#O.O#.##....#..................O#..O.##.#OO..O..O##O.O.O.....
|
||||
.OO......O##...O#.O......OO.OO.#..OO#..O..#.......O#...O.OO......OO##O.O...........#...#OOOO.#.O..#.
|
||||
..#.O...#.#..O.....#.O.O#.O...O#.......O.#.O#...O.....#....#.#O...O..O...O#.O..........#O...#....#O.
|
||||
.OO.......O#.#........#O..#...........OO.#........#OO.OO#....#..#O#..........OO.O..O.O.....#..#O....
|
||||
.OO#.......#.#.#..#.OO.#.O#.#.O....O..OO...O.#...O#O..#.O.....O#.OOO.#..O..O..#.#OO.#....##O........
|
||||
.#...O#O....O#..OOO....#....#......OO..#OOO....O.#....O...O.#..O..O.#OO..O#...#.O.....#......###OO..
|
||||
O...##.......#......O#OO.#O#O#O.O.O.O...O...#...O.#..O.......O#...#O.O##..#.O......##...#.O#.......O
|
||||
#..#O#.......OO...OO.....#.#...OO..O.O..O#.OO..O#...O......#.##O..#.#...O..O#...#.#....#O.#O...O...#
|
||||
..#.OO##O.O.O...##......##..#.###.O....##.O..O#.O.#..O..O.O..#.O#O#..O................O.....O......O
|
||||
OO...O#O.OO..OO..O...#....#..O...O###.O...#.OO..O#....O..OO.#OO...........O.O.##..OO#.O......O.#..O.
|
||||
..OO...#..#.O.O.....##..#O#.O...O..O#.##..#.#..#...OO.#...#..........#....O........O.#O#.....O....#.
|
||||
#...O..O..###O.....O......O..O.#O..#......OO.....##....#.OO#.O..#..O..##.#..O..O....##..#O#....O...#
|
||||
O#.#..O..O.#.OOO....O.....O....O..O#....#O#.O...O##..OO.#..#.........####..#O#O#.O.O...O.O.O.#.....O
|
||||
.O...O......#.......OO...O.#.....##.O...O#...O.#..O.O#O....OO.#....O...#O#..#....OO...#.#......O.O..
|
||||
O..OO....O.O..#.O...O...OO......#.#..O..O.O.O.#.#.O.....O......#..#..##.O.##O#.#O........O.O....OO.O
|
||||
..O.OOOO.........O#....O...##OO..O.O........O#.O.O.O##..#....#O...#.......##..#....O...O....O##..O#.
|
||||
.#.#...#..O.#..O#.O..#.....#O..O...O#..#..O.#....O.#O...#O.O#O.##......O.O.#.#.##....##..O..#....O..
|
||||
O...#O.......OO..#.O.#.#..O.....OO.........O.O#..O#O.#.#......OO.#.OO..#..O.#.O..##..#.....O........
|
||||
..#.#.....#.O..O#.........O.O...#..O..O...##...........###O............#.O..O..#..O.......O....O....
|
||||
#O..O....O.#..##O..#.O.#.#...#O..#.##...OO.#.O...O..O..O..###O....OOOO..#O......#O.OO..#.OO#O......#
|
||||
35
2023/14/part1.py
Normal file
35
2023/14/part1.py
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
|
||||
map: list[list[str]] = []
|
||||
|
||||
for line in open("./input2"):
|
||||
map.append([x for x in line.strip()])
|
||||
|
||||
|
||||
def printmap():
|
||||
for line in map:
|
||||
print("".join(line))
|
||||
|
||||
printmap()
|
||||
|
||||
moved = 1
|
||||
while moved:
|
||||
moved = 0
|
||||
for i in range(1, len(map)):
|
||||
for j in range(len(map[i])):
|
||||
if map[i][j] == "O" and map[i-1][j] == ".":
|
||||
map[i][j] = "."
|
||||
map[i-1][j] = "O"
|
||||
moved += 1
|
||||
|
||||
print()
|
||||
|
||||
printmap()
|
||||
|
||||
value = 0
|
||||
|
||||
for i in range(len(map)):
|
||||
for j in range(len(map[i])):
|
||||
if map[i][j] == "O":
|
||||
value += len(map) - i
|
||||
|
||||
print(value)
|
||||
83
2023/14/part2.py
Normal file
83
2023/14/part2.py
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
from functools import lru_cache
|
||||
|
||||
map: list[list[str]] = []
|
||||
|
||||
for line in open("./input1"):
|
||||
map.append([x for x in line.strip()])
|
||||
|
||||
|
||||
def printmap():
|
||||
for line in map:
|
||||
print("".join(line))
|
||||
|
||||
printmap()
|
||||
|
||||
# @lru_cache(maxsize=20)
|
||||
def north(map):
|
||||
moved = 1
|
||||
while moved:
|
||||
moved = 0
|
||||
for i in range(1, len(map)):
|
||||
for j in range(len(map[i])):
|
||||
if map[i][j] == "O" and map[i-1][j] == ".":
|
||||
map[i][j] = "."
|
||||
map[i-1][j] = "O"
|
||||
moved += 1
|
||||
|
||||
# @lru_cache(maxsize=20)
|
||||
def west(map):
|
||||
moved = 1
|
||||
while moved:
|
||||
moved = 0
|
||||
for i in range(len(map)):
|
||||
for j in range(1, len(map[i])):
|
||||
if map[i][j] == "O" and map[i][j-1] == ".":
|
||||
map[i][j] = "."
|
||||
map[i][j-1] = "O"
|
||||
moved += 1
|
||||
# @lru_cache(maxsize=20)
|
||||
def south(map):
|
||||
moved = 1
|
||||
while moved:
|
||||
moved = 0
|
||||
for i in range(len(map)-1):
|
||||
for j in range(len(map[i])):
|
||||
if map[i][j] == "O" and map[i+1][j] == ".":
|
||||
map[i][j] = "."
|
||||
map[i+1][j] = "O"
|
||||
moved += 1
|
||||
# @lru_cache(maxsize=20)
|
||||
def east(map):
|
||||
moved = 1
|
||||
while moved:
|
||||
moved = 0
|
||||
for i in range(len(map)):
|
||||
for j in range(len(map[i])-1):
|
||||
if map[i][j] == "O" and map[i][j+1] == ".":
|
||||
map[i][j] = "."
|
||||
map[i][j+1] = "O"
|
||||
moved += 1
|
||||
|
||||
def cycle():
|
||||
north(map)
|
||||
west(map)
|
||||
south(map)
|
||||
east(map)
|
||||
|
||||
for i in range(1_000_000_000):
|
||||
cycle()
|
||||
|
||||
print()
|
||||
|
||||
printmap()
|
||||
|
||||
|
||||
|
||||
value = 0
|
||||
|
||||
for i in range(len(map)):
|
||||
for j in range(len(map[i])):
|
||||
if map[i][j] == "O":
|
||||
value += len(map) - i
|
||||
|
||||
print(value)
|
||||
227
2023/14/src/main.rs
Normal file
227
2023/14/src/main.rs
Normal file
|
|
@ -0,0 +1,227 @@
|
|||
use std::{fs::File, io::{BufReader, BufRead}, fmt::{Display, Formatter}};
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
enum Item {
|
||||
Empty,
|
||||
Solid,
|
||||
Movable
|
||||
}
|
||||
|
||||
impl Display for Item {
|
||||
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
|
||||
match self {
|
||||
Item::Empty => write!(f, "."),
|
||||
Item::Solid => write!(f, "#"),
|
||||
Item::Movable => write!(f, "O")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn printmap(map: &Vec<Vec<Item>>) {
|
||||
for row in map {
|
||||
for item in row {
|
||||
print!("{}", item);
|
||||
}
|
||||
println!();
|
||||
}
|
||||
}
|
||||
|
||||
fn value(map: &Vec<Vec<Item>>) -> usize {
|
||||
let mut value = 0;
|
||||
for (i, row) in map.iter().enumerate() {
|
||||
for item in row {
|
||||
match item {
|
||||
Item::Movable => value += map.len() - i,
|
||||
_ => ()
|
||||
}
|
||||
}
|
||||
}
|
||||
value
|
||||
}
|
||||
|
||||
fn north(map: &mut Vec<Vec<Item>>) {
|
||||
for x in 0..map.len() {
|
||||
let mut rockcount = vec![];
|
||||
let mut _rock = 0;
|
||||
let mut first_y = 0;
|
||||
|
||||
for y in 0..map.len() {
|
||||
match map[y][x] {
|
||||
Item::Movable => _rock += 1,
|
||||
Item::Solid => {
|
||||
rockcount.push((first_y, _rock));
|
||||
_rock = 0;
|
||||
first_y = y + 1;
|
||||
},
|
||||
_ => ()
|
||||
}
|
||||
}
|
||||
if _rock > 0 {
|
||||
rockcount.push((first_y, _rock));
|
||||
}
|
||||
|
||||
let mut rockshift = 0;
|
||||
|
||||
for y in 0..map.len() {
|
||||
if map[y][x] == Item::Solid {
|
||||
rockshift += 1;
|
||||
continue;
|
||||
}
|
||||
map[y][x] = if rockshift < rockcount.len() && rockcount[rockshift].0 + rockcount[rockshift].1 > y {
|
||||
Item::Movable
|
||||
} else {
|
||||
Item::Empty
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn south(map: &mut Vec<Vec<Item>>) {
|
||||
for x in 0..map.len() {
|
||||
let mut rockcount = vec![];
|
||||
let mut _rock = 0;
|
||||
let mut first_y = 0;
|
||||
|
||||
for y in (0..map.len()).rev() {
|
||||
match map[y][x] {
|
||||
Item::Movable => _rock += 1,
|
||||
Item::Solid => {
|
||||
rockcount.push((first_y, _rock));
|
||||
_rock = 0;
|
||||
first_y = y + 1;
|
||||
},
|
||||
_ => ()
|
||||
}
|
||||
}
|
||||
if _rock > 0 {
|
||||
rockcount.push((first_y, _rock));
|
||||
}
|
||||
|
||||
let mut rockshift = 0;
|
||||
|
||||
for y in 0..map.len() {
|
||||
if map[y][x] == Item::Solid {
|
||||
rockshift += 1;
|
||||
continue;
|
||||
}
|
||||
map[y][x] = if rockshift < rockcount.len() && rockcount[rockshift].0 + rockcount[rockshift].1 < y {
|
||||
Item::Movable
|
||||
} else {
|
||||
Item::Empty
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn east(map: &mut Vec<Vec<Item>>) {
|
||||
for y in 0..map.len() {
|
||||
let mut rockcount = vec![];
|
||||
let mut _rock = 0;
|
||||
let mut first_x = 0;
|
||||
|
||||
for x in (0..map.len()).rev() {
|
||||
match map[y][x] {
|
||||
Item::Movable => _rock += 1,
|
||||
Item::Solid => {
|
||||
rockcount.push((first_x, _rock));
|
||||
_rock = 0;
|
||||
first_x = x + 1;
|
||||
},
|
||||
_ => ()
|
||||
}
|
||||
}
|
||||
if _rock > 0 {
|
||||
rockcount.push((first_x, _rock));
|
||||
}
|
||||
|
||||
let mut rockshift = 0;
|
||||
|
||||
for x in 0..map.len() {
|
||||
if map[y][x] == Item::Solid {
|
||||
rockshift += 1;
|
||||
continue;
|
||||
}
|
||||
map[y][x] = if rockshift < rockcount.len() && rockcount[rockshift].0 + rockcount[rockshift].1 > x {
|
||||
Item::Movable
|
||||
} else {
|
||||
Item::Empty
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn west(map: &mut Vec<Vec<Item>>) {
|
||||
for y in 0..map.len() {
|
||||
let mut rockcount = vec![];
|
||||
let mut _rock = 0;
|
||||
let mut first_x = 0;
|
||||
|
||||
for x in 0..map.len() {
|
||||
match map[y][x] {
|
||||
Item::Movable => _rock += 1,
|
||||
Item::Solid => {
|
||||
rockcount.push((first_x, _rock));
|
||||
_rock = 0;
|
||||
first_x = x + 1;
|
||||
},
|
||||
_ => ()
|
||||
}
|
||||
}
|
||||
if _rock > 0 {
|
||||
rockcount.push((first_x, _rock));
|
||||
}
|
||||
|
||||
let mut rockshift = 0;
|
||||
|
||||
for x in 0..map.len() {
|
||||
if map[y][x] == Item::Solid {
|
||||
rockshift += 1;
|
||||
continue;
|
||||
}
|
||||
map[y][x] = if rockshift < rockcount.len() && rockcount[rockshift].0 + rockcount[rockshift].1 < x {
|
||||
Item::Movable
|
||||
} else {
|
||||
Item::Empty
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut map = vec![];
|
||||
|
||||
let file = File::open("input1").expect("File not found");
|
||||
let reader = BufReader::new(file);
|
||||
|
||||
for line in reader.lines() {
|
||||
let line = line.unwrap();
|
||||
let mut row = vec![];
|
||||
for c in line.chars() {
|
||||
match c {
|
||||
'.' => row.push(Item::Empty),
|
||||
'#' => row.push(Item::Solid),
|
||||
'O' => row.push(Item::Movable),
|
||||
_ => panic!("Unknown item")
|
||||
}
|
||||
}
|
||||
map.push(row);
|
||||
}
|
||||
|
||||
printmap(&map);
|
||||
|
||||
println!("\nnorth");
|
||||
north(&mut map);
|
||||
printmap(&map);
|
||||
println!("\nwest");
|
||||
west(&mut map);
|
||||
printmap(&map);
|
||||
println!("\nsouth");
|
||||
south(&mut map);
|
||||
printmap(&map);
|
||||
println!("\neast");
|
||||
east(&mut map);
|
||||
|
||||
printmap(&map);
|
||||
|
||||
println!("Value: {}", value(&map));
|
||||
}
|
||||
1
2023/15/input1
Normal file
1
2023/15/input1
Normal file
|
|
@ -0,0 +1 @@
|
|||
rn=1,cm-,qp=3,cm=2,qp-,pc=4,ot=9,ab=5,pc-,pc=6,ot=7
|
||||
1
2023/15/input2
Normal file
1
2023/15/input2
Normal file
File diff suppressed because one or more lines are too long
19
2023/15/part1.py
Normal file
19
2023/15/part1.py
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
|
||||
content = ""
|
||||
|
||||
for line in open("./input2"):
|
||||
content += line.strip()
|
||||
|
||||
values = []
|
||||
for part in line.split(","):
|
||||
currentValue = 0
|
||||
for letter in part:
|
||||
asciiValue = ord(letter)
|
||||
currentValue += asciiValue
|
||||
currentValue *= 17
|
||||
currentValue %= 256
|
||||
|
||||
values.append(currentValue)
|
||||
|
||||
print(values)
|
||||
print(sum(values))
|
||||
43
2023/15/part2.py
Normal file
43
2023/15/part2.py
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
import re
|
||||
|
||||
content = ""
|
||||
|
||||
for line in open("./input2"):
|
||||
content += line.strip()
|
||||
|
||||
boxes = []
|
||||
for i in range(256):
|
||||
boxes.append([])
|
||||
# print(boxes)
|
||||
for part in line.split(","):
|
||||
currentValue = 0
|
||||
key, value = re.split(r"=|-", part)
|
||||
for letter in part:
|
||||
if letter == "=":
|
||||
# find if key is in boxes[currentValue]. Boxes is a list of tuples with (key, value)
|
||||
# if it is, replace it
|
||||
# if it is not, append it
|
||||
for i in range(len(boxes[currentValue])):
|
||||
if boxes[currentValue][i][0] == key:
|
||||
boxes[currentValue][i] = (key, value)
|
||||
break
|
||||
else:
|
||||
boxes[currentValue].append((key, value))
|
||||
break
|
||||
if letter == "-":
|
||||
boxes[currentValue] = [x for x in boxes[currentValue] if x[0] != key]
|
||||
break
|
||||
asciiValue = ord(letter)
|
||||
currentValue += asciiValue
|
||||
currentValue *= 17
|
||||
currentValue %= 256
|
||||
# print(key, value)
|
||||
# print(boxes)
|
||||
|
||||
totalPower = 0
|
||||
|
||||
for i in range(len(boxes)):
|
||||
for j in range(len(boxes[i])):
|
||||
totalPower += int(boxes[i][j][1]) * (i + 1) * (j + 1)
|
||||
|
||||
print(totalPower)
|
||||
Loading…
Reference in a new issue