r/adventofcode • • Dec 09 '16

Spoilers [2016 Day 9 (Part 1 & 2)] [Icon Programming Language]

Part 1

procedure main(args)
	inf := open(args[1],"r")
	filelen := 0
	while line := !inf do {
		output := ""
		line ? while not pos(0) do {
			output ||:= tab(upto('(') | 0)
			if ="(" then {
				nchars := integer(tab(many(&digits)))
				="x"
				rep := integer(tab(many(&digits)))
				=")"
				output ||:= repl(move(nchars),rep)
			}
		}
		write(line)
		write(*output," ",output)
		filelen +:= *output
	}
	write(filelen)
end

Part 2

procedure main(args)
	inf := open(args[1],"r")
	filelen := 0
	while line := !inf do {
		linelen := 0
		line ? while not pos(0) do {
			linelen +:= decoder()
		}
		filelen +:= linelen
	}
	write(filelen)
end

# Returns the length of the "next" part
procedure decoder()

	s := &pos
	tab(upto('(')|0)
	l := &pos - s
	if pos(0) then return l

	="("
	nchars := tab(many(&digits))
	="x"
	rep := tab(many(&digits))
	=")"

	npart := move(nchars)
	n := 0
	npart ? while not pos(0) do {
		n +:= decoder()
	}

	return l + (n * rep)
end
0 Upvotes

0 comments sorted by