-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
259 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
function north(lines) { | ||
for (let x = 0; x < lines[0].length; x++) { | ||
let target = 0; | ||
for (let y = 0; y < lines.length; y++) { | ||
if (lines[y][x] === 'O') { | ||
lines[y][x] = '.'; | ||
lines[target][x] = 'O'; | ||
target++; | ||
} | ||
if (lines[y][x] === '#') { | ||
target = y + 1; | ||
} | ||
} | ||
} | ||
} | ||
|
||
function west(lines) { | ||
for (let y = 0; y < lines.length; y++) { | ||
let target = 0; | ||
for (let x = 0; x < lines[0].length; x++) { | ||
if (lines[y][x] === 'O') { | ||
lines[y][x] = '.'; | ||
lines[y][target] = 'O'; | ||
target++; | ||
} | ||
if (lines[y][x] === '#') { | ||
target = x + 1; | ||
} | ||
} | ||
} | ||
} | ||
|
||
function south(lines) { | ||
for (let x = 0; x < lines[0].length; x++) { | ||
let target = lines.length - 1; | ||
for (let y = lines.length - 1; y >= 0; y--) { | ||
if (lines[y][x] === 'O') { | ||
lines[y][x] = '.'; | ||
lines[target][x] = 'O'; | ||
target--; | ||
} | ||
if (lines[y][x] === '#') { | ||
target = y - 1; | ||
} | ||
} | ||
} | ||
} | ||
|
||
function east(lines) { | ||
for (let y = 0; y < lines.length; y++) { | ||
let target = lines[0].length - 1; | ||
for (let x = lines[0].length - 1; x >= 0; x--) { | ||
if (lines[y][x] === 'O') { | ||
lines[y][x] = '.'; | ||
lines[y][target] = 'O'; | ||
target--; | ||
} | ||
if (lines[y][x] === '#') { | ||
target = x - 1; | ||
} | ||
} | ||
} | ||
} | ||
|
||
function weight(lines) { | ||
let result = 0; | ||
for (let i = 0; i < lines[0].length; i++) { | ||
for (let j = 0; j < lines.length; j++) { | ||
if (lines[j][i] === 'O') { | ||
result += lines.length - j; | ||
} | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
export function part1(input) { | ||
const lines = input.split('\n').map(line => line.split('')); | ||
north(lines); | ||
return weight(lines); | ||
} | ||
|
||
export function part2(input) { | ||
const lines = input.split('\n').map(line => line.split('')); | ||
const memory = new Map(); | ||
const count = 1000000000; | ||
for (let i = 0; i < count; i++) { | ||
north(lines); | ||
west(lines); | ||
south(lines); | ||
east(lines); | ||
|
||
const key = JSON.stringify(lines); | ||
const index = memory.get(key); | ||
if (index !== undefined) { | ||
i += Math.floor((count - i - 1) / (i - index)) * (i - index); | ||
} else { | ||
memory.set(key, i); | ||
} | ||
} | ||
return weight(lines); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { part1, part2 } from './day14.js'; | ||
import readInput from '../utils/read-input.js'; | ||
|
||
const input = readInput(import.meta.url); | ||
|
||
describe('day14 2023', () => { | ||
describe('part1', () => { | ||
it('should work for part 1 examples', () => { | ||
expect( | ||
part1( | ||
[ | ||
'O....#....', | ||
'O.OO#....#', | ||
'.....##...', | ||
'OO.#O....O', | ||
'.O.....O#.', | ||
'O.#..O.#.#', | ||
'..O..#O..O', | ||
'.......O..', | ||
'#....###..', | ||
'#OO..#....', | ||
].join('\n'), | ||
), | ||
).toEqual(136); | ||
}); | ||
|
||
it('should work for part 1 input', () => { | ||
expect(part1(input)).toEqual(105784); | ||
}); | ||
}); | ||
|
||
describe('part2', () => { | ||
it('should work for part 2 examples', () => { | ||
expect( | ||
part2( | ||
[ | ||
'O....#....', | ||
'O.OO#....#', | ||
'.....##...', | ||
'OO.#O....O', | ||
'.O.....O#.', | ||
'O.#..O.#.#', | ||
'..O..#O..O', | ||
'.......O..', | ||
'#....###..', | ||
'#OO..#....', | ||
].join('\n'), | ||
), | ||
).toEqual(64); | ||
}); | ||
|
||
it('should work for part 2 input', () => { | ||
expect(part2(input)).toEqual(91286); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
..#..#......#...O.O#O#..#O.O#.O..O.O.#.O..OO.O......###...#..#....OO..O.#.....#.O...#.#OO#O#..O.O#O. | ||
#.#..OOO..#..#...O......#.###O..O.O...#..........#..O......O.#O#......#....O..O#OO.........OO.##O... | ||
..O.#....O.O...O#O...#.##O#OO.#OO.....#.O....O....#OO.O#....O.O.#.......O......O#.O.O...#.....#O.O.O | ||
#OO.O...#.#......#.......##.O#...OO..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.OO.OO.OOOO...#..OOO..O..#O.##...#..OOOO.O####....O#....#O##.O.........O...O | ||
.##...OO..O..#...O.O...O#.O.O.O..OO..O..##O.##...#.OOO.#O.....O...O....OOO.#O...#O#.......#.O.OO.O.. | ||
.O....O..#.OO...###....#.##...O.O#...O.#.O.#....O...#..#.#O.O.#.....O#O#O..OO..#.O.O...OO.O..##.#.O. | ||
.#.##O#.OO....#..###.#.O...#.#O.OO.#..O..O......O...#....OO..##.#O#O.O..O.....#.#..#..O.#...#O....O. | ||
...#.O..#OO...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..#O..O#.O....#...#O.... | ||
.#O..O.#..O#OO#..O.......#.O#O#...O...#O.O...OO#....#....O....#....O#O..OO....O.......#.....O.O#O##. | ||
#....#..#.#..O#.#.OO....##...#O.O###.....O.##O#OO#......O.....O#.#O..O...#...#.O.O.O.#...#.....#.OO# | ||
.#..OO.#...##.O...#......O...#O#.O.#O.#.#....#.#.....OO#.OO.......#O...OO.O.#....#.......#.#.##....O | ||
...O....O.##O.O....#O...#....#OOOO......#..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#..#. | ||
.OO.......##...O...#.#....#O#.#O..#.#O.#......O.O.#.......#O.O........#.#.O.#.OO.#..OO#..#O..O.#...# | ||
...O..O..O#...#......O...#.OO.O....O.........O#O.O..#.#O..##..........O.O..#OO.O.#O..O...O...#...... | ||
...O.#...#.O##.#..OO#OO..#....O.#...#........#...O#...O..#..O..##.....O..##.O..#..#.OO...O.#.#.OO.#. | ||
..O.#..O.O..#......O.....OO..O..OO.O.....O....#....##....OO....#..O#....O....#O#....#...##.....O#..# | ||
OO.....#.##.OO#........O..##.....O.#.O#.....#.OO.OO.O...O.OO...#....#.#.O#.#...#OO...#..O#...O#O.O.. | ||
##...O#.#.O............O....#OOOOOO.....#.O#OO#O.OO....O#.#.O............#...OO.....#.....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......#OOOOO..........##.O......#.......##.##..O.#.....O# | ||
#...#..#O..#.O.#....O..#.O.OOO#..O...#.#..O.OO.#...##..O..#.#..##.OOO....#..O.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.##.......#...#...#...O.........#...O...#.O.O....#.OO..#..OO...O...OOO#O...OO.O#..O.OO.....#O | ||
.....O..#..OOO......##O..O.OO.#....O..O.##.#O..#..O.OOO...O#...OO.O..#.#.O#....#O..O..##.O....O.O..# | ||
O#..O..........O....O##..OO....O.O..O.OOOO....O.......#...#O.#.###O.............#....O.O##..#O.....# | ||
......#.OO#....O..O.........O..#.#O#O....O....OO#O..#.....#.O.#.#.O.OO...O.........#.##..OOO..#...#. | ||
...#..O.....O##.....#O.O.O#....OOO.OO..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.#..O#...O##.O.......#........O..O#..OO..#... | ||
#O..O..##OO....OO.O.....#...O...O...#..O#.#.......#O.OO##.....O.O...O...OOOO...OO....O......O.#....# | ||
OOOO....O..##..OO....O#..##..OOO.#..O.OO.O#O....O.#.#.....OOO.##....O.O.O..#OO..O...#...#O.O.OO...O. | ||
.#..OO.O...##..#......O..........#.O.....#........###..O#..#.O.O#.....O..O..O.#...O....#.O..O......O | ||
....O.#O..#O.#..O...###..OO...O.OOO..#...O...OO.....#.O.#O..O........#..#...##.OOO.OO..#..O...#....O | ||
..OO.....O#........OO#O.O.........#..#.....#.......#.O..#......#O#.O.O.O..OO..O.......O...O......#O. | ||
##....O.....#..#.OOO..O.#.O.###..O...OOOO.#.......O..#...O.O#OO.O#...O....#.O.O..#.....O...O#...#..# | ||
.O#..#..O..#O#.#.........O......O.O##...#..#...O....O..O.#....#...O..OOO..O....#..#.OO.O#.....#....# | ||
.O#.OO.#.....#.O.#.O##O#......O......O.#O..#O#....#..OO.OO#.O.O.O..#OO#.#........###..........##.... | ||
...#O#....#....OO.....O.#..O..O....#..OOO##.......O......##..#...O#...#O#........O.....#..#.O.#.#.O# | ||
.O.O....O.O....OO#.#...O#.OO..O.#...#...#...#.#.......#..#.O..#...O#..O...O..#..#.O.O.....O..#O.O..# | ||
....O.O#...OO##OO....O.OO...O#...#.....O....OO..O.O...#....OO....O#.#.OO.O...........#.#.O..O.#..... | ||
O.O.#.......#O.#O.....#...#OO....#.OO...#O.#..OO#.O....#.O#..........#OO#.......#O..#...#....O#..... | ||
#.#..#.O...O...OOO.#.#..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#......#O...O..##....##....####..O......O..##..OO...........O#.......OO..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.#..OO#O.O..##.#.....O..O.O | ||
.O.O.O#.O...O...O..#O.....O.#OOO..O.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...#OO...O.#..#.O.........OOOO.O...........#..O......#...#.#...##...O....... | ||
....O#.OO...#....O#...#O.......OO.OOO..O......#......#...........####...O.##O.#.....O......#.......O | ||
O...O..O.O.#O.##...OO#.O#O.#...#.#...OO...#O.O.OO.....#.................##.#..O.O.O..O.O.#.......O.O | ||
O.O.#..#...O.O....O........##.#.....OO....O..O.......#..#O.#OO.OO.....##....O.O##....#.....#O.....OO | ||
O.#.....#.......#O..O#......O..O..O#.O..O...#.O...##O#....O.#O....#O#...O......OO.O..O.#O#.#O..#.... | ||
#O.O#....#.....#O........#........#..O.OOO.O......#O#.....#.##.....##.#OO#.O......OO.OOOO.#.O#....O. | ||
O.............#.....OO..OO.#.O#.#.......OO.......O#....#.#.#..........O..#...O.....O#..O...O.O#...#. | ||
O.#O#.........#.#..O#O#..OOO#O#...#....##.#.O.##.O....OO....O......O.O#.O...O#O.#.##..........#OOO.O | ||
...O.......OO...O#O#..O...O...O..OO..O..#..O.........##........O#....O..#.OO...OO...OO..#.##O#....#. | ||
#....#O....O.#..O...O..#O#.....#OO.O#..O..#.....#..##........O..#O#O..........O....#OO..O#...O.O...O | ||
O........O.O.O#..O....O###......O..##O.....O.....#O..O.#.....##.....#OO...OO...#..O#.#...#O.OO##.O.. | ||
.#O.........##.......#O......O#..OOOOOO#O....#..O#.#O#.......O.O...O...O#...#....O....#..#...#O.#.OO | ||
.O.O...#.....OO.#.O..#OO.....#.........O....OO.....#.#.#.#.#..O...##...O..O...OO....#.O.......OO.... | ||
##OO.OOO#.#O#..O....O.O.O...O...O#..#O...O..#......OO.O..#...#O..O.#.OOOO.O...O......#.##O.#O.#...## | ||
OO...O....#.#...#.#.O.#..#..O.O.O..#...#......O.......O......O.O.O......O.....OO##.#..O............. | ||
....#.O.#......#..OO......O#.O.#...O.O.#.O...#O.....#..#............O..#O#.#.O..OO...O.OO..O.#...OO# | ||
O#O..#O...#..OO............#.O..#..O.##OO.....OO.##..O.......O.#.O.....O...OO...OO.O.#......O...O#.. | ||
OO.##.#OO#O.......OO.O...#.OOO...O#....O.O#.O....OO.#..O..OO#OO.#....#..O##O.O.O....OOOO...###..#..O | ||
....OO...#..O..........O.....O..O#.O.......##O.##OOO..OOO...O#.O.O...O..O#O..O.#O.#..O#.....O...OOO. | ||
#..OOOO.#O......#...........O.##..##....#...O##O#......O.O..#...#O#....OO.#..OO..#..OO#.O...##...... | ||
.#O.....#O..O.#O.O.##.#.....OO....O#..##....O#..O.....#...#.O..OOO...##....OO...O..O...OO...OO...#O. | ||
....O..O#.O#....OO#..#.#....OO##.O#O.##.....O........O#O...#.........O.....#.#.O.O...OO...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.........O..#.....OOO.#...##.O.O.....#.OO..#...O.O........O#...O...O..#O...O.#......O.. | ||
....O..O...#O.O.OO.O.#...O#...O..#..O...#O#.#.....OO#..O..#...O#.O..#OO..#...OO.OO.#.#.....#....#.O# | ||
..OOO.#......O..O.......#.......#...#....O.O#...O#.OO...#..#OO.O....#.#.#..O....O.....O.O.O#...O.O.. | ||
O......O.#.O.OO..O..#..O..#.....OO.O.OOOO#..O#....O.#..#...#.##.OO.....O.O.#.#.#.O.#O...OO.O.#.#...O | ||
O...OO..###.O##........O..O.#.....OO..#....#.#.#..#.#O...O...###...#O..#O........#..#O.#.....###.OOO | ||
.O..###O........OO###......#....##.....O.OO.....O.#...#.....O##O..OO.#..O...O#....OO#....O..#..#.OO. | ||
.O..O..O#......O..O...###.....O..#.#....#........#....#.#..O#...O.#.#####O#O.......#O..O..##..O..O.. | ||
.OOO.OO.O###...........#.......O.O......O.....O...O.....OO.O#............#.OO.........OO...O#......# | ||
....#.O#...#.#..OOO.#O.O.O...OO....#...##..O..OOO.......OOO....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 | ||
OOOO..##............OO..O.....O...O.#.....#..#...O.#.O..O.#.##.....O..#..#...#O..OO....O..O...O..... | ||
O..#OO.OO.....OO.O........#...O.O...OO.#.#......#....O...#OO...OO..#.O...#.O.......O...O...O#O##.O## | ||
O.O.O.OO...##....#.......#...#......#..#O.......##.#.#O..#OO......O......#....OO...OO.#.....O.O..##. | ||
O..O......##.....O...#...#.OO..OO.......###.#.......#.....O#OOO..#O.OO#......O..O.#.O..O..OO...#..#. | ||
.....O...#.##.#.##...O#.#..O..OO.#O##O....O#O.O..OO.#O....O....#OO..O.O...OO...O....#....O.##O#...O. | ||
.#.#....#..OOO.....#.....##.##..........#.O...##..O.#O.......OOOO#O.O.O#.O..O..O....O.....#......OO. | ||
O..O##OO#....#O.....OO.O.O.##O.O............OOO..O#.....O...#O...#...........#OO..#.##.#.#.....O#... | ||
.##.OO......#..##..O..OO...O..#...O..#O#.OO.O.OOO...#.OO..O.OO#..O.......#..#..##...O.O##O..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......#.......#OO..O.#.O#OO.##.O.......OO..# | ||
##....#...#.O..#.#..OOO...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..#.O...O.........#...#........#.O..OO..O.#O.##.O..O..OO#.OO#O..OO...O#.O.#..#.#O#.#......O.#...#.O | ||
|