Skip to content

Instantly share code, notes, and snippets.

@Uzo2005

Uzo2005/code.nim Secret

Created February 19, 2023 23:44
Show Gist options
  • Save Uzo2005/82ab2875a75a7d8fe34f60ac26cfc1ee to your computer and use it in GitHub Desktop.
Save Uzo2005/82ab2875a75a7d8fe34f60ac26cfc1ee to your computer and use it in GitHub Desktop.
Recipe for headache (UB in my Nim code)
#This code was written in an attempt to solve 2022 Advent of Code Day 7 [https://adventofcode.com/2022/day/7]
import strutils, sequtils, math
# const data = readfile("data.txt").splitLines()
type
Efile = tuple #Elf file type
fsize: int
fname: string
Edir = object #Elf directory type
files: seq[Efile]
dirs: seq[Edir]
proc touchfile(main: var Edir, file: Efile) = #proc to help add a single file to an elf directory
main.files.add(file)
proc mkdir(main: var Edir, dir: Edir) = #proc to help add a child_directory into a given elf directory
main.dirs.add(dir)
proc newDir(files: seq[(int, string)]): Edir = #proc to help turn any sequence of files into an elf directory
Edir(files: files)
proc dirSize(dir: Edir): int =
# This is the proc giving me a lot of headache
# My plan was for this proc to go through a given directory, add all the file_sizes to the `temp` sequence
# and return the sum of the values in the `temp` sequence.
# To do this, I planned to recursively call `dirSize` on any child_directory I found in the parent directory
# However, when I call the recursive `dirsize` on the `folder` variable on line 42, the compiler complains
# saying that `folder` is of type <Efile>, instead of it being of type <Edir>.
#`Similarly, if `temp` was a seq[int], the compiler would complain that `val` was a string meanwhile it isn't.
var
#temp: seq[int] ---> when temp was a seq[int]
temp: seq[string] # This should have been seq[int] but due to similar issues,
# I had to use seq[string] as a workaround.
for fieldName, fieldValue in dir.fieldPairs:
if fieldName == "files":
for file in fieldValue:
for n, val in file.fieldPairs:
if n == "fsize":
#echo typeof(val) -----> returns int
#temp.add(val) ----> complained that `val` was a string (which is not true), and `temp` was a seq[int] (which was true at that moment)
temp.add($val) #similar problem was occuring but I was able to solve it by casting val to string,
#then mapping over temp and parsing to seq[int] before summation
else:
for folder in fieldValue:
#echo typeof(folder) #-----> This returns Edir
#================================================
temp.add($dirSize(folder)) # -----> Line 51, fails with "type mismatch: got <Efile> instead of <Edir>"
#================================================
result = sum temp.mapIt(parseInt(it)) #Just summing the values of `temp`
runnableExamples:
var root: Edir #make a new elf directory
root.touchfile((12, "hi")) #add a new file to the root directory
root.touchfile((14, "hi5"))# add another file to the directory
root.mkdir(root)#add another directory under root which contains the same thing as root
echo root.dirSize
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment