Skip to content

Commit

Permalink
finish 2018
Browse files Browse the repository at this point in the history
  • Loading branch information
Kr0nox committed Dec 7, 2024
1 parent 8bacf2b commit 14b7ef5
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 101 deletions.
182 changes: 85 additions & 97 deletions 2018/17/task.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,26 @@
export async function taskOne(input: string[]): Promise<void> {
const grid = task(input)
let c = 0
for (let x = 0; x < grid.length; x++) {
for (let y = 0; y < grid[0].length; y++) {
if (grid[x][y] == WaterState.WATER || grid[x][y] == WaterState.FLOWING) c++
}
}
console.log(c)
}

import { readdir, writeFile } from 'fs/promises';
export async function taskTwo(input: string[]): Promise<void> {
const grid = task(input)
let c = 0
for (let x = 0; x < grid.length; x++) {
for (let y = 0; y < grid[0].length; y++) {
if (grid[x][y] == WaterState.WATER) c++
}
}
console.log(c)
}

export async function taskOne(input: string[]): Promise<void> {
function task(input: string[]) {
const ranges = parseInput(input)
let minX = 500
let minY = Infinity
Expand All @@ -23,109 +42,95 @@ export async function taskOne(input: string[]): Promise<void> {
}
}
}

const yLen = grid[0].length
grid[500-minX][0] = WaterState.WATER
recurseFrom(500-minX, 0,)
//print(grid)

await writeFile("test.txt", toStringArray(grid).join('\n'))


