advent of code 2025 in ts and nix
at main 1.7 kB view raw
1let 2 input = builtins.readFile ../../shared/01/input.txt; 3 lines = builtins.filter (s: builtins.isString s && s != "") (builtins.split "\n" input); 4 5 mod = a: b: a - (a / b) * b; 6 7 part1 = 8 let 9 processLine = state: line: 10 let 11 dir = builtins.substring 0 1 line == "R"; 12 num = builtins.fromJSON (builtins.substring 1 (builtins.stringLength line - 1) line); 13 newDial = mod (if dir then state.dial + num else state.dial - num + 100) 100; 14 in { 15 dial = newDial; 16 count = state.count + (if newDial == 0 then 1 else 0); 17 }; 18 19 result = builtins.foldl' processLine { dial = 50; count = 0; } lines; 20 in result.count; 21 22 part2 = 23 let 24 processLine = state: line: 25 let 26 dir = builtins.substring 0 1 line; 27 num = builtins.fromJSON (builtins.substring 1 (builtins.stringLength line - 1) line); 28 dialBefore = state.dial; 29 30 distToZero = 31 let raw = if dir == "R" then mod (100 - dialBefore) 100 else mod dialBefore 100; 32 in if raw == 0 then 100 else raw; 33 34 passCount = if num >= distToZero then 1 + (num - distToZero) / 100 else 0; 35 36 newDial = 37 let d = if dir == "R" 38 then mod (dialBefore + num) 100 39 else mod (100 + dialBefore - (mod num 100)) 100; 40 in mod (100 + d) 100; 41 in { 42 dial = newDial; 43 count = state.count + passCount; 44 }; 45 46 result = builtins.foldl' processLine { dial = 50; count = 0; } lines; 47 in result.count; 48 49in { 50 inherit part1 part2; 51}