Files
advent-of-code-2021/day_3/day_3.m
2021-12-03 18:48:44 +00:00

8 lines
324 B
Mathematica

(* ::Package:: *)
nums=Import[StringJoin[NotebookDirectory[], "/input.txt"],"Table"];
part1 = 1##&@@Tr[1##&@@@(#/."up"->-"down")]&;
part2=1##&@@Rest@Fold[Function[{state,elt},Switch[elt[[1]],"forward",state+{0,elt[[2]],state[[1]] elt[[2]]},"down",MapAt[elt[[2]]+#&,state,1],"up",MapAt[#-elt[[2]]&,state,1]]],{0,0,0},#]&;