function recurseFrom(x: number, y: number) {
if (grid[x][y+1] == WaterState.NONE) {
let curY = y
while((curY + 1) < yLen && grid[x][curY+1] == WaterState.NONE) curY++
if ((curY + 1) < yLen) {
let filledRow = true
while(curY > y) {
while(y < curY && canFillRow(x,curY)) {
fillRow(x,curY)
filledRow = true
curY--
for (let y = grid[0].length-2; y >= 0; y--) {
let x = 0
let hadLeft = false
let hadFloorSinceLast = true
while(x < grid.length) {
if (grid[x][y] == WaterState.SAND) {
if (hadLeft && hadFloorSinceLast) {
let dX = x - 1
while(grid[dX][y] != WaterState.SAND) {
grid[dX][y] = WaterState.POTENTIAL_WATER
dX--
}
if (filledRow)
fillDropRow(x, curY)
else
curY--
filledRow = false
}

}
curY = y + 1
while(grid[x][curY] == WaterState.NONE) {
grid[x][curY] = WaterState.WATER
curY++
}



hadFloorSinceLast = true
hadLeft = true
}
if (grid[x][y+1] == WaterState.NONE) hadFloorSinceLast = false
x++
}
}
const yLen = grid[0].length
drop(500-minX,0)
return grid

function canFillRow(x: number, y:number) {
let cX = x
while(cX < grid.length && grid[cX][y] != WaterState.SAND && grid[cX][y+1] != WaterState.NONE) cX++
if (cX >= grid.length || grid[cX][y+1] == WaterState.NONE) return false
cX = x
while(cX >= 0 && grid[cX][y] != WaterState.SAND && grid[cX][y+1] != WaterState.NONE) cX--
if (cX < 0 || grid[cX][y+1] == WaterState.NONE) return false
return true
}


function fillRow(x: number, y: number) {
let cX = x
while(cX >= 0 && grid[cX][y] != WaterState.SAND && grid[cX][y+1] != WaterState.NONE) cX--

cX++
while(grid[cX][y] != WaterState.SAND) {
grid[cX][y] = WaterState.WATER
cX++
function drop(x: number, y: number) {
while(y < yLen && grid[x][y] == WaterState.NONE) {
grid[x][y] = WaterState.FLOWING
y++
}
}
if (y >= yLen) return
if (grid[x][y] == WaterState.WATER || grid[x][y] == WaterState.FLOWING) return

function fillDropRow(x: number, y: number) {
let cX = x
while(cX < grid.length && grid[cX][y+1] != WaterState.NONE && grid[cX][y] != WaterState.SAND) {
grid[cX][y] = WaterState.WATER
cX++
}
if (cX < grid.length && grid[cX][y+1] == WaterState.NONE && grid[cX][y] != WaterState.SAND) {
grid[cX][y] = WaterState.WATER
recurseFrom(cX,y)
fillPotential(x,y)

let dX = x + 1
y--
while((grid[dX][y+1] == WaterState.SAND || grid[dX][y+1] == WaterState.WATER) && grid[dX][y] == WaterState.NONE) {
grid[dX][y] = WaterState.FLOWING
dX++
}
cX = x - 1
while(cX >= 0 && grid[cX][y+1] != WaterState.NONE && grid[cX][y] != WaterState.SAND) {
grid[cX][y] = WaterState.WATER
cX--

if (grid[dX][y] == WaterState.NONE) {
drop(dX, y)
}

if (cX >= 0 && grid[cX][y+1] == WaterState.NONE && grid[cX][y] != WaterState.SAND) {
grid[cX][y] = WaterState.WATER
recurseFrom(cX,y)
dX = x - 1
while((grid[dX][y+1] == WaterState.SAND || grid[dX][y+1] == WaterState.WATER) && grid[dX][y] == WaterState.NONE) {
grid[dX][y] = WaterState.FLOWING
dX--
}
if (grid[dX][y] == WaterState.NONE) {
drop(dX, y)
}
}

let count = 0

for (let x = 0; x < grid.length; x++) {
for (let y = 0; y < grid[x].length; y++) {
if (grid[x][y] == WaterState.WATER) count++
function fillPotential(x: number, y: number) {
let tY = y
while (grid[x][tY] == WaterState.POTENTIAL_WATER) {
let dX = x
while(grid[dX][tY] != WaterState.SAND) {
grid[dX][tY] = WaterState.WATER
if (grid[dX][tY+1] == WaterState.POTENTIAL_WATER) {
fillPotential(dX,tY+1)
}
dX++
}
dX = x
while(grid[dX][tY] != WaterState.SAND) {
grid[dX][tY] = WaterState.WATER
if (grid[dX][tY+1] == WaterState.POTENTIAL_WATER) {
fillPotential(dX,tY+1)
}
dX--
}
tY++
}
}
console.log(count)
}

export async function taskTwo(input: string[]): Promise<void> {
console.log("Unimplemented");
}

interface Range {
x1: number, x2: number, y1: number, y2: number
}

enum WaterState {
NONE, SAND, WATER
NONE, SAND, WATER, POTENTIAL_WATER, FLOWING
}

function parseInput(input: string[]) {
Expand All @@ -148,21 +153,4 @@ function parseInput(input: string[]) {
}
return r
})
}

function print(grid: WaterState[][]) {
console.log(toStringArray(grid))
}

function toStringArray(grid: WaterState[][]) {
const p = Array.from({length: grid[0].length}, () => Array.from({length: grid.length}, () => ' '))
for (let x = 0; x < grid.length; x++) {
for (let y = 0; y < grid[x].length; y++) {
if (grid[x][y] == WaterState.WATER) p[y][x] = '~'
if (grid[x][y] == WaterState.SAND) p[y][x] = '#'
//if (grid[x][y] == WaterState.TEST1) p[y][x] = '1'
//if (grid[x][y] == WaterState.TEST2) p[y][x] = '2'
}
}
return p.map(i => i.join(''))
}
29 changes: 29 additions & 0 deletions 2018/25/runner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { readFile } from "fs/promises";
import { taskOne, taskTwo } from "./task";

async function main() {
let lastArgument = process.argv.pop() as string;
let taskNumber = 1;
let isTest = false;

if (lastArgument === "test") {
isTest = true;
taskNumber = parseInt(process.argv.pop() as string);
} else {
taskNumber = parseInt(lastArgument)
}

const fileToLoad = isTest ? "test.in" : "solve.in";
const fileContents = await readFile(fileToLoad, "utf-8")

const lines = fileContents.split("\n");

if (taskNumber === 1) {
await taskOne(lines);
}
if (taskNumber === 2) {
await taskTwo(lines);
}
}

void main();
40 changes: 40 additions & 0 deletions 2018/25/task.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
export async function taskOne(input: string[]): Promise<void> {
const points = input.map(i=>i.split(',').map(Number))
const graph: number[][] = Array.from({length:input.length}, () => [])

for (let i = 0; i < points.length; i++) {
for (let j = i+1; j < points.length; j++) {
let dist = [
points[i][0]-points[j][0],
points[i][1]-points[j][1],
points[i][2]-points[j][2],
points[i][3]-points[j][3],
].map(Math.abs).reduce((a,b)=>a+b,0)
if (dist <= 3) {
graph[i].push(j)
graph[j].push(i)
}
}
}

const visited = new Set<number>
let groups = 0
for (let i = 0; i < graph.length; i++) {
if (visited.has(i)) continue
groups++
const stack = [i]
while(stack.length > 0) {
const q = stack.pop()!
if (visited.has(q)) continue
visited.add(q)
for (const n of graph[q]) {
stack.push(n)
}
}
}
console.log(groups)
}

export async function taskTwo(input: string[]): Promise<void> {
console.log("Unimplemented");
}
2 changes: 1 addition & 1 deletion 2018/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 2018
![](https://img.shields.io/badge/stars%20⭐-46-yellow)
![](https://img.shields.io/badge/stars%20⭐-50-yellow)

Puzzles not solved during event.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# My Advent of Code Solutions

![](https://img.shields.io/badge/Total%20stars%20⭐-423-yellow)
![](https://img.shields.io/badge/Total%20stars%20⭐-443-yellow)

## What language do I use?
Whatever I feel like today. But it will most likely either be Java, Python, C++, Typescript or Haskell
Expand Down Expand Up @@ -40,11 +40,11 @@ Whatever I feel like today. But it will most likely either be Java, Python, C++,

<h3><a href="2019/README.md">2019</a></h3>

![](https://img.shields.io/badge/stars%20⭐-25-yellow)
![](https://img.shields.io/badge/stars%20⭐-29-yellow)

<h3><a href="2018/README.md">2019</a></h3>

![](https://img.shields.io/badge/stars%20⭐-46-yellow)
![](https://img.shields.io/badge/stars%20⭐-50-yellow)

<h3><a href="2017/README.md">2017</a></h3>

Expand Down

0 comments on commit 14b7ef5

Please sign in to comment.