Last active
December 9, 2023 18:07
-
-
Save depperm/3e118ccd771134dc0a59e26b7bf05876 to your computer and use it in GitHub Desktop.
AoC 2023 day 5
This file contains hidden or 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
# nim compile --run day#/advent.nim demo 1 | |
# nim compile --run day#/advent.nim demo 2 | |
# nim compile --run day#/advent.nim one | |
# nim compile --run day#/advent.nim two | |
import os | |
import strutils | |
import sequtils | |
import sets | |
proc challenge(file: string, challenge: int): void = | |
let input = multiReplace(readFile(file), [("\r", "")]) | |
var inputAndMappings = input.split("\n\n") | |
var curNums = inputAndMappings[0][inputAndMappings[0].find(":")+2 .. high( | |
inputAndMappings[0])].split(" ").map(proc( | |
n: string): int64 = n.parseInt.int64) | |
# get initial seeds as range | |
var seeds: seq[array[2, int64]] | |
if challenge == 1: | |
for num in curNums: | |
seeds.add([num, num]) | |
else: | |
for i in countup(0, (curNums.len/2).int-1): | |
seeds.add([curNums[i*2], curNums[i*2]+curNums[i*2+1]-1]) | |
echo seeds | |
for i in 1 .. high(inputAndMappings): | |
var instruct = inputAndMappings[i].split('\n') | |
var name = instruct[0] | |
instruct.delete(0, 0) | |
# transformation line as numbers | |
var tran = instruct.map(proc(l: string): seq[int] = l.split(' ').map(proc( | |
n: string): int = n.parseInt)) | |
echo name | |
var mapped: seq[array[2, int64]] | |
for t in tran: | |
var dest = t[0].int64 | |
var source = t[1].int64 | |
var step = t[2].int64 | |
var diff = dest-source | |
var i = 0 | |
while i < seeds.len: | |
if seeds[i][1] < source or seeds[i][0] > source+step-1: | |
i+=1 | |
continue | |
# first half of range matches | |
if seeds[i][0] < source and seeds[i][1] < source + step - 1 and seeds[ | |
i][1] >= source: | |
#echo "left", seeds[i] | |
mapped.add([dest, seeds[i][1]+diff]) | |
seeds[i] = [seeds[i][0], source-1] | |
# second half of range matches | |
if seeds[i][1] > source + step - 1 and seeds[i][0] > source and seeds[ | |
i][0] <= source + step - 1: | |
#echo "right", seeds[i] | |
mapped.add([seeds[i][0]+diff, dest+step-1]) | |
seeds[i] = [source+step, seeds[i][1]] | |
# whole range matches/smaller | |
if seeds[i][0] >= source and seeds[i][1] <= source + step - 1: | |
#echo "min", seeds[i] | |
mapped.add([seeds[i][0]+diff, seeds[i][1]+diff]) | |
seeds.delete(i) | |
i-=1 | |
#echo "seeds:", seeds, " mapped:", mapped | |
i+=1 | |
if seeds.len > 0: | |
mapped &= seeds | |
seeds = mapped | |
#echo seeds | |
var min = seeds[0][0] | |
for seed in seeds: | |
if seed[0] < min: | |
min = seed[0] | |
echo min | |
# pt 2: 63092906 too high | |
if paramStr(1) == "demo": | |
challenge("./demo.txt", parseInt(paramStr(2))) | |
elif paramStr(1) == "one": | |
challenge("./input.txt", 1) | |
else: | |
challenge("./input.txt", 2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